16#include <IndustryStandard/Xen/memory.h>
23#define NR_RESERVED_ENTRIES 8
25#define NR_GRANT_FRAMES (FixedPcdGet32 (PcdXenGrantFrames))
26#define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * EFI_PAGE_SIZE / sizeof(grant_entry_v1_t))
29STATIC grant_ref_t GrantList[NR_GRANT_ENTRIES];
32STATIC BOOLEAN GrantInUseList[NR_GRANT_ENTRIES];
37XenGrantTablePutFreeEntry (
43 ASSERT (GrantInUseList[Ref]);
44 GrantInUseList[Ref] =
FALSE;
46 GrantList[Ref] = GrantList[0];
53XenGrantTableGetFreeEntry (
61 ASSERT (Ref >= NR_RESERVED_ENTRIES && Ref < NR_GRANT_ENTRIES);
62 GrantList[0] = GrantList[Ref];
64 ASSERT (!GrantInUseList[Ref]);
65 GrantInUseList[Ref] =
TRUE;
73XenGrantTableGrantAccess (
82 ASSERT (GrantTable !=
NULL);
83 Ref = XenGrantTableGetFreeEntry ();
84 GrantTable[Ref].frame = (UINT32)Frame;
85 GrantTable[Ref].domid = DomainId;
87 Flags = GTF_permit_access;
89 Flags |= GTF_readonly;
92 GrantTable[Ref].flags = Flags;
99XenGrantTableEndAccess (
103 UINT16 Flags, OldFlags;
105 ASSERT (GrantTable !=
NULL);
106 ASSERT (Ref >= NR_RESERVED_ENTRIES && Ref < NR_GRANT_ENTRIES);
108 OldFlags = GrantTable[Ref].flags;
110 if ((Flags = OldFlags) & (GTF_reading | GTF_writing)) {
111 DEBUG ((DEBUG_WARN,
"WARNING: g.e. still in use! (%x)\n", Flags));
112 return EFI_NOT_READY;
116 }
while (OldFlags != Flags);
118 XenGrantTablePutFreeEntry (Ref);
132 SetMem (GrantInUseList,
sizeof (GrantInUseList), 1);
135 for (Index = NR_RESERVED_ENTRIES; Index < NR_GRANT_ENTRIES; Index++) {
136 XenGrantTablePutFreeEntry ((grant_ref_t)Index);
139 GrantTable = (VOID *)(
UINTN)Dev->XenIo->GrantTableAddress;
140 for (Index = 0; Index < NR_GRANT_FRAMES; Index++) {
141 Parameters.domid = DOMID_SELF;
142 Parameters.idx = Index;
143 Parameters.space = XENMAPSPACE_grant_table;
144 Parameters.gpfn = (xen_pfn_t)((
UINTN)GrantTable >> EFI_PAGE_SHIFT) + Index;
146 if (ReturnCode != 0) {
149 "Xen GrantTable, add_to_physmap hypercall error: %Ld\n",
161 INTN ReturnCode, Index;
164 if (GrantTable ==
NULL) {
168 for (Index = NR_GRANT_FRAMES - 1; Index >= 0; Index--) {
169 Parameters.domid = DOMID_SELF;
170 Parameters.gpfn = (xen_pfn_t)((
UINTN)GrantTable >> EFI_PAGE_SHIFT) + Index;
173 "Xen GrantTable, removing %Lx\n",
174 (UINT64)Parameters.gpfn
177 if (ReturnCode != 0) {
180 "Xen GrantTable, remove_from_physmap hypercall error: %Ld\n",
196 OUT grant_ref_t *RefPtr
199 *RefPtr = XenGrantTableGrantAccess (DomainId, Frame, ReadOnly);
210 return XenGrantTableEndAccess (Ref);
VOID EFIAPI MemoryFence(VOID)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
EFI_STATUS EFIAPI XenBusGrantEndAccess(IN XENBUS_PROTOCOL *This, IN grant_ref_t Ref)
EFI_STATUS EFIAPI XenBusGrantAccess(IN XENBUS_PROTOCOL *This, IN domid_t DomainId, IN UINTN Frame, IN BOOLEAN ReadOnly, OUT grant_ref_t *RefPtr)
VOID XenGrantTableInit(IN XENBUS_DEVICE *Dev)
#define DEBUG(Expression)
UINT16 EFIAPI InterlockedCompareExchange16(IN OUT volatile UINT16 *Value, IN UINT16 CompareValue, IN UINT16 ExchangeValue)
VOID EFIAPI EfiReleaseLock(IN EFI_LOCK *Lock)
VOID EFIAPI EfiAcquireLock(IN EFI_LOCK *Lock)
EFI_LOCK *EFIAPI EfiInitializeLock(IN OUT EFI_LOCK *Lock, IN EFI_TPL Priority)
INTN EFIAPI XenHypercallMemoryOp(IN UINTN Operation, IN OUT VOID *Arguments)