1 /******************************************************************************
2  * xc_vmtrace.c
3  *
4  * API for manipulating hardware tracing features
5  *
6  * Copyright (c) 2020, Michal Leszczynski
7  *
8  * Copyright 2020 CERT Polska. All rights reserved.
9  * Use is subject to license terms.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #include "xc_private.h"
26 
xc_vmtrace_enable(xc_interface * xch,uint32_t domid,uint32_t vcpu)27 int xc_vmtrace_enable(
28     xc_interface *xch, uint32_t domid, uint32_t vcpu)
29 {
30     struct xen_domctl domctl = {
31         .cmd = XEN_DOMCTL_vmtrace_op,
32         .domain = domid,
33         .u.vmtrace_op = {
34             .cmd = XEN_DOMCTL_vmtrace_enable,
35             .vcpu = vcpu,
36         },
37     };
38 
39     return do_domctl(xch, &domctl);
40 }
41 
xc_vmtrace_disable(xc_interface * xch,uint32_t domid,uint32_t vcpu)42 int xc_vmtrace_disable(
43     xc_interface *xch, uint32_t domid, uint32_t vcpu)
44 {
45     struct xen_domctl domctl = {
46         .cmd = XEN_DOMCTL_vmtrace_op,
47         .domain = domid,
48         .u.vmtrace_op = {
49             .cmd = XEN_DOMCTL_vmtrace_disable,
50             .vcpu = vcpu,
51         },
52     };
53 
54     return do_domctl(xch, &domctl);
55 }
56 
xc_vmtrace_reset_and_enable(xc_interface * xch,uint32_t domid,uint32_t vcpu)57 int xc_vmtrace_reset_and_enable(
58     xc_interface *xch, uint32_t domid, uint32_t vcpu)
59 {
60     struct xen_domctl domctl = {
61         .cmd = XEN_DOMCTL_vmtrace_op,
62         .domain = domid,
63         .u.vmtrace_op = {
64             .cmd = XEN_DOMCTL_vmtrace_reset_and_enable,
65             .vcpu = vcpu,
66         },
67     };
68 
69     return do_domctl(xch, &domctl);
70 }
71 
xc_vmtrace_output_position(xc_interface * xch,uint32_t domid,uint32_t vcpu,uint64_t * pos)72 int xc_vmtrace_output_position(
73     xc_interface *xch, uint32_t domid, uint32_t vcpu, uint64_t *pos)
74 {
75     struct xen_domctl domctl = {
76         .cmd = XEN_DOMCTL_vmtrace_op,
77         .domain = domid,
78         .u.vmtrace_op = {
79             .cmd = XEN_DOMCTL_vmtrace_output_position,
80             .vcpu = vcpu,
81         },
82     };
83     int rc = do_domctl(xch, &domctl);
84 
85     if ( !rc )
86         *pos = domctl.u.vmtrace_op.value;
87 
88     return rc;
89 }
90 
xc_vmtrace_get_option(xc_interface * xch,uint32_t domid,uint32_t vcpu,uint64_t key,uint64_t * value)91 int xc_vmtrace_get_option(
92     xc_interface *xch, uint32_t domid, uint32_t vcpu,
93     uint64_t key, uint64_t *value)
94 {
95     struct xen_domctl domctl = {
96         .cmd = XEN_DOMCTL_vmtrace_op,
97         .domain = domid,
98         .u.vmtrace_op = {
99             .cmd = XEN_DOMCTL_vmtrace_get_option,
100             .vcpu = vcpu,
101             .key = key,
102         },
103     };
104     int rc = do_domctl(xch, &domctl);
105 
106     if ( !rc )
107         *value = domctl.u.vmtrace_op.value;
108 
109     return rc;
110 }
111 
xc_vmtrace_set_option(xc_interface * xch,uint32_t domid,uint32_t vcpu,uint64_t key,uint64_t value)112 int xc_vmtrace_set_option(
113     xc_interface *xch, uint32_t domid, uint32_t vcpu,
114     uint64_t key, uint64_t value)
115 {
116     struct xen_domctl domctl = {
117         .cmd = XEN_DOMCTL_vmtrace_op,
118         .domain = domid,
119         .u.vmtrace_op = {
120             .cmd = XEN_DOMCTL_vmtrace_set_option,
121             .vcpu = vcpu,
122             .key = key,
123             .value = value,
124         },
125     };
126 
127     return do_domctl(xch, &domctl);
128 }
129