1 /******************************************************************************
2  *
3  * tools/libxc/xc_mem_paging.c
4  *
5  * Interface to low-level memory paging functionality.
6  *
7  * Copyright (c) 2009 by Citrix Systems, Inc. (Patrick Colp)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "xc_private.h"
24 
xc_mem_paging_memop(xc_interface * xch,uint32_t domain_id,unsigned int op,uint64_t gfn,void * buffer)25 static int xc_mem_paging_memop(xc_interface *xch, uint32_t domain_id,
26                                unsigned int op, uint64_t gfn, void *buffer)
27 {
28     xen_mem_paging_op_t mpo;
29 
30     memset(&mpo, 0, sizeof(mpo));
31 
32     mpo.op      = op;
33     mpo.domain  = domain_id;
34     mpo.gfn     = gfn;
35     mpo.buffer  = (unsigned long) buffer;
36 
37     return do_memory_op(xch, XENMEM_paging_op, &mpo, sizeof(mpo));
38 }
39 
xc_mem_paging_enable(xc_interface * xch,uint32_t domain_id,uint32_t * port)40 int xc_mem_paging_enable(xc_interface *xch, uint32_t domain_id,
41                          uint32_t *port)
42 {
43     if ( !port )
44     {
45         errno = EINVAL;
46         return -1;
47     }
48 
49     return xc_vm_event_control(xch, domain_id,
50                                XEN_VM_EVENT_ENABLE,
51                                XEN_DOMCTL_VM_EVENT_OP_PAGING,
52                                port);
53 }
54 
xc_mem_paging_disable(xc_interface * xch,uint32_t domain_id)55 int xc_mem_paging_disable(xc_interface *xch, uint32_t domain_id)
56 {
57     return xc_vm_event_control(xch, domain_id,
58                                XEN_VM_EVENT_DISABLE,
59                                XEN_DOMCTL_VM_EVENT_OP_PAGING,
60                                NULL);
61 }
62 
xc_mem_paging_resume(xc_interface * xch,uint32_t domain_id)63 int xc_mem_paging_resume(xc_interface *xch, uint32_t domain_id)
64 {
65     return xc_vm_event_control(xch, domain_id,
66                                XEN_VM_EVENT_RESUME,
67                                XEN_DOMCTL_VM_EVENT_OP_PAGING,
68                                NULL);
69 }
70 
xc_mem_paging_nominate(xc_interface * xch,uint32_t domain_id,uint64_t gfn)71 int xc_mem_paging_nominate(xc_interface *xch, uint32_t domain_id, uint64_t gfn)
72 {
73     return xc_mem_paging_memop(xch, domain_id,
74                                XENMEM_paging_op_nominate,
75                                gfn, NULL);
76 }
77 
xc_mem_paging_evict(xc_interface * xch,uint32_t domain_id,uint64_t gfn)78 int xc_mem_paging_evict(xc_interface *xch, uint32_t domain_id, uint64_t gfn)
79 {
80     return xc_mem_paging_memop(xch, domain_id,
81                                XENMEM_paging_op_evict,
82                                gfn, NULL);
83 }
84 
xc_mem_paging_prep(xc_interface * xch,uint32_t domain_id,uint64_t gfn)85 int xc_mem_paging_prep(xc_interface *xch, uint32_t domain_id, uint64_t gfn)
86 {
87     return xc_mem_paging_memop(xch, domain_id,
88                                XENMEM_paging_op_prep,
89                                gfn, NULL);
90 }
91 
xc_mem_paging_load(xc_interface * xch,uint32_t domain_id,uint64_t gfn,void * buffer)92 int xc_mem_paging_load(xc_interface *xch, uint32_t domain_id,
93                        uint64_t gfn, void *buffer)
94 {
95     int rc, old_errno;
96 
97     errno = EINVAL;
98 
99     if ( !buffer )
100         return -1;
101 
102     if ( ((unsigned long) buffer) & (XC_PAGE_SIZE - 1) )
103         return -1;
104 
105     if ( mlock(buffer, XC_PAGE_SIZE) )
106         return -1;
107 
108     rc = xc_mem_paging_memop(xch, domain_id,
109                              XENMEM_paging_op_prep,
110                              gfn, buffer);
111 
112     old_errno = errno;
113     munlock(buffer, XC_PAGE_SIZE);
114     errno = old_errno;
115 
116     return rc;
117 }
118 
119 
120 /*
121  * Local variables:
122  * mode: C
123  * c-file-style: "BSD"
124  * c-basic-offset: 4
125  * indent-tabs-mode: nil
126  * End:
127  */
128