/
external-module-compat.c
250 lines (193 loc) · 4.85 KB
/
external-module-compat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* smp_call_function_single() is not exported below 2.6.20.
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
#undef smp_call_function_single
#include <linux/spinlock.h>
#include <linux/smp.h>
struct scfs_thunk_info {
int cpu;
void (*func)(void *info);
void *info;
};
static void scfs_thunk(void *_thunk)
{
struct scfs_thunk_info *thunk = _thunk;
if (raw_smp_processor_id() == thunk->cpu)
thunk->func(thunk->info);
}
int kvm_smp_call_function_single(int cpu, void (*func)(void *info),
void *info, int wait)
{
int r, this_cpu;
struct scfs_thunk_info thunk;
this_cpu = get_cpu();
WARN_ON(irqs_disabled());
if (cpu == this_cpu) {
r = 0;
local_irq_disable();
func(info);
local_irq_enable();
} else {
thunk.cpu = cpu;
thunk.func = func;
thunk.info = info;
r = smp_call_function(scfs_thunk, &thunk, 0, 1);
}
put_cpu();
return r;
}
#define smp_call_function_single kvm_smp_call_function_single
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
/*
* pre 2.6.23 doesn't handle smp_call_function_single on current cpu
*/
#undef smp_call_function_single
#include <linux/smp.h>
int kvm_smp_call_function_single(int cpu, void (*func)(void *info),
void *info, int wait)
{
int this_cpu, r;
this_cpu = get_cpu();
WARN_ON(irqs_disabled());
if (cpu == this_cpu) {
r = 0;
local_irq_disable();
func(info);
local_irq_enable();
} else
r = smp_call_function_single(cpu, func, info, 0, wait);
put_cpu();
return r;
}
#define smp_call_function_single kvm_smp_call_function_single
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
/* The 'nonatomic' argument was removed in 2.6.27. */
#undef smp_call_function_single
#include <linux/smp.h>
int kvm_smp_call_function_single(int cpu, void (*func)(void *info),
void *info, int wait)
{
return smp_call_function_single(cpu, func, info, 0, wait);
}
#define smp_call_function_single kvm_smp_call_function_single
#endif
/* div64_u64 is fairly new */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
#ifndef CONFIG_64BIT
/* 64bit divisor, dividend and result. dynamic precision */
uint64_t div64_u64(uint64_t dividend, uint64_t divisor)
{
uint32_t high, d;
high = divisor >> 32;
if (high) {
unsigned int shift = fls(high);
d = divisor >> shift;
dividend >>= shift;
} else
d = divisor;
do_div(dividend, d);
return dividend;
}
#endif
#endif
/*
* smp_call_function_mask() is not defined/exported below 2.6.24
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
#include <linux/smp.h>
struct kvm_call_data_struct {
void (*func) (void *info);
void *info;
atomic_t started;
atomic_t finished;
int wait;
};
static void kvm_ack_smp_call(void *_data)
{
struct kvm_call_data_struct *data = _data;
/* if wait == 0, data can be out of scope
* after atomic_inc(info->started)
*/
void (*func) (void *info) = data->func;
void *info = data->info;
int wait = data->wait;
smp_mb();
atomic_inc(&data->started);
(*func)(info);
if (wait) {
smp_mb();
atomic_inc(&data->finished);
}
}
int kvm_smp_call_function_mask(cpumask_t mask,
void (*func) (void *info), void *info, int wait)
{
struct kvm_call_data_struct data;
cpumask_t allbutself;
int cpus;
int cpu;
int me;
me = get_cpu();
WARN_ON(irqs_disabled());
allbutself = cpu_online_map;
cpu_clear(me, allbutself);
cpus_and(mask, mask, allbutself);
cpus = cpus_weight(mask);
if (!cpus)
goto out;
data.func = func;
data.info = info;
atomic_set(&data.started, 0);
data.wait = wait;
if (wait)
atomic_set(&data.finished, 0);
for (cpu = first_cpu(mask); cpu != NR_CPUS; cpu = next_cpu(cpu, mask))
smp_call_function_single(cpu, kvm_ack_smp_call, &data, 0);
while (atomic_read(&data.started) != cpus) {
cpu_relax();
barrier();
}
if (!wait)
goto out;
while (atomic_read(&data.finished) != cpus) {
cpu_relax();
barrier();
}
out:
put_cpu();
return 0;
}
#endif
/* manually export hrtimer_init/start/cancel */
void (*hrtimer_init_p)(struct hrtimer *timer, clockid_t which_clock,
enum hrtimer_mode mode);
int (*hrtimer_start_p)(struct hrtimer *timer, ktime_t tim,
const enum hrtimer_mode mode);
int (*hrtimer_cancel_p)(struct hrtimer *timer);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
static void kvm_set_normalized_timespec(struct timespec *ts, time_t sec,
long nsec)
{
while (nsec >= NSEC_PER_SEC) {
nsec -= NSEC_PER_SEC;
++sec;
}
while (nsec < 0) {
nsec += NSEC_PER_SEC;
--sec;
}
ts->tv_sec = sec;
ts->tv_nsec = nsec;
}
struct timespec kvm_ns_to_timespec(const s64 nsec)
{
struct timespec ts;
if (!nsec)
return (struct timespec) {0, 0};
ts.tv_sec = div_long_long_rem_signed(nsec, NSEC_PER_SEC, &ts.tv_nsec);
if (unlikely(nsec < 0))
kvm_set_normalized_timespec(&ts, ts.tv_sec, ts.tv_nsec);
return ts;
}
#endif