2 Low-level kernel interface to the XenStore.
4 The XenStore interface is a simple storage system that is a means of
5 communicating state and configuration data between the Xen Domain 0
6 and the various guest domains. All configuration data other than
7 a small amount of essential information required during the early
8 boot process of launching a Xen aware guest, is managed using the
11 The XenStore is ASCII string based, and has a structure and semantics
12 similar to a filesystem. There are files and directories, the directories
13 able to contain files or other directories. The depth of the hierachy
14 is only limited by the XenStore's maximum path length.
16 The communication channel between the XenStore service and other
17 domains is via two, guest specific, ring buffers in a shared memory
18 area. One ring buffer is used for communicating in each direction.
19 The grant table references for this shared memory are given to the
20 guest either via the xen_start_info structure for a fully para-
21 virtualized guest, or via HVM hypercalls for a hardware virtualized
24 The XenStore communication relies on an event channel and thus
25 interrupts. But under OVMF this XenStore client will pull the
26 state of the event channel.
28 Several Xen services depend on the XenStore, most notably the
29 XenBus used to discover and manage Xen devices.
31 Copyright (C) 2005 Rusty Russell, IBM Corporation
32 Copyright (C) 2009,2010 Spectra Logic Corporation
33 Copyright (C) 2014, Citrix Ltd.
35 This file may be distributed separately from the Linux kernel, or
36 incorporated into other software packages, subject to the following license:
38 Permission is hereby granted, free of charge, to any person obtaining a copy
39 of this source file (the "Software"), to deal in the Software without
40 restriction, including without limitation the rights to use, copy, modify,
41 merge, publish, distribute, sublicense, and/or sell copies of the Software,
42 and to permit persons to whom the Software is furnished to do so, subject to
43 the following conditions:
45 The above copyright notice and this permission notice shall be included in
46 all copies or substantial portions of the Software.
48 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
59 #include <Library/PrintLib.h>
61 #include <IndustryStandard/Xen/hvm/params.h>
63 #include "XenHypercall.h"
64 #include "EventChannel.h"
67 // Private Data Structures
75 /* Register callback to watch subtree (node) in the XenStore. */
76 #define XENSTORE_WATCH_SIGNATURE SIGNATURE_32 ('X','S','w','a')
77 struct _XENSTORE_WATCH
82 /* Path being watched. */
86 #define XENSTORE_WATCH_FROM_LINK(l) \
87 CR (l, XENSTORE_WATCH, Link, XENSTORE_WATCH_SIGNATURE)
91 * Structure capturing messages received from the XenStore service.
93 #define XENSTORE_MESSAGE_SIGNATURE SIGNATURE_32 ('X', 'S', 's', 'm')
98 struct xsd_sockmsg Header
;
101 /* Queued replies. */
106 /* Queued watch events. */
108 XENSTORE_WATCH
*Handle
;
109 CONST CHAR8
**Vector
;
114 #define XENSTORE_MESSAGE_FROM_LINK(r) \
115 CR (r, XENSTORE_MESSAGE, Link, XENSTORE_MESSAGE_SIGNATURE)
118 * Container for all XenStore related state.
122 * Pointer to shared memory communication structures allowing us
123 * to communicate with the XenStore service.
125 struct xenstore_domain_interface
*XenStore
;
130 * A list of replies to our requests.
132 * The reply list is filled by xs_rcv_thread(). It
133 * is consumed by the context that issued the request
134 * to which a reply is made. The requester blocks in
135 * XenStoreReadReply ().
137 * /note Only one requesting context can be active at a time.
139 LIST_ENTRY ReplyList
;
141 /** Lock protecting the reply list. */
145 * List of registered watches.
147 LIST_ENTRY RegisteredWatches
;
149 /** Lock protecting the registered watches list. */
150 EFI_LOCK RegisteredWatchesLock
;
153 * List of pending watch callback events.
155 LIST_ENTRY WatchEvents
;
157 /** Lock protecting the watch calback list. */
158 EFI_LOCK WatchEventsLock
;
161 * The event channel for communicating with the
164 evtchn_port_t EventChannel
;
166 /** Handle for XenStore events. */
167 EFI_EVENT EventChannelEvent
;
173 static XENSTORE_PRIVATE xs
;
177 // Private Utility Functions
181 Count and optionally record pointers to a number of NUL terminated
184 @param Strings A pointer to a contiguous buffer of NUL terminated strings.
185 @param Len The length of the buffer pointed to by strings.
186 @param Dst An array to store pointers to each string found in strings.
188 @return A count of the number of strings found.
193 IN CONST CHAR8
*Strings
,
195 OUT CONST CHAR8
**Dst OPTIONAL
201 for (Ptr
= Strings
; Ptr
< Strings
+ Len
; Ptr
+= AsciiStrSize (Ptr
)) {
212 Convert a contiguous buffer containing a series of NUL terminated
213 strings into an array of pointers to strings.
215 The returned pointer references the array of string pointers which
216 is followed by the storage for the string data. It is the client's
217 responsibility to free this storage.
219 The storage addressed by Strings is free'd prior to Split returning.
221 @param Strings A pointer to a contiguous buffer of NUL terminated strings.
222 @param Len The length of the buffer pointed to by strings.
223 @param NumPtr The number of strings found and returned in the strings
226 @return An array of pointers to the strings found in the input buffer.
238 ASSERT(NumPtr
!= NULL
);
239 ASSERT(Strings
!= NULL
);
241 /* Protect against unterminated buffers. */
243 Strings
[Len
- 1] = '\0';
246 /* Count the Strings. */
247 *NumPtr
= ExtractStrings (Strings
, Len
, NULL
);
249 /* Transfer to one big alloc for easy freeing by the caller. */
250 Dst
= AllocatePool (*NumPtr
* sizeof (CHAR8
*) + Len
);
251 CopyMem (&Dst
[*NumPtr
], Strings
, Len
);
254 /* Extract pointers to newly allocated array. */
255 Strings
= (CHAR8
*) &Dst
[*NumPtr
];
256 ExtractStrings (Strings
, Len
, Dst
);
262 Convert from watch token (unique identifier) to the associated
263 internal tracking structure for this watch.
265 @param Tocken The unique identifier for the watch to find.
267 @return A pointer to the found watch structure or NULL.
272 IN CONST CHAR8
*Token
275 XENSTORE_WATCH
*Watch
, *WantedWatch
;
278 WantedWatch
= (VOID
*) AsciiStrHexToUintn (Token
);
280 if (IsListEmpty (&xs
.RegisteredWatches
)) {
283 for (Entry
= GetFirstNode (&xs
.RegisteredWatches
);
284 !IsNull (&xs
.RegisteredWatches
, Entry
);
285 Entry
= GetNextNode (&xs
.RegisteredWatches
, Entry
)) {
286 Watch
= XENSTORE_WATCH_FROM_LINK (Entry
);
287 if (Watch
== WantedWatch
)
295 // Public Utility Functions
296 // API comments for these methods can be found in XenStore.h
301 IN CONST CHAR8
*DirectoryPath
,
307 /* +1 for '/' and +1 for '\0' */
308 Buf
= AllocateZeroPool (
309 AsciiStrLen (DirectoryPath
) + AsciiStrLen (Node
) + 2);
310 AsciiStrCat (Buf
, DirectoryPath
);
311 if (Node
[0] != '\0') {
312 AsciiStrCat (Buf
, "/");
313 AsciiStrCat (Buf
, Node
);
320 // Low Level Communication Management
324 Verify that the indexes for a ring are valid.
326 The difference between the producer and consumer cannot
327 exceed the size of the ring.
329 @param Cons The consumer index for the ring to test.
330 @param Prod The producer index for the ring to test.
332 @retval TRUE If indexes are in range.
333 @retval FALSE If the indexes are out of range.
337 XenStoreCheckIndexes (
338 XENSTORE_RING_IDX Cons
,
339 XENSTORE_RING_IDX Prod
342 return ((Prod
- Cons
) <= XENSTORE_RING_SIZE
);
346 Return a pointer to, and the length of, the contiguous
347 free region available for output in a ring buffer.
349 @param Cons The consumer index for the ring.
350 @param Prod The producer index for the ring.
351 @param Buffer The base address of the ring's storage.
352 @param LenPtr The amount of contiguous storage available.
354 @return A pointer to the start location of the free region.
358 XenStoreGetOutputChunk (
359 IN XENSTORE_RING_IDX Cons
,
360 IN XENSTORE_RING_IDX Prod
,
366 Len
= XENSTORE_RING_SIZE
- MASK_XENSTORE_IDX (Prod
);
367 if ((XENSTORE_RING_SIZE
- (Prod
- Cons
)) < Len
) {
368 Len
= XENSTORE_RING_SIZE
- (Prod
- Cons
);
371 return (Buffer
+ MASK_XENSTORE_IDX (Prod
));
375 Return a pointer to, and the length of, the contiguous
376 data available to read from a ring buffer.
378 @param Cons The consumer index for the ring.
379 @param Prod The producer index for the ring.
380 @param Buffer The base address of the ring's storage.
381 @param LenPtr The amount of contiguous data available to read.
383 @return A pointer to the start location of the available data.
387 XenStoreGetInputChunk (
388 IN XENSTORE_RING_IDX Cons
,
389 IN XENSTORE_RING_IDX Prod
,
390 IN CONST CHAR8
*Buffer
,
396 Len
= XENSTORE_RING_SIZE
- MASK_XENSTORE_IDX (Cons
);
397 if ((Prod
- Cons
) < Len
) {
401 return (Buffer
+ MASK_XENSTORE_IDX (Cons
));
405 Wait for an event or timeout.
407 @param Event Event to wait for.
408 @param Timeout A timeout value in 100ns units.
410 @retval EFI_SUCCESS Event have been triggered or the current TPL is not
412 @retval EFI_TIMEOUT Timeout have expired.
416 XenStoreWaitForEvent (
423 EFI_EVENT TimerEvent
;
424 EFI_EVENT WaitList
[2];
426 gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
427 gBS
->SetTimer (TimerEvent
, TimerRelative
, Timeout
);
429 WaitList
[0] = xs
.EventChannelEvent
;
430 WaitList
[1] = TimerEvent
;
431 Status
= gBS
->WaitForEvent (2, WaitList
, &Index
);
432 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
433 gBS
->CloseEvent (TimerEvent
);
434 if (Status
== EFI_UNSUPPORTED
) {
445 Transmit data to the XenStore service.
447 The buffer pointed to by DataPtr is at least Len bytes in length.
449 @param DataPtr A pointer to the contiguous data to send.
450 @param Len The amount of data to send.
452 @return On success 0, otherwise an errno value indicating the
458 IN CONST VOID
*DataPtr
,
462 XENSTORE_RING_IDX Cons
, Prod
;
463 CONST CHAR8
*Data
= (CONST CHAR8
*)DataPtr
;
469 Cons
= xs
.XenStore
->req_cons
;
470 Prod
= xs
.XenStore
->req_prod
;
471 if ((Prod
- Cons
) == XENSTORE_RING_SIZE
) {
473 * Output ring is full. Wait for a ring event.
475 * Note that the events from both queues are combined, so being woken
476 * does not guarantee that data exist in the read ring.
480 Status
= XenStoreWaitForEvent (xs
.EventChannelEvent
,
481 EFI_TIMER_PERIOD_SECONDS (1));
482 if (Status
== EFI_TIMEOUT
) {
483 DEBUG ((EFI_D_WARN
, "XenStore Write, waiting for a ring event.\n"));
488 /* Verify queue sanity. */
489 if (!XenStoreCheckIndexes (Cons
, Prod
)) {
490 xs
.XenStore
->req_cons
= xs
.XenStore
->req_prod
= 0;
491 return XENSTORE_STATUS_EIO
;
494 Dest
= XenStoreGetOutputChunk (Cons
, Prod
, xs
.XenStore
->req
, &Available
);
495 if (Available
> Len
) {
499 CopyMem (Dest
, Data
, Available
);
504 * The store to the producer index, which indicates
505 * to the other side that new data has arrived, must
506 * be visible only after our copy of the data into the
507 * ring has completed.
510 xs
.XenStore
->req_prod
+= Available
;
513 * The other side will see the change to req_prod at the time of the
517 XenEventChannelNotify (xs
.Dev
, xs
.EventChannel
);
520 return XENSTORE_STATUS_SUCCESS
;
524 Receive data from the XenStore service.
526 The buffer pointed to by DataPtr is at least Len bytes in length.
528 @param DataPtr A pointer to the contiguous buffer to receive the data.
529 @param Len The amount of data to receive.
531 @return On success 0, otherwise an errno value indicating the
541 XENSTORE_RING_IDX Cons
, Prod
;
542 CHAR8
*Data
= (CHAR8
*) DataPtr
;
548 Cons
= xs
.XenStore
->rsp_cons
;
549 Prod
= xs
.XenStore
->rsp_prod
;
552 * Nothing to read. Wait for a ring event.
554 * Note that the events from both queues are combined, so being woken
555 * does not guarantee that data exist in the read ring.
559 Status
= XenStoreWaitForEvent (xs
.EventChannelEvent
,
560 EFI_TIMER_PERIOD_SECONDS (1));
561 if (Status
== EFI_TIMEOUT
) {
562 DEBUG ((EFI_D_WARN
, "XenStore Read, waiting for a ring event.\n"));
567 /* Verify queue sanity. */
568 if (!XenStoreCheckIndexes (Cons
, Prod
)) {
569 xs
.XenStore
->rsp_cons
= xs
.XenStore
->rsp_prod
= 0;
570 return XENSTORE_STATUS_EIO
;
573 Src
= XenStoreGetInputChunk (Cons
, Prod
, xs
.XenStore
->rsp
, &Available
);
574 if (Available
> Len
) {
579 * Insure the data we read is related to the indexes
584 CopyMem (Data
, Src
, Available
);
589 * Insure that the producer of this ring does not see
590 * the ring space as free until after we have copied it
594 xs
.XenStore
->rsp_cons
+= Available
;
597 * The producer will see the updated consumer index when the event is
601 XenEventChannelNotify (xs
.Dev
, xs
.EventChannel
);
604 return XENSTORE_STATUS_SUCCESS
;
608 // Received Message Processing
612 Block reading the next message from the XenStore service and
615 @return XENSTORE_STATUS_SUCCESS on success. Otherwise an errno value
616 indicating the type of failure encountered.
620 XenStoreProcessMessage (
624 XENSTORE_MESSAGE
*Message
;
626 XENSTORE_STATUS Status
;
628 Message
= AllocateZeroPool (sizeof (XENSTORE_MESSAGE
));
629 Message
->Signature
= XENSTORE_MESSAGE_SIGNATURE
;
630 Status
= XenStoreReadStore (&Message
->Header
, sizeof (Message
->Header
));
631 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
633 DEBUG ((EFI_D_ERROR
, "XenStore: Error read store (%d)\n", Status
));
637 Body
= AllocatePool (Message
->Header
.len
+ 1);
638 Status
= XenStoreReadStore (Body
, Message
->Header
.len
);
639 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
642 DEBUG ((EFI_D_ERROR
, "XenStore: Error read store (%d)\n", Status
));
645 Body
[Message
->Header
.len
] = '\0';
647 if (Message
->Header
.type
== XS_WATCH_EVENT
) {
648 Message
->u
.Watch
.Vector
= Split(Body
, Message
->Header
.len
,
649 &Message
->u
.Watch
.VectorSize
);
651 EfiAcquireLock (&xs
.RegisteredWatchesLock
);
652 Message
->u
.Watch
.Handle
=
653 XenStoreFindWatch (Message
->u
.Watch
.Vector
[XS_WATCH_TOKEN
]);
654 DEBUG ((EFI_D_INFO
, "XenStore: Watch event %a\n",
655 Message
->u
.Watch
.Vector
[XS_WATCH_TOKEN
]));
656 if (Message
->u
.Watch
.Handle
!= NULL
) {
657 EfiAcquireLock (&xs
.WatchEventsLock
);
658 InsertHeadList (&xs
.WatchEvents
, &Message
->Link
);
659 EfiReleaseLock (&xs
.WatchEventsLock
);
661 DEBUG ((EFI_D_WARN
, "XenStore: Watch handle %a not found\n",
662 Message
->u
.Watch
.Vector
[XS_WATCH_TOKEN
]));
663 FreePool(Message
->u
.Watch
.Vector
);
666 EfiReleaseLock (&xs
.RegisteredWatchesLock
);
668 Message
->u
.Reply
.Body
= Body
;
669 EfiAcquireLock (&xs
.ReplyLock
);
670 InsertTailList (&xs
.ReplyList
, &Message
->Link
);
671 EfiReleaseLock (&xs
.ReplyLock
);
674 return XENSTORE_STATUS_SUCCESS
;
678 // XenStore Message Request/Reply Processing
682 Convert a XenStore error string into an errno number.
684 Unknown error strings are converted to EINVAL.
686 @param errorstring The error string to convert.
688 @return The errno best matching the input string.
692 XENSTORE_STATUS Status
;
693 CONST CHAR8
*ErrorStr
;
696 static XenStoreErrors gXenStoreErrors
[] = {
697 { XENSTORE_STATUS_EINVAL
, "EINVAL" },
698 { XENSTORE_STATUS_EACCES
, "EACCES" },
699 { XENSTORE_STATUS_EEXIST
, "EEXIST" },
700 { XENSTORE_STATUS_EISDIR
, "EISDIR" },
701 { XENSTORE_STATUS_ENOENT
, "ENOENT" },
702 { XENSTORE_STATUS_ENOMEM
, "ENOMEM" },
703 { XENSTORE_STATUS_ENOSPC
, "ENOSPC" },
704 { XENSTORE_STATUS_EIO
, "EIO" },
705 { XENSTORE_STATUS_ENOTEMPTY
, "ENOTEMPTY" },
706 { XENSTORE_STATUS_ENOSYS
, "ENOSYS" },
707 { XENSTORE_STATUS_EROFS
, "EROFS" },
708 { XENSTORE_STATUS_EBUSY
, "EBUSY" },
709 { XENSTORE_STATUS_EAGAIN
, "EAGAIN" },
710 { XENSTORE_STATUS_EISCONN
, "EISCONN" },
711 { XENSTORE_STATUS_E2BIG
, "E2BIG" }
713 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
718 CONST CHAR8
*ErrorStr
723 for (Index
= 0; Index
< ARRAY_SIZE(gXenStoreErrors
); Index
++) {
724 if (!AsciiStrCmp (ErrorStr
, gXenStoreErrors
[Index
].ErrorStr
)) {
725 return gXenStoreErrors
[Index
].Status
;
728 DEBUG ((EFI_D_WARN
, "XenStore gave unknown error %a\n", ErrorStr
));
729 return XENSTORE_STATUS_EINVAL
;
733 Block waiting for a reply to a message request.
735 @param TypePtr The returned type of the reply.
736 @param LenPtr The returned body length of the reply.
737 @param Result The returned body of the reply.
742 OUT
enum xsd_sockmsg_type
*TypePtr
,
743 OUT UINT32
*LenPtr OPTIONAL
,
747 XENSTORE_MESSAGE
*Message
;
751 while (IsListEmpty (&xs
.ReplyList
)) {
752 XENSTORE_STATUS Status
;
753 Status
= XenStoreProcessMessage ();
754 if (Status
!= XENSTORE_STATUS_SUCCESS
&& Status
!= XENSTORE_STATUS_EAGAIN
) {
755 DEBUG ((EFI_D_ERROR
, "XenStore, error while reading the ring (%d).",
760 EfiAcquireLock (&xs
.ReplyLock
);
761 Entry
= GetFirstNode (&xs
.ReplyList
);
762 Message
= XENSTORE_MESSAGE_FROM_LINK (Entry
);
763 RemoveEntryList (Entry
);
764 EfiReleaseLock (&xs
.ReplyLock
);
766 *TypePtr
= Message
->Header
.type
;
767 if (LenPtr
!= NULL
) {
768 *LenPtr
= Message
->Header
.len
;
770 Body
= Message
->u
.Reply
.Body
;
774 return XENSTORE_STATUS_SUCCESS
;
778 Send a message with an optionally muti-part body to the XenStore service.
780 @param Transaction The transaction to use for this request.
781 @param RequestType The type of message to send.
782 @param WriteRequest Pointers to the body sections of the request.
783 @param NumRequests The number of body sections in the request.
784 @param LenPtr The returned length of the reply.
785 @param ResultPtr The returned body of the reply.
787 @return XENSTORE_STATUS_SUCCESS on success. Otherwise an errno indicating
788 the cause of failure.
793 IN XENSTORE_TRANSACTION Transaction
,
794 IN
enum xsd_sockmsg_type RequestType
,
795 IN CONST WRITE_REQUEST
*WriteRequest
,
796 IN UINT32 NumRequests
,
797 OUT UINT32
*LenPtr OPTIONAL
,
798 OUT VOID
**ResultPtr OPTIONAL
801 struct xsd_sockmsg Message
;
804 XENSTORE_STATUS Status
;
806 Message
.tx_id
= Transaction
.Id
;
808 Message
.type
= RequestType
;
810 for (Index
= 0; Index
< NumRequests
; Index
++) {
811 Message
.len
+= WriteRequest
[Index
].Len
;
814 Status
= XenStoreWriteStore (&Message
, sizeof (Message
));
815 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
816 DEBUG ((EFI_D_ERROR
, "XenStoreTalkv failed %d\n", Status
));
820 for (Index
= 0; Index
< NumRequests
; Index
++) {
821 Status
= XenStoreWriteStore (WriteRequest
[Index
].Data
, WriteRequest
[Index
].Len
);
822 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
823 DEBUG ((EFI_D_ERROR
, "XenStoreTalkv failed %d\n", Status
));
828 Status
= XenStoreReadReply (&Message
.type
, LenPtr
, &Return
);
831 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
835 if (Message
.type
== XS_ERROR
) {
836 Status
= XenStoreGetError (Return
);
841 /* Reply is either error or an echo of our request message type. */
842 ASSERT (Message
.type
== RequestType
);
850 return XENSTORE_STATUS_SUCCESS
;
854 Wrapper for XenStoreTalkv allowing easy transmission of a message with
855 a single, contiguous, message body.
857 The returned result is provided in malloced storage and thus must be free'd
860 @param Transaction The transaction to use for this request.
861 @param RequestType The type of message to send.
862 @param Body The body of the request.
863 @param LenPtr The returned length of the reply.
864 @param Result The returned body of the reply.
866 @return 0 on success. Otherwise an errno indicating
867 the cause of failure.
872 IN XENSTORE_TRANSACTION Transaction
,
873 IN
enum xsd_sockmsg_type RequestType
,
874 IN CONST CHAR8
*Body
,
875 OUT UINT32
*LenPtr OPTIONAL
,
876 OUT VOID
**Result OPTIONAL
879 WRITE_REQUEST WriteRequest
;
881 WriteRequest
.Data
= (VOID
*) Body
;
882 WriteRequest
.Len
= AsciiStrSize (Body
);
884 return XenStoreTalkv (Transaction
, RequestType
, &WriteRequest
, 1,
889 // XenStore Watch Support
893 Transmit a watch request to the XenStore service.
895 @param Path The path in the XenStore to watch.
896 @param Tocken A unique identifier for this watch.
898 @return XENSTORE_STATUS_SUCCESS on success. Otherwise an errno indicating the
908 WRITE_REQUEST WriteRequest
[2];
910 WriteRequest
[0].Data
= (VOID
*) Path
;
911 WriteRequest
[0].Len
= AsciiStrSize (Path
);
912 WriteRequest
[1].Data
= (VOID
*) Token
;
913 WriteRequest
[1].Len
= AsciiStrSize (Token
);
915 return XenStoreTalkv (XST_NIL
, XS_WATCH
, WriteRequest
, 2, NULL
, NULL
);
919 Transmit an uwatch request to the XenStore service.
921 @param Path The path in the XenStore to watch.
922 @param Tocken A unique identifier for this watch.
924 @return XENSTORE_STATUS_SUCCESS on success. Otherwise an errno indicating
925 the cause of failure.
934 WRITE_REQUEST WriteRequest
[2];
936 WriteRequest
[0].Data
= (VOID
*) Path
;
937 WriteRequest
[0].Len
= AsciiStrSize (Path
);
938 WriteRequest
[1].Data
= (VOID
*) Token
;
939 WriteRequest
[1].Len
= AsciiStrSize (Token
);
941 return XenStoreTalkv (XST_NIL
, XS_UNWATCH
, WriteRequest
, 2, NULL
, NULL
);
950 XENSTORE_MESSAGE
*Message
;
951 LIST_ENTRY
*Entry
= NULL
;
952 LIST_ENTRY
*Last
= NULL
;
953 XENSTORE_STATUS Status
;
956 EfiAcquireLock (&xs
.WatchEventsLock
);
957 if (IsListEmpty (&xs
.WatchEvents
) ||
958 Last
== GetFirstNode (&xs
.WatchEvents
)) {
959 EfiReleaseLock (&xs
.WatchEventsLock
);
960 Status
= XenStoreProcessMessage ();
961 if (Status
!= XENSTORE_STATUS_SUCCESS
&& Status
!= XENSTORE_STATUS_EAGAIN
) {
967 for (Entry
= GetFirstNode (&xs
.WatchEvents
);
968 Entry
!= Last
&& !IsNull (&xs
.WatchEvents
, Entry
);
969 Entry
= GetNextNode (&xs
.WatchEvents
, Entry
)) {
970 Message
= XENSTORE_MESSAGE_FROM_LINK (Entry
);
971 if (Message
->u
.Watch
.Handle
== Token
) {
972 RemoveEntryList (Entry
);
973 EfiReleaseLock (&xs
.WatchEventsLock
);
974 FreePool(Message
->u
.Watch
.Vector
);
976 return XENSTORE_STATUS_SUCCESS
;
979 Last
= GetFirstNode (&xs
.WatchEvents
);
980 EfiReleaseLock (&xs
.WatchEventsLock
);
986 NotifyEventChannelCheckForEvent (
991 XENSTORE_PRIVATE
*xs
;
992 xs
= (XENSTORE_PRIVATE
*)Context
;
993 if (TestAndClearBit (xs
->EventChannel
, xs
->Dev
->SharedInfo
->evtchn_pending
)) {
994 gBS
->SignalEvent (Event
);
999 Setup communication channels with the XenStore service.
1001 @retval EFI_SUCCESS if everything went well.
1006 XENSTORE_PRIVATE
*xs
1010 EFI_EVENT TimerEvent
;
1011 struct xenstore_domain_interface
*XenStore
= xs
->XenStore
;
1013 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
1014 Status
= gBS
->SetTimer (TimerEvent
, TimerRelative
,
1015 EFI_TIMER_PERIOD_SECONDS (5));
1016 while (XenStore
->rsp_prod
!= XenStore
->rsp_cons
) {
1017 Status
= gBS
->CheckEvent (TimerEvent
);
1018 if (!EFI_ERROR (Status
)) {
1019 DEBUG ((EFI_D_WARN
, "XENSTORE response ring is not quiescent "
1020 "(%08x:%08x): fixing up\n",
1021 XenStore
->rsp_cons
, XenStore
->rsp_prod
));
1022 XenStore
->rsp_cons
= XenStore
->rsp_prod
;
1025 gBS
->CloseEvent (TimerEvent
);
1027 Status
= gBS
->CreateEvent (EVT_NOTIFY_WAIT
, TPL_NOTIFY
,
1028 NotifyEventChannelCheckForEvent
, xs
,
1029 &xs
->EventChannelEvent
);
1030 ASSERT_EFI_ERROR (Status
);
1036 Initialize XenStore.
1038 @param Dev A XENBUS_DEVICE instance.
1040 @retval EFI_SUCCESS if everything went well.
1049 * The HVM guest pseudo-physical frame number. This is Xen's mapping
1050 * of the true machine frame number into our "physical address space".
1056 xs
.EventChannel
= XenHypercallHvmGetParam (Dev
, HVM_PARAM_STORE_EVTCHN
);
1057 XenStoreGpfn
= XenHypercallHvmGetParam (Dev
, HVM_PARAM_STORE_PFN
);
1058 xs
.XenStore
= (VOID
*) (XenStoreGpfn
<< EFI_PAGE_SHIFT
);
1059 DEBUG ((EFI_D_INFO
, "XenBusInit: XenBus rings @%p, event channel %x\n",
1060 xs
.XenStore
, xs
.EventChannel
));
1062 InitializeListHead (&xs
.ReplyList
);
1063 InitializeListHead (&xs
.WatchEvents
);
1064 InitializeListHead (&xs
.RegisteredWatches
);
1066 EfiInitializeLock (&xs
.ReplyLock
, TPL_NOTIFY
);
1067 EfiInitializeLock (&xs
.RegisteredWatchesLock
, TPL_NOTIFY
);
1068 EfiInitializeLock (&xs
.WatchEventsLock
, TPL_NOTIFY
);
1070 /* Initialize the shared memory rings to talk to xenstored */
1071 Status
= XenStoreInitComms (&xs
);
1072 if (EFI_ERROR (Status
)) {
1081 IN XENBUS_DEVICE
*Dev
1085 // Emptying the list RegisteredWatches, but this list should already be
1086 // empty. Every driver that is using Watches should unregister them when
1089 if (!IsListEmpty (&xs
.RegisteredWatches
)) {
1090 XENSTORE_WATCH
*Watch
;
1092 DEBUG ((EFI_D_WARN
, "XenStore: RegisteredWatches is not empty, cleaning up..."));
1093 Entry
= GetFirstNode (&xs
.RegisteredWatches
);
1094 while (!IsNull (&xs
.RegisteredWatches
, Entry
)) {
1095 Watch
= XENSTORE_WATCH_FROM_LINK (Entry
);
1096 Entry
= GetNextNode (&xs
.RegisteredWatches
, Entry
);
1098 XenStoreUnregisterWatch (Watch
);
1103 // Emptying the list WatchEvents, but this list should already be empty after
1104 // having cleanup the list RegisteredWatches.
1106 if (!IsListEmpty (&xs
.WatchEvents
)) {
1108 DEBUG ((EFI_D_WARN
, "XenStore: WatchEvents is not empty, cleaning up..."));
1109 Entry
= GetFirstNode (&xs
.WatchEvents
);
1110 while (!IsNull (&xs
.WatchEvents
, Entry
)) {
1111 XENSTORE_MESSAGE
*Message
= XENSTORE_MESSAGE_FROM_LINK (Entry
);
1112 Entry
= GetNextNode (&xs
.WatchEvents
, Entry
);
1113 RemoveEntryList (&Message
->Link
);
1114 FreePool (Message
->u
.Watch
.Vector
);
1119 if (!IsListEmpty (&xs
.ReplyList
)) {
1120 XENSTORE_MESSAGE
*Message
;
1122 Entry
= GetFirstNode (&xs
.ReplyList
);
1123 while (!IsNull (&xs
.ReplyList
, Entry
)) {
1124 Message
= XENSTORE_MESSAGE_FROM_LINK (Entry
);
1125 Entry
= GetNextNode (&xs
.ReplyList
, Entry
);
1126 RemoveEntryList (&Message
->Link
);
1127 FreePool (Message
->u
.Reply
.Body
);
1132 gBS
->CloseEvent (xs
.EventChannelEvent
);
1134 if (xs
.XenStore
->server_features
& XENSTORE_SERVER_FEATURE_RECONNECTION
) {
1135 xs
.XenStore
->connection
= XENSTORE_RECONNECT
;
1136 XenEventChannelNotify (xs
.Dev
, xs
.EventChannel
);
1137 while (*(volatile UINT32
*)&xs
.XenStore
->connection
== XENSTORE_RECONNECT
) {
1138 XenStoreWaitForEvent (xs
.EventChannelEvent
, EFI_TIMER_PERIOD_MILLISECONDS (100));
1141 /* If the backend reads the state while we're erasing it then the
1142 * ring state will become corrupted, preventing guest frontends from
1143 * connecting. This is rare. To help diagnose the failure, we fill
1144 * the ring with XS_INVALID packets. */
1145 SetMem (xs
.XenStore
->req
, XENSTORE_RING_SIZE
, 0xff);
1146 SetMem (xs
.XenStore
->rsp
, XENSTORE_RING_SIZE
, 0xff);
1147 xs
.XenStore
->req_cons
= xs
.XenStore
->req_prod
= 0;
1148 xs
.XenStore
->rsp_cons
= xs
.XenStore
->rsp_prod
= 0;
1155 // API comments for these methods can be found in XenStore.h
1159 XenStoreListDirectory (
1160 IN XENSTORE_TRANSACTION Transaction
,
1161 IN CONST CHAR8
*DirectoryPath
,
1162 IN CONST CHAR8
*Node
,
1163 OUT UINT32
*DirectoryCountPtr
,
1164 OUT CONST CHAR8
***DirectoryListPtr
1170 XENSTORE_STATUS Status
;
1172 Path
= XenStoreJoin (DirectoryPath
, Node
);
1173 Status
= XenStoreSingle (Transaction
, XS_DIRECTORY
, Path
, &Len
,
1174 (VOID
**) &TempStr
);
1176 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
1180 *DirectoryListPtr
= Split (TempStr
, Len
, DirectoryCountPtr
);
1182 return XENSTORE_STATUS_SUCCESS
;
1186 XenStorePathExists (
1187 IN XENSTORE_TRANSACTION Transaction
,
1188 IN CONST CHAR8
*Directory
,
1189 IN CONST CHAR8
*Node
1192 CONST CHAR8
**TempStr
;
1193 XENSTORE_STATUS Status
;
1196 Status
= XenStoreListDirectory (Transaction
, Directory
, Node
,
1197 &TempNum
, &TempStr
);
1198 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
1207 IN XENSTORE_TRANSACTION Transaction
,
1208 IN CONST CHAR8
*DirectoryPath
,
1209 IN CONST CHAR8
*Node
,
1210 OUT UINT32
*LenPtr OPTIONAL
,
1216 XENSTORE_STATUS Status
;
1218 Path
= XenStoreJoin (DirectoryPath
, Node
);
1219 Status
= XenStoreSingle (Transaction
, XS_READ
, Path
, LenPtr
, &Value
);
1221 if (Status
!= XENSTORE_STATUS_SUCCESS
) {
1226 return XENSTORE_STATUS_SUCCESS
;
1231 IN XENSTORE_TRANSACTION Transaction
,
1232 IN CONST CHAR8
*DirectoryPath
,
1233 IN CONST CHAR8
*Node
,
1238 WRITE_REQUEST WriteRequest
[2];
1239 XENSTORE_STATUS Status
;
1241 Path
= XenStoreJoin (DirectoryPath
, Node
);
1243 WriteRequest
[0].Data
= (VOID
*) Path
;
1244 WriteRequest
[0].Len
= AsciiStrSize (Path
);
1245 WriteRequest
[1].Data
= (VOID
*) Str
;
1246 WriteRequest
[1].Len
= AsciiStrLen (Str
);
1248 Status
= XenStoreTalkv (Transaction
, XS_WRITE
, WriteRequest
, 2, NULL
, NULL
);
1256 IN XENSTORE_TRANSACTION Transaction
,
1257 IN CONST CHAR8
*DirectoryPath
,
1258 IN CONST CHAR8
*Node
1262 XENSTORE_STATUS Status
;
1264 Path
= XenStoreJoin (DirectoryPath
, Node
);
1265 Status
= XenStoreSingle (Transaction
, XS_RM
, Path
, NULL
, NULL
);
1272 XenStoreTransactionStart (
1273 OUT XENSTORE_TRANSACTION
*Transaction
1277 XENSTORE_STATUS Status
;
1279 Status
= XenStoreSingle (XST_NIL
, XS_TRANSACTION_START
, "", NULL
,
1281 if (Status
== XENSTORE_STATUS_SUCCESS
) {
1282 Transaction
->Id
= AsciiStrDecimalToUintn (IdStr
);
1290 XenStoreTransactionEnd (
1291 IN XENSTORE_TRANSACTION Transaction
,
1298 AsciiStrCpy (AbortStr
, "F");
1300 AsciiStrCpy (AbortStr
, "T");
1303 return XenStoreSingle (Transaction
, XS_TRANSACTION_END
, AbortStr
, NULL
, NULL
);
1308 IN XENSTORE_TRANSACTION Transaction
,
1309 IN CONST CHAR8
*DirectoryPath
,
1310 IN CONST CHAR8
*Node
,
1311 IN CONST CHAR8
*FormatString
,
1316 XENSTORE_STATUS Status
;
1319 BufSize
= SPrintLengthAsciiFormat (FormatString
, Marker
) + 1;
1320 Buf
= AllocateZeroPool (BufSize
);
1321 AsciiVSPrint (Buf
, BufSize
, FormatString
, Marker
);
1322 Status
= XenStoreWrite (Transaction
, DirectoryPath
, Node
, Buf
);
1331 IN XENSTORE_TRANSACTION Transaction
,
1332 IN CONST CHAR8
*DirectoryPath
,
1333 IN CONST CHAR8
*Node
,
1334 IN CONST CHAR8
*FormatString
,
1339 XENSTORE_STATUS Status
;
1341 VA_START (Marker
, FormatString
);
1342 Status
= XenStoreVSPrint (Transaction
, DirectoryPath
, Node
, FormatString
, Marker
);
1349 XenStoreRegisterWatch (
1350 IN CONST CHAR8
*DirectoryPath
,
1351 IN CONST CHAR8
*Node
,
1352 OUT XENSTORE_WATCH
**WatchPtr
1355 /* Pointer in ascii is the token. */
1356 CHAR8 Token
[sizeof (XENSTORE_WATCH
) * 2 + 1];
1357 XENSTORE_STATUS Status
;
1358 XENSTORE_WATCH
*Watch
;
1360 Watch
= AllocateZeroPool (sizeof (XENSTORE_WATCH
));
1361 Watch
->Signature
= XENSTORE_WATCH_SIGNATURE
;
1362 Watch
->Node
= XenStoreJoin (DirectoryPath
, Node
);
1364 EfiAcquireLock (&xs
.RegisteredWatchesLock
);
1365 InsertTailList (&xs
.RegisteredWatches
, &Watch
->Link
);
1366 EfiReleaseLock (&xs
.RegisteredWatchesLock
);
1368 AsciiSPrint (Token
, sizeof (Token
), "%p", (VOID
*) Watch
);
1369 Status
= XenStoreWatch (Watch
->Node
, Token
);
1371 /* Ignore errors due to multiple registration. */
1372 if (Status
== XENSTORE_STATUS_EEXIST
) {
1373 Status
= XENSTORE_STATUS_SUCCESS
;
1376 if (Status
== XENSTORE_STATUS_SUCCESS
) {
1379 EfiAcquireLock (&xs
.RegisteredWatchesLock
);
1380 RemoveEntryList (&Watch
->Link
);
1381 EfiReleaseLock (&xs
.RegisteredWatchesLock
);
1382 FreePool (Watch
->Node
);
1390 XenStoreUnregisterWatch (
1391 IN XENSTORE_WATCH
*Watch
1394 CHAR8 Token
[sizeof (Watch
) * 2 + 1];
1397 ASSERT (Watch
->Signature
== XENSTORE_WATCH_SIGNATURE
);
1399 AsciiSPrint (Token
, sizeof (Token
), "%p", (VOID
*) Watch
);
1400 if (XenStoreFindWatch (Token
) == NULL
) {
1404 EfiAcquireLock (&xs
.RegisteredWatchesLock
);
1405 RemoveEntryList (&Watch
->Link
);
1406 EfiReleaseLock (&xs
.RegisteredWatchesLock
);
1408 XenStoreUnwatch (Watch
->Node
, Token
);
1410 /* Cancel pending watch events. */
1411 EfiAcquireLock (&xs
.WatchEventsLock
);
1412 Entry
= GetFirstNode (&xs
.WatchEvents
);
1413 while (!IsNull (&xs
.WatchEvents
, Entry
)) {
1414 XENSTORE_MESSAGE
*Message
= XENSTORE_MESSAGE_FROM_LINK (Entry
);
1415 Entry
= GetNextNode (&xs
.WatchEvents
, Entry
);
1416 if (Message
->u
.Watch
.Handle
== Watch
) {
1417 RemoveEntryList (&Message
->Link
);
1418 FreePool (Message
->u
.Watch
.Vector
);
1422 EfiReleaseLock (&xs
.WatchEventsLock
);
1424 FreePool (Watch
->Node
);
1435 XenBusWaitForWatch (
1436 IN XENBUS_PROTOCOL
*This
,
1440 return XenStoreWaitWatch (Token
);
1445 XenBusXenStoreRead (
1446 IN XENBUS_PROTOCOL
*This
,
1447 IN XENSTORE_TRANSACTION Transaction
,
1448 IN CONST CHAR8
*Node
,
1452 return XenStoreRead (Transaction
, This
->Node
, Node
, NULL
, Value
);
1457 XenBusXenStoreBackendRead (
1458 IN XENBUS_PROTOCOL
*This
,
1459 IN XENSTORE_TRANSACTION Transaction
,
1460 IN CONST CHAR8
*Node
,
1464 return XenStoreRead (Transaction
, This
->Backend
, Node
, NULL
, Value
);
1469 XenBusXenStoreRemove (
1470 IN XENBUS_PROTOCOL
*This
,
1471 IN XENSTORE_TRANSACTION Transaction
,
1475 return XenStoreRemove (Transaction
, This
->Node
, Node
);
1480 XenBusXenStoreTransactionStart (
1481 IN XENBUS_PROTOCOL
*This
,
1482 OUT XENSTORE_TRANSACTION
*Transaction
1485 return XenStoreTransactionStart (Transaction
);
1490 XenBusXenStoreTransactionEnd (
1491 IN XENBUS_PROTOCOL
*This
,
1492 IN XENSTORE_TRANSACTION Transaction
,
1496 return XenStoreTransactionEnd (Transaction
, Abort
);
1501 XenBusXenStoreSPrint (
1502 IN XENBUS_PROTOCOL
*This
,
1503 IN XENSTORE_TRANSACTION Transaction
,
1504 IN CONST CHAR8
*DirectoryPath
,
1505 IN CONST CHAR8
*Node
,
1506 IN CONST CHAR8
*FormatString
,
1511 XENSTORE_STATUS Status
;
1513 VA_START (Marker
, FormatString
);
1514 Status
= XenStoreVSPrint (Transaction
, DirectoryPath
, Node
, FormatString
, Marker
);
1522 XenBusRegisterWatch (
1523 IN XENBUS_PROTOCOL
*This
,
1524 IN CONST CHAR8
*Node
,
1528 return XenStoreRegisterWatch (This
->Node
, Node
, (XENSTORE_WATCH
**) Token
);
1533 XenBusRegisterWatchBackend (
1534 IN XENBUS_PROTOCOL
*This
,
1535 IN CONST CHAR8
*Node
,
1539 return XenStoreRegisterWatch (This
->Backend
, Node
, (XENSTORE_WATCH
**) Token
);
1544 XenBusUnregisterWatch (
1545 IN XENBUS_PROTOCOL
*This
,
1549 XenStoreUnregisterWatch ((XENSTORE_WATCH
*) Token
);