forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.c
More file actions
224 lines (184 loc) · 4.97 KB
/
cpu.c
File metadata and controls
224 lines (184 loc) · 4.97 KB
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
#include <linux/kernel.h>
#include <linux/sched.h>
#include <asm/host_ops.h>
#include <asm/cpu.h>
#include <asm/thread_info.h>
#include <asm/unistd.h>
#include <asm/sched.h>
#include <asm/syscalls.h>
/*
* This structure is used to get access to the "LKL CPU" that allows us to run
* Linux code. Because we have to deal with various synchronization requirements
* between idle thread, system calls, interrupts, "reentrancy", CPU shutdown,
* imbalance wake up (i.e. acquire the CPU from one thread and release it from
* another), we can't use a simple synchronization mechanism such as (recursive)
* mutex or semaphore. Instead, we use a mutex and a bunch of status data plus a
* semaphore.
*/
struct lkl_cpu {
/* lock that protects the CPU status data */
struct lkl_mutex *lock;
/*
* Since we must free the cpu lock during shutdown we need a
* synchronization algorithm between lkl_cpu_shutdown() and the CPU
* access functions since lkl_cpu_get() gets called from thread
* destructor callback functions which may be scheduled after
* lkl_cpu_shutdown() has freed the cpu lock.
*
* An atomic counter is used to keep track of the number of running
* CPU access functions and allow the shutdown function to wait for
* them.
*
* The shutdown functions adds MAX_THREADS to this counter which allows
* the CPU access functions to check if the shutdown process has
* started.
*
* This algorithm assumes that we never have more the MAX_THREADS
* requesting CPU access.
*/
#define MAX_THREADS 1000000
unsigned int shutdown_gate;
bool irqs_pending;
/* no of threads waiting the CPU */
unsigned int sleepers;
/* no of times the current thread got the CPU */
unsigned int count;
/* current thread that owns the CPU */
lkl_thread_t owner;
/* semaphore for threads waiting the CPU */
struct lkl_sem *sem;
/* semaphore used for shutdown */
struct lkl_sem *shutdown_sem;
} cpu;
static int __cpu_try_get_lock(int n)
{
lkl_thread_t self;
if (__sync_fetch_and_add(&cpu.shutdown_gate, n) >= MAX_THREADS)
return -2;
lkl_ops->mutex_lock(cpu.lock);
if (cpu.shutdown_gate >= MAX_THREADS)
return -1;
self = lkl_ops->thread_self();
if (cpu.owner && !lkl_ops->thread_equal(cpu.owner, self))
return 0;
cpu.owner = self;
cpu.count++;
return 1;
}
static void __cpu_try_get_unlock(int lock_ret, int n)
{
if (lock_ret >= -1)
lkl_ops->mutex_unlock(cpu.lock);
__sync_fetch_and_sub(&cpu.shutdown_gate, n);
}
void lkl_cpu_change_owner(lkl_thread_t owner)
{
lkl_ops->mutex_lock(cpu.lock);
if (cpu.count > 1)
lkl_bug("bad count while changing owner\n");
cpu.owner = owner;
lkl_ops->mutex_unlock(cpu.lock);
}
int lkl_cpu_get(void)
{
int ret;
ret = __cpu_try_get_lock(1);
while (ret == 0) {
cpu.sleepers++;
__cpu_try_get_unlock(ret, 0);
lkl_ops->sem_down(cpu.sem);
ret = __cpu_try_get_lock(0);
}
__cpu_try_get_unlock(ret, 1);
return ret;
}
void lkl_cpu_put(void)
{
lkl_ops->mutex_lock(cpu.lock);
if (!cpu.count || !cpu.owner ||
!lkl_ops->thread_equal(cpu.owner, lkl_ops->thread_self()))
lkl_bug("%s: unbalanced put\n", __func__);
while (cpu.irqs_pending && !irqs_disabled()) {
cpu.irqs_pending = false;
lkl_ops->mutex_unlock(cpu.lock);
run_irqs();
lkl_ops->mutex_lock(cpu.lock);
}
if (test_ti_thread_flag(current_thread_info(), TIF_HOST_THREAD) &&
!single_task_running() && cpu.count == 1) {
if (in_interrupt())
lkl_bug("%s: in interrupt\n", __func__);
lkl_ops->mutex_unlock(cpu.lock);
thread_sched_jb();
return;
}
if (--cpu.count > 0) {
lkl_ops->mutex_unlock(cpu.lock);
return;
}
if (cpu.sleepers) {
cpu.sleepers--;
lkl_ops->sem_up(cpu.sem);
}
cpu.owner = 0;
lkl_ops->mutex_unlock(cpu.lock);
}
int lkl_cpu_try_run_irq(int irq)
{
int ret;
ret = __cpu_try_get_lock(1);
if (!ret) {
set_irq_pending(irq);
cpu.irqs_pending = true;
}
__cpu_try_get_unlock(ret, 1);
return ret;
}
void lkl_cpu_shutdown(void)
{
__sync_fetch_and_add(&cpu.shutdown_gate, MAX_THREADS);
}
void lkl_cpu_wait_shutdown(void)
{
lkl_ops->sem_down(cpu.shutdown_sem);
lkl_ops->sem_free(cpu.shutdown_sem);
}
static void lkl_cpu_cleanup(bool shutdown)
{
while (__sync_fetch_and_add(&cpu.shutdown_gate, 0) > MAX_THREADS)
;
if (shutdown)
lkl_ops->sem_up(cpu.shutdown_sem);
else if (cpu.shutdown_sem)
lkl_ops->sem_free(cpu.shutdown_sem);
if (cpu.sem)
lkl_ops->sem_free(cpu.sem);
if (cpu.lock)
lkl_ops->mutex_free(cpu.lock);
}
void arch_cpu_idle(void)
{
if (cpu.shutdown_gate >= MAX_THREADS) {
lkl_ops->mutex_lock(cpu.lock);
while (cpu.sleepers--)
lkl_ops->sem_up(cpu.sem);
lkl_ops->mutex_unlock(cpu.lock);
lkl_cpu_cleanup(true);
lkl_ops->thread_exit();
}
/* enable irqs now to allow direct irqs to run */
local_irq_enable();
/* switch to idle_host_task */
wakeup_idle_host_task();
}
int lkl_cpu_init(void)
{
cpu.lock = lkl_ops->mutex_alloc(0);
cpu.sem = lkl_ops->sem_alloc(0);
cpu.shutdown_sem = lkl_ops->sem_alloc(0);
if (!cpu.lock || !cpu.sem || !cpu.shutdown_sem) {
lkl_cpu_cleanup(false);
return -ENOMEM;
}
return 0;
}