mirror of
https://github.com/xen-project/xen.git
synced 2026-10-28 18:21:39 +01:00
Commit 8ef5f344d0 "tools/libxendevicemodel: add a call to restrict the
handle" added a function to the devicemodel interface to restrict
operations through the API to a specific domain, where a capable under-
lying privcmd driver exists.
This patch adds similar functionality to the xenforeignmemory API. This
will be necessary (as much as xendevicemodel restriction) for limiting
the scope of device models to specific domains.
NOTE: My patch to the linux kernel [1] added the appropriate checks to
the foreign memory ioctls.
[1] https://git.kernel.org/cgit/linux/kernel/git/ostr/linux.git/commit/?id=4610d240
Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
124 lines
2.9 KiB
C
124 lines
2.9 KiB
C
/*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation;
|
|
* version 2.1 of the License.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
|
|
#include "private.h"
|
|
|
|
xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
|
|
unsigned open_flags)
|
|
{
|
|
xenforeignmemory_handle *fmem = malloc(sizeof(*fmem));
|
|
int rc;
|
|
|
|
if (!fmem) return NULL;
|
|
|
|
fmem->fd = -1;
|
|
fmem->logger = logger;
|
|
fmem->logger_tofree = NULL;
|
|
|
|
if (!fmem->logger) {
|
|
fmem->logger = fmem->logger_tofree =
|
|
(xentoollog_logger*)
|
|
xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
|
|
if (!fmem->logger) goto err;
|
|
}
|
|
|
|
rc = osdep_xenforeignmemory_open(fmem);
|
|
if ( rc < 0 ) goto err;
|
|
|
|
return fmem;
|
|
|
|
err:
|
|
osdep_xenforeignmemory_close(fmem);
|
|
xtl_logger_destroy(fmem->logger_tofree);
|
|
free(fmem);
|
|
return NULL;
|
|
}
|
|
|
|
int xenforeignmemory_close(xenforeignmemory_handle *fmem)
|
|
{
|
|
int rc;
|
|
|
|
if ( !fmem )
|
|
return 0;
|
|
|
|
rc = osdep_xenforeignmemory_close(fmem);
|
|
xtl_logger_destroy(fmem->logger_tofree);
|
|
free(fmem);
|
|
return rc;
|
|
}
|
|
|
|
void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
|
|
uint32_t dom, int prot,
|
|
size_t num,
|
|
const xen_pfn_t arr[/*num*/], int err[/*num*/])
|
|
{
|
|
void *ret;
|
|
int *err_to_free = NULL;
|
|
|
|
if ( err == NULL )
|
|
err = err_to_free = malloc(num * sizeof(int));
|
|
|
|
if ( err == NULL )
|
|
return NULL;
|
|
|
|
ret = osdep_xenforeignmemory_map(fmem, dom, prot, num, arr, err);
|
|
|
|
if ( ret && err_to_free )
|
|
{
|
|
int i;
|
|
|
|
for ( i = 0 ; i < num ; i++ )
|
|
{
|
|
if ( err[i] )
|
|
{
|
|
errno = -err[i];
|
|
(void)osdep_xenforeignmemory_unmap(fmem, ret, num);
|
|
ret = NULL;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
free(err_to_free);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
|
|
void *addr, size_t num)
|
|
{
|
|
return osdep_xenforeignmemory_unmap(fmem, addr, num);
|
|
}
|
|
|
|
int xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
|
|
domid_t domid)
|
|
{
|
|
return osdep_xenforeignmemory_restrict(fmem, domid);
|
|
}
|
|
|
|
/*
|
|
* Local variables:
|
|
* mode: C
|
|
* c-file-style: "BSD"
|
|
* c-basic-offset: 4
|
|
* tab-width: 4
|
|
* indent-tabs-mode: nil
|
|
* End:
|
|
*/
|