1 /*
2  * Copyright (C) 2017 Citrix Systems R&D
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <xen/errno.h>
28 #include <xen/guest_access.h>
29 #include <xen/types.h>
30 
31 #include "coverage.h"
32 
33 #ifndef __clang__
34 #error "LLVM coverage selected without clang compiler"
35 #endif
36 
37 #if BITS_PER_LONG == 64
38 #define LLVM_PROFILE_MAGIC (((uint64_t)255 << 56) | ((uint64_t)'l' << 48) | \
39     ((uint64_t)'p' << 40) | ((uint64_t)'r' << 32) | ((uint64_t)'o' << 24) | \
40     ((uint64_t)'f' << 16) | ((uint64_t)'r' << 8)  | ((uint64_t)129))
41 #else
42 #define LLVM_PROFILE_MAGIC (((uint64_t)255 << 56) | ((uint64_t)'l' << 48) | \
43     ((uint64_t)'p' << 40) | ((uint64_t)'r' << 32) | ((uint64_t)'o' << 24) | \
44     ((uint64_t)'f' << 16) | ((uint64_t)'R' << 8)  | ((uint64_t)129)
45 #endif
46 
47 #define LLVM_PROFILE_VERSION    4
48 #define LLVM_PROFILE_NUM_KINDS  2
49 
50 struct llvm_profile_data {
51     uint64_t name_ref;
52     uint64_t function_hash;
53     void *counter;
54     void *function;
55     void *values;
56     uint32_t nr_counters;
57     uint16_t nr_value_sites[LLVM_PROFILE_NUM_KINDS];
58 };
59 
60 struct llvm_profile_header {
61     uint64_t magic;
62     uint64_t version;
63     uint64_t data_size;
64     uint64_t counters_size;
65     uint64_t names_size;
66     uint64_t counters_delta;
67     uint64_t names_delta;
68     uint64_t value_kind_last;
69 };
70 
71 /*
72  * Since Xen uses the llvm code coverage support without the run time library
73  * __llvm_profile_runtime must be defined according to the docs at:
74  *
75  * https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
76  */
77 int __llvm_profile_runtime;
78 
79 extern const struct llvm_profile_data __start___llvm_prf_data[];
80 extern const struct llvm_profile_data __stop___llvm_prf_data[];
81 extern const char __start___llvm_prf_names[];
82 extern const char __stop___llvm_prf_names[];
83 extern uint64_t __start___llvm_prf_cnts[];
84 extern uint64_t __stop___llvm_prf_cnts[];
85 
86 #define START_DATA      ((const void *)__start___llvm_prf_data)
87 #define END_DATA        ((const void *)__stop___llvm_prf_data)
88 #define START_NAMES     ((const void *)__start___llvm_prf_names)
89 #define END_NAMES       ((const void *)__stop___llvm_prf_names)
90 #define START_COUNTERS  ((void *)__start___llvm_prf_cnts)
91 #define END_COUNTERS    ((void *)__stop___llvm_prf_cnts)
92 
reset_counters(void)93 static void cf_check reset_counters(void)
94 {
95     memset(START_COUNTERS, 0, END_COUNTERS - START_COUNTERS);
96 }
97 
get_size(void)98 static uint32_t cf_check get_size(void)
99 {
100     return ROUNDUP(sizeof(struct llvm_profile_header) + END_DATA - START_DATA +
101                    END_COUNTERS - START_COUNTERS + END_NAMES - START_NAMES, 8);
102 }
103 
dump(XEN_GUEST_HANDLE_PARAM (char)buffer,uint32_t * buf_size)104 static int cf_check dump(
105     XEN_GUEST_HANDLE_PARAM(char) buffer, uint32_t *buf_size)
106 {
107     struct llvm_profile_header header = {
108         .magic = LLVM_PROFILE_MAGIC,
109         .version = LLVM_PROFILE_VERSION,
110         .data_size = (END_DATA - START_DATA) / sizeof(struct llvm_profile_data),
111         .counters_size = (END_COUNTERS - START_COUNTERS) / sizeof(uint64_t),
112         .names_size = END_NAMES - START_NAMES,
113         .counters_delta = (uintptr_t)START_COUNTERS,
114         .names_delta = (uintptr_t)START_NAMES,
115         .value_kind_last = LLVM_PROFILE_NUM_KINDS - 1,
116     };
117     unsigned int off = 0;
118 
119 #define APPEND_TO_BUFFER(src, size)                             \
120 ({                                                              \
121     if ( off + (size) > *buf_size )                             \
122         return -ENOMEM;                                         \
123     copy_to_guest_offset(buffer, off, (const char *)src, size); \
124     off += (size);                                              \
125 })
126     APPEND_TO_BUFFER(&header, sizeof(header));
127     APPEND_TO_BUFFER(START_DATA, END_DATA - START_DATA);
128     APPEND_TO_BUFFER(START_COUNTERS, END_COUNTERS - START_COUNTERS);
129     APPEND_TO_BUFFER(START_NAMES, END_NAMES - START_NAMES);
130 #undef APPEND_TO_BUFFER
131 
132     clear_guest_offset(buffer, off, *buf_size - off);
133 
134     return 0;
135 }
136 
137 const struct cov_sysctl_ops cov_ops = {
138     .get_size = get_size,
139     .reset_counters = reset_counters,
140     .dump = dump,
141 };
142 
143 /*
144  * Local variables:
145  * mode: C
146  * c-file-style: "BSD"
147  * c-basic-offset: 4
148  * tab-width: 4
149  * indent-tabs-mode: nil
150  * End:
151  */
152