2 Grant Table function implementation.
4 Grant Table are used to grant access to certain page of the current
7 Author: Steven Smith (sos22@cam.ac.uk)
8 Changes: Grzegorz Milos (gm281@cam.ac.uk)
9 Copyright (C) 2006, Cambridge University
10 Copyright (C) 2014, Citrix Ltd.
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
21 THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "XenBusDxe.h"
35 #include <IndustryStandard/Xen/memory.h>
37 #include <Library/XenHypercallLib.h>
39 #include "GrantTable.h"
40 #include "InterlockedCompareExchange16.h"
42 #define NR_RESERVED_ENTRIES 8
44 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
45 #define NR_GRANT_FRAMES 4
46 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * EFI_PAGE_SIZE / sizeof(grant_entry_v1_t))
48 STATIC grant_entry_v1_t
*GrantTable
= NULL
;
49 STATIC grant_ref_t GrantList
[NR_GRANT_ENTRIES
];
50 STATIC EFI_LOCK mGrantListLock
;
52 STATIC BOOLEAN GrantInUseList
[NR_GRANT_ENTRIES
];
57 XenGrantTablePutFreeEntry (
61 EfiAcquireLock (&mGrantListLock
);
63 ASSERT (GrantInUseList
[Ref
]);
64 GrantInUseList
[Ref
] = FALSE
;
66 GrantList
[Ref
] = GrantList
[0];
68 EfiReleaseLock (&mGrantListLock
);
73 XenGrantTableGetFreeEntry (
79 EfiAcquireLock (&mGrantListLock
);
81 ASSERT (Ref
>= NR_RESERVED_ENTRIES
&& Ref
< NR_GRANT_ENTRIES
);
82 GrantList
[0] = GrantList
[Ref
];
84 ASSERT (!GrantInUseList
[Ref
]);
85 GrantInUseList
[Ref
] = TRUE
;
87 EfiReleaseLock (&mGrantListLock
);
93 XenGrantTableGrantAccess (
102 ASSERT (GrantTable
!= NULL
);
103 Ref
= XenGrantTableGetFreeEntry ();
104 GrantTable
[Ref
].frame
= (UINT32
)Frame
;
105 GrantTable
[Ref
].domid
= DomainId
;
107 Flags
= GTF_permit_access
;
109 Flags
|= GTF_readonly
;
111 GrantTable
[Ref
].flags
= Flags
;
118 XenGrantTableEndAccess (
122 UINT16 Flags
, OldFlags
;
124 ASSERT (GrantTable
!= NULL
);
125 ASSERT (Ref
>= NR_RESERVED_ENTRIES
&& Ref
< NR_GRANT_ENTRIES
);
127 OldFlags
= GrantTable
[Ref
].flags
;
129 if ((Flags
= OldFlags
) & (GTF_reading
| GTF_writing
)) {
130 DEBUG ((EFI_D_WARN
, "WARNING: g.e. still in use! (%x)\n", Flags
));
131 return EFI_NOT_READY
;
133 OldFlags
= InterlockedCompareExchange16 (&GrantTable
[Ref
].flags
, Flags
, 0);
134 } while (OldFlags
!= Flags
);
136 XenGrantTablePutFreeEntry (Ref
);
142 IN XENBUS_DEVICE
*Dev
,
146 xen_add_to_physmap_t Parameters
;
151 SetMem(GrantInUseList
, sizeof (GrantInUseList
), 1);
153 EfiInitializeLock (&mGrantListLock
, TPL_NOTIFY
);
154 for (Index
= NR_RESERVED_ENTRIES
; Index
< NR_GRANT_ENTRIES
; Index
++) {
155 XenGrantTablePutFreeEntry ((grant_ref_t
)Index
);
158 GrantTable
= (VOID
*)(UINTN
) MmioAddr
;
159 for (Index
= 0; Index
< NR_GRANT_FRAMES
; Index
++) {
160 Parameters
.domid
= DOMID_SELF
;
161 Parameters
.idx
= Index
;
162 Parameters
.space
= XENMAPSPACE_grant_table
;
163 Parameters
.gpfn
= (xen_pfn_t
) ((UINTN
) GrantTable
>> EFI_PAGE_SHIFT
) + Index
;
164 ReturnCode
= XenHypercallMemoryOp (XENMEM_add_to_physmap
, &Parameters
);
165 if (ReturnCode
!= 0) {
166 DEBUG ((EFI_D_ERROR
, "Xen GrantTable, add_to_physmap hypercall error: %d\n", ReturnCode
));
172 XenGrantTableDeinit (
176 INTN ReturnCode
, Index
;
177 xen_remove_from_physmap_t Parameters
;
179 if (GrantTable
== NULL
) {
183 for (Index
= NR_GRANT_FRAMES
- 1; Index
>= 0; Index
--) {
184 Parameters
.domid
= DOMID_SELF
;
185 Parameters
.gpfn
= (xen_pfn_t
) ((UINTN
) GrantTable
>> EFI_PAGE_SHIFT
) + Index
;
186 DEBUG ((EFI_D_INFO
, "Xen GrantTable, removing %X\n", Parameters
.gpfn
));
187 ReturnCode
= XenHypercallMemoryOp (XENMEM_remove_from_physmap
, &Parameters
);
188 if (ReturnCode
!= 0) {
189 DEBUG ((EFI_D_ERROR
, "Xen GrantTable, remove_from_physmap hypercall error: %d\n", ReturnCode
));
198 IN XENBUS_PROTOCOL
*This
,
200 IN UINTN Frame
, // MFN
202 OUT grant_ref_t
*RefPtr
205 *RefPtr
= XenGrantTableGrantAccess (DomainId
, Frame
, ReadOnly
);
211 XenBusGrantEndAccess (
212 IN XENBUS_PROTOCOL
*This
,
216 return XenGrantTableEndAccess (Ref
);