1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation;
5  * version 2.1 of the License.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #include <stdlib.h>
17 #include <assert.h>
18 #include <errno.h>
19 
20 #include "private.h"
21 
all_restrict_cb(Xentoolcore__Active_Handle * ah,domid_t domid)22 static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) {
23     xenforeignmemory_handle *fmem = CONTAINER_OF(ah, *fmem, tc_ah);
24 
25     if (fmem->fd < 0)
26         /* just in case */
27         return 0;
28 
29     return xenforeignmemory_restrict(fmem, domid);
30 }
31 
xenforeignmemory_open(xentoollog_logger * logger,unsigned open_flags)32 xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
33                                                unsigned open_flags)
34 {
35     xenforeignmemory_handle *fmem = malloc(sizeof(*fmem));
36     int rc;
37 
38     if (!fmem) return NULL;
39 
40     fmem->fd = -1;
41     fmem->logger = logger;
42     fmem->logger_tofree = NULL;
43 
44     fmem->tc_ah.restrict_callback = all_restrict_cb;
45     xentoolcore__register_active_handle(&fmem->tc_ah);
46 
47     if (!fmem->logger) {
48         fmem->logger = fmem->logger_tofree =
49             (xentoollog_logger*)
50             xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
51         if (!fmem->logger) goto err;
52     }
53 
54     rc = osdep_xenforeignmemory_open(fmem);
55     if ( rc  < 0 ) goto err;
56 
57     return fmem;
58 
59 err:
60     xentoolcore__deregister_active_handle(&fmem->tc_ah);
61     osdep_xenforeignmemory_close(fmem);
62     xtl_logger_destroy(fmem->logger_tofree);
63     free(fmem);
64     return NULL;
65 }
66 
xenforeignmemory_close(xenforeignmemory_handle * fmem)67 int xenforeignmemory_close(xenforeignmemory_handle *fmem)
68 {
69     int rc;
70 
71     if ( !fmem )
72         return 0;
73 
74     xentoolcore__deregister_active_handle(&fmem->tc_ah);
75     rc = osdep_xenforeignmemory_close(fmem);
76     xtl_logger_destroy(fmem->logger_tofree);
77     free(fmem);
78     return rc;
79 }
80 
xenforeignmemory_map2(xenforeignmemory_handle * fmem,uint32_t dom,void * addr,int prot,int flags,size_t num,const xen_pfn_t arr[],int err[])81 void *xenforeignmemory_map2(xenforeignmemory_handle *fmem,
82                             uint32_t dom, void *addr,
83                             int prot, int flags, size_t num,
84                             const xen_pfn_t arr[/*num*/], int err[/*num*/])
85 {
86     void *ret;
87     int *err_to_free = NULL;
88 
89     if ( err == NULL )
90         err = err_to_free = malloc(num * sizeof(int));
91 
92     if ( err == NULL )
93         return NULL;
94 
95     ret = osdep_xenforeignmemory_map(fmem, dom, addr, prot, flags, num, arr, err);
96 
97     if ( ret && err_to_free )
98     {
99         int i;
100 
101         for ( i = 0 ; i < num ; i++ )
102         {
103             if ( err[i] )
104             {
105                 errno = -err[i];
106                 (void)osdep_xenforeignmemory_unmap(fmem, ret, num);
107                 ret = NULL;
108                 break;
109             }
110         }
111     }
112 
113     free(err_to_free);
114 
115     return ret;
116 }
117 
xenforeignmemory_map(xenforeignmemory_handle * fmem,uint32_t dom,int prot,size_t num,const xen_pfn_t arr[],int err[])118 void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
119                            uint32_t dom, int prot,
120                            size_t num,
121                            const xen_pfn_t arr[/*num*/], int err[/*num*/])
122 {
123     return xenforeignmemory_map2(fmem, dom, NULL, prot, 0, num, arr, err);
124 }
125 
xenforeignmemory_unmap(xenforeignmemory_handle * fmem,void * addr,size_t num)126 int xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
127                            void *addr, size_t num)
128 {
129     return osdep_xenforeignmemory_unmap(fmem, addr, num);
130 }
131 
xenforeignmemory_restrict(xenforeignmemory_handle * fmem,domid_t domid)132 int xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
133                               domid_t domid)
134 {
135     return osdep_xenforeignmemory_restrict(fmem, domid);
136 }
137 
138 /*
139  * Local variables:
140  * mode: C
141  * c-file-style: "BSD"
142  * c-basic-offset: 4
143  * tab-width: 4
144  * indent-tabs-mode: nil
145  * End:
146  */
147