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 
18 #include "private.h"
19 
all_restrict_cb(Xentoolcore__Active_Handle * ah,domid_t domid)20 static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) {
21     xencall_handle *xcall = CONTAINER_OF(ah, *xcall, tc_ah);
22     return xentoolcore__restrict_by_dup2_null(xcall->fd);
23 }
24 
xencall_open(xentoollog_logger * logger,unsigned open_flags)25 xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
26 {
27     xencall_handle *xcall = malloc(sizeof(*xcall));
28     int rc;
29 
30     if (!xcall) return NULL;
31 
32     xcall->fd = -1;
33     xcall->tc_ah.restrict_callback = all_restrict_cb;
34     xentoolcore__register_active_handle(&xcall->tc_ah);
35 
36     xcall->flags = open_flags;
37     xcall->buffer_cache_nr = 0;
38 
39     xcall->buffer_total_allocations = 0;
40     xcall->buffer_total_releases = 0;
41     xcall->buffer_current_allocations = 0;
42     xcall->buffer_maximum_allocations = 0;
43     xcall->buffer_cache_hits = 0;
44     xcall->buffer_cache_misses = 0;
45     xcall->buffer_cache_toobig = 0;
46     xcall->logger = logger;
47     xcall->logger_tofree = NULL;
48 
49     if (!xcall->logger) {
50         xcall->logger = xcall->logger_tofree =
51             (xentoollog_logger*)
52             xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
53         if (!xcall->logger) goto err;
54     }
55 
56     rc = osdep_xencall_open(xcall);
57     if ( rc  < 0 ) goto err;
58 
59     return xcall;
60 
61 err:
62     xentoolcore__deregister_active_handle(&xcall->tc_ah);
63     osdep_xencall_close(xcall);
64     xtl_logger_destroy(xcall->logger_tofree);
65     free(xcall);
66     return NULL;
67 }
68 
xencall_close(xencall_handle * xcall)69 int xencall_close(xencall_handle *xcall)
70 {
71     int rc;
72 
73     if ( !xcall )
74         return 0;
75 
76     xentoolcore__deregister_active_handle(&xcall->tc_ah);
77     rc = osdep_xencall_close(xcall);
78     buffer_release_cache(xcall);
79     xtl_logger_destroy(xcall->logger_tofree);
80     free(xcall);
81     return rc;
82 }
83 
xencall0(xencall_handle * xcall,unsigned int op)84 int xencall0(xencall_handle *xcall, unsigned int op)
85 {
86     privcmd_hypercall_t call = {
87         .op = op,
88     };
89 
90     return osdep_hypercall(xcall, &call);
91 }
92 
xencall1(xencall_handle * xcall,unsigned int op,uint64_t arg1)93 int xencall1(xencall_handle *xcall, unsigned int op,
94              uint64_t arg1)
95 {
96     privcmd_hypercall_t call = {
97         .op = op,
98         .arg = { arg1 },
99     };
100 
101     return osdep_hypercall(xcall, &call);
102 }
103 
xencall2(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2)104 int xencall2(xencall_handle *xcall, unsigned int op,
105              uint64_t arg1, uint64_t arg2)
106 {
107     privcmd_hypercall_t call = {
108         .op = op,
109         .arg = { arg1, arg2 },
110     };
111 
112     return osdep_hypercall(xcall, &call);
113 }
114 
xencall3(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2,uint64_t arg3)115 int xencall3(xencall_handle *xcall, unsigned int op,
116              uint64_t arg1, uint64_t arg2, uint64_t arg3)
117 {
118     privcmd_hypercall_t call = {
119         .op = op,
120         .arg = { arg1, arg2, arg3},
121     };
122 
123     return osdep_hypercall(xcall, &call);
124 }
125 
xencall4(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4)126 int xencall4(xencall_handle *xcall, unsigned int op,
127              uint64_t arg1, uint64_t arg2, uint64_t arg3,
128              uint64_t arg4)
129 {
130     privcmd_hypercall_t call = {
131         .op = op,
132         .arg = { arg1, arg2, arg3, arg4 },
133     };
134 
135     return osdep_hypercall(xcall, &call);
136 }
137 
xencall5(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5)138 int xencall5(xencall_handle *xcall, unsigned int op,
139              uint64_t arg1, uint64_t arg2, uint64_t arg3,
140              uint64_t arg4, uint64_t arg5)
141 {
142     privcmd_hypercall_t call = {
143         .op = op,
144         .arg = { arg1, arg2, arg3, arg4, arg5 },
145     };
146 
147     return osdep_hypercall(xcall, &call);
148 }
149 
150 /*
151  * Local variables:
152  * mode: C
153  * c-file-style: "BSD"
154  * c-basic-offset: 4
155  * tab-width: 4
156  * indent-tabs-mode: nil
157  * End:
158  */
159