1 /******************************************************************************
2  * xc_tbuf.c
3  *
4  * API for manipulating and accessing trace buffer parameters
5  *
6  * Copyright (c) 2005, Rob Gardner
7  *
8  * Copyright 2006 Sun Microsystems, Inc.  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 #include <xen/trace.h>
27 
tbuf_enable(xc_interface * xch,int enable)28 static int tbuf_enable(xc_interface *xch, int enable)
29 {
30     DECLARE_SYSCTL;
31 
32     sysctl.cmd = XEN_SYSCTL_tbuf_op;
33     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
34     if ( enable )
35         sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_enable;
36     else
37         sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_disable;
38 
39     return xc_sysctl(xch, &sysctl);
40 }
41 
xc_tbuf_set_size(xc_interface * xch,unsigned long size)42 int xc_tbuf_set_size(xc_interface *xch, unsigned long size)
43 {
44     DECLARE_SYSCTL;
45 
46     sysctl.cmd = XEN_SYSCTL_tbuf_op;
47     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
48     sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_set_size;
49     sysctl.u.tbuf_op.size = size;
50 
51     return xc_sysctl(xch, &sysctl);
52 }
53 
xc_tbuf_get_size(xc_interface * xch,unsigned long * size)54 int xc_tbuf_get_size(xc_interface *xch, unsigned long *size)
55 {
56     struct t_info *t_info;
57     int rc;
58     DECLARE_SYSCTL;
59 
60     sysctl.cmd = XEN_SYSCTL_tbuf_op;
61     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
62     sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_get_info;
63 
64     rc = xc_sysctl(xch, &sysctl);
65     if ( rc != 0 )
66         return rc;
67 
68     t_info = xc_map_foreign_range(xch, DOMID_XEN,
69                     sysctl.u.tbuf_op.size, PROT_READ | PROT_WRITE,
70                     sysctl.u.tbuf_op.buffer_mfn);
71 
72     if ( t_info == NULL || t_info->tbuf_size == 0 )
73         rc = -1;
74     else
75 	*size = t_info->tbuf_size;
76 
77     xenforeignmemory_unmap(xch->fmem, t_info, sysctl.u.tbuf_op.size);
78 
79     return rc;
80 }
81 
xc_tbuf_enable(xc_interface * xch,unsigned long pages,unsigned long * mfn,unsigned long * size)82 int xc_tbuf_enable(xc_interface *xch, unsigned long pages, unsigned long *mfn,
83                    unsigned long *size)
84 {
85     DECLARE_SYSCTL;
86     int rc;
87 
88     /*
89      * Ignore errors (at least for now) as we get an error if size is already
90      * set (since trace buffers cannot be reallocated). If we really have no
91      * buffers at all then tbuf_enable() will fail, so this is safe.
92      */
93     (void)xc_tbuf_set_size(xch, pages);
94 
95     if ( tbuf_enable(xch, 1) != 0 )
96         return -1;
97 
98     sysctl.cmd = XEN_SYSCTL_tbuf_op;
99     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
100     sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_get_info;
101 
102     rc = xc_sysctl(xch, &sysctl);
103     if ( rc == 0 )
104     {
105         *size = sysctl.u.tbuf_op.size;
106         *mfn = sysctl.u.tbuf_op.buffer_mfn;
107     }
108 
109     return 0;
110 }
111 
xc_tbuf_disable(xc_interface * xch)112 int xc_tbuf_disable(xc_interface *xch)
113 {
114     return tbuf_enable(xch, 0);
115 }
116 
xc_tbuf_set_cpu_mask(xc_interface * xch,xc_cpumap_t mask)117 int xc_tbuf_set_cpu_mask(xc_interface *xch, xc_cpumap_t mask)
118 {
119     DECLARE_SYSCTL;
120     DECLARE_HYPERCALL_BOUNCE(mask, 0, XC_HYPERCALL_BUFFER_BOUNCE_IN);
121     int ret = -1;
122     int bits, cpusize;
123 
124     cpusize = xc_get_cpumap_size(xch);
125     if (cpusize <= 0)
126     {
127         PERROR("Could not get number of cpus");
128         return -1;
129     }
130 
131     HYPERCALL_BOUNCE_SET_SIZE(mask, cpusize);
132 
133     bits = xc_get_max_cpus(xch);
134     if (bits <= 0)
135     {
136         PERROR("Could not get number of bits");
137         return -1;
138     }
139 
140     if ( xc_hypercall_bounce_pre(xch, mask) )
141     {
142         PERROR("Could not allocate memory for xc_tbuf_set_cpu_mask hypercall");
143         goto out;
144     }
145 
146     sysctl.cmd = XEN_SYSCTL_tbuf_op;
147     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
148     sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_set_cpu_mask;
149 
150     set_xen_guest_handle(sysctl.u.tbuf_op.cpu_mask.bitmap, mask);
151     sysctl.u.tbuf_op.cpu_mask.nr_bits = bits;
152 
153     ret = do_sysctl(xch, &sysctl);
154 
155     xc_hypercall_bounce_post(xch, mask);
156 
157  out:
158     return ret;
159 }
160 
xc_tbuf_set_evt_mask(xc_interface * xch,uint32_t mask)161 int xc_tbuf_set_evt_mask(xc_interface *xch, uint32_t mask)
162 {
163     DECLARE_SYSCTL;
164 
165     sysctl.cmd = XEN_SYSCTL_tbuf_op;
166     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
167     sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_set_evt_mask;
168     sysctl.u.tbuf_op.evt_mask = mask;
169 
170     return do_sysctl(xch, &sysctl);
171 }
172 
173