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     DECLARE_HYPERCALL_BOUNCE(buffer, XC_PAGE_SIZE,
30                              XC_HYPERCALL_BUFFER_BOUNCE_IN);
31     int rc;
32 
33     memset(&mpo, 0, sizeof(mpo));
34 
35     mpo.op      = op;
36     mpo.domain  = domain_id;
37     mpo.gfn     = gfn;
38 
39     if ( buffer )
40     {
41         if ( xc_hypercall_bounce_pre(xch, buffer) )
42         {
43             PERROR("Could not bounce memory for XENMEM_paging_op %u", op);
44             return -1;
45         }
46 
47         set_xen_guest_handle(mpo.buffer, buffer);
48     }
49 
50     rc = do_memory_op(xch, XENMEM_paging_op, &mpo, sizeof(mpo));
51 
52     if ( buffer )
53         xc_hypercall_bounce_post(xch, buffer);
54 
55     return rc;
56 }
57 
xc_mem_paging_enable(xc_interface * xch,uint32_t domain_id,uint32_t * port)58 int xc_mem_paging_enable(xc_interface *xch, uint32_t domain_id,
59                          uint32_t *port)
60 {
61     if ( !port )
62     {
63         errno = EINVAL;
64         return -1;
65     }
66 
67     return xc_vm_event_control(xch, domain_id,
68                                XEN_VM_EVENT_ENABLE,
69                                XEN_DOMCTL_VM_EVENT_OP_PAGING,
70                                port);
71 }
72 
xc_mem_paging_disable(xc_interface * xch,uint32_t domain_id)73 int xc_mem_paging_disable(xc_interface *xch, uint32_t domain_id)
74 {
75     return xc_vm_event_control(xch, domain_id,
76                                XEN_VM_EVENT_DISABLE,
77                                XEN_DOMCTL_VM_EVENT_OP_PAGING,
78                                NULL);
79 }
80 
xc_mem_paging_resume(xc_interface * xch,uint32_t domain_id)81 int xc_mem_paging_resume(xc_interface *xch, uint32_t domain_id)
82 {
83     return xc_vm_event_control(xch, domain_id,
84                                XEN_VM_EVENT_RESUME,
85                                XEN_DOMCTL_VM_EVENT_OP_PAGING,
86                                NULL);
87 }
88 
xc_mem_paging_nominate(xc_interface * xch,uint32_t domain_id,uint64_t gfn)89 int xc_mem_paging_nominate(xc_interface *xch, uint32_t domain_id, uint64_t gfn)
90 {
91     return xc_mem_paging_memop(xch, domain_id,
92                                XENMEM_paging_op_nominate,
93                                gfn, NULL);
94 }
95 
xc_mem_paging_evict(xc_interface * xch,uint32_t domain_id,uint64_t gfn)96 int xc_mem_paging_evict(xc_interface *xch, uint32_t domain_id, uint64_t gfn)
97 {
98     return xc_mem_paging_memop(xch, domain_id,
99                                XENMEM_paging_op_evict,
100                                gfn, NULL);
101 }
102 
xc_mem_paging_prep(xc_interface * xch,uint32_t domain_id,uint64_t gfn)103 int xc_mem_paging_prep(xc_interface *xch, uint32_t domain_id, uint64_t gfn)
104 {
105     return xc_mem_paging_memop(xch, domain_id,
106                                XENMEM_paging_op_prep,
107                                gfn, NULL);
108 }
109 
xc_mem_paging_load(xc_interface * xch,uint32_t domain_id,uint64_t gfn,void * buffer)110 int xc_mem_paging_load(xc_interface *xch, uint32_t domain_id,
111                        uint64_t gfn, void *buffer)
112 {
113     errno = EINVAL;
114 
115     if ( !buffer )
116         return -1;
117 
118     return xc_mem_paging_memop(xch, domain_id, XENMEM_paging_op_prep,
119                                gfn, buffer);
120 }
121 
122 
123 /*
124  * Local variables:
125  * mode: C
126  * c-file-style: "BSD"
127  * c-basic-offset: 4
128  * indent-tabs-mode: nil
129  * End:
130  */
131