hc
2023-10-25 6c2073b7aa40e29d0eca7d571dd7bc590c7ecaa7
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
// SPDX-License-Identifier: GPL-2.0
#include <config.h>
 
#include "preempt.h"
 
#include "assume.h"
#include "locks.h"
 
/* Support NR_CPUS of at most 64 */
#define CPU_PREEMPTION_LOCKS_INIT0 LOCK_IMPL_INITIALIZER
#define CPU_PREEMPTION_LOCKS_INIT1 \
   CPU_PREEMPTION_LOCKS_INIT0, CPU_PREEMPTION_LOCKS_INIT0
#define CPU_PREEMPTION_LOCKS_INIT2 \
   CPU_PREEMPTION_LOCKS_INIT1, CPU_PREEMPTION_LOCKS_INIT1
#define CPU_PREEMPTION_LOCKS_INIT3 \
   CPU_PREEMPTION_LOCKS_INIT2, CPU_PREEMPTION_LOCKS_INIT2
#define CPU_PREEMPTION_LOCKS_INIT4 \
   CPU_PREEMPTION_LOCKS_INIT3, CPU_PREEMPTION_LOCKS_INIT3
#define CPU_PREEMPTION_LOCKS_INIT5 \
   CPU_PREEMPTION_LOCKS_INIT4, CPU_PREEMPTION_LOCKS_INIT4
 
/*
 * Simulate disabling preemption by locking a particular cpu. NR_CPUS
 * should be the actual number of cpus, not just the maximum.
 */
struct lock_impl cpu_preemption_locks[NR_CPUS] = {
   CPU_PREEMPTION_LOCKS_INIT0
#if (NR_CPUS - 1) & 1
   , CPU_PREEMPTION_LOCKS_INIT0
#endif
#if (NR_CPUS - 1) & 2
   , CPU_PREEMPTION_LOCKS_INIT1
#endif
#if (NR_CPUS - 1) & 4
   , CPU_PREEMPTION_LOCKS_INIT2
#endif
#if (NR_CPUS - 1) & 8
   , CPU_PREEMPTION_LOCKS_INIT3
#endif
#if (NR_CPUS - 1) & 16
   , CPU_PREEMPTION_LOCKS_INIT4
#endif
#if (NR_CPUS - 1) & 32
   , CPU_PREEMPTION_LOCKS_INIT5
#endif
};
 
#undef CPU_PREEMPTION_LOCKS_INIT0
#undef CPU_PREEMPTION_LOCKS_INIT1
#undef CPU_PREEMPTION_LOCKS_INIT2
#undef CPU_PREEMPTION_LOCKS_INIT3
#undef CPU_PREEMPTION_LOCKS_INIT4
#undef CPU_PREEMPTION_LOCKS_INIT5
 
__thread int thread_cpu_id;
__thread int preempt_disable_count;
 
void preempt_disable(void)
{
   BUG_ON(preempt_disable_count < 0 || preempt_disable_count == INT_MAX);
 
   if (preempt_disable_count++)
       return;
 
   thread_cpu_id = nondet_int();
   assume(thread_cpu_id >= 0);
   assume(thread_cpu_id < NR_CPUS);
   lock_impl_lock(&cpu_preemption_locks[thread_cpu_id]);
}
 
void preempt_enable(void)
{
   BUG_ON(preempt_disable_count < 1);
 
   if (--preempt_disable_count)
       return;
 
   lock_impl_unlock(&cpu_preemption_locks[thread_cpu_id]);
}