Skip to content

Commit 0865053

Browse files
committed
lkl: let atomic ops outsourced
This will allow us to reimplpement atomic ops with lock/touch/unlock way if underlying hosts don't support a particular atomic ops. Signed-off-by: Hajime Tazaki <thehajime@gmail.com>
1 parent a4c9c32 commit 0865053

File tree

6 files changed

+105
-12
lines changed

6 files changed

+105
-12
lines changed

arch/lkl/include/uapi/asm/host_ops.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,11 @@ int lkl_is_running(void);
151151
int lkl_printf(const char *, ...);
152152
void lkl_bug(const char *, ...);
153153

154+
/* atomic ops */
155+
int lkl__sync_fetch_and_sub(int *ptr, int value);
156+
int lkl__sync_fetch_and_add(int *ptr, int value);
157+
long lkl__sync_fetch_and_or(long *ptr, long value);
158+
long lkl__sync_fetch_and_and(long *ptr, long value);
159+
void lkl__sync_synchronize(void);
160+
154161
#endif

arch/lkl/kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
extra-y := vmlinux.lds
22

33
obj-y = setup.o threads.o irq.o time.o syscalls.o misc.o console.o \
4-
syscalls_32.o cpu.o
4+
syscalls_32.o cpu.o atomic.o

arch/lkl/kernel/atomic.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <linux/kernel.h>
2+
#include <linux/init.h>
3+
#include <asm/host_ops.h>
4+
5+
#if defined(__ARMEL__)
6+
static void *atomic_lock;
7+
8+
long lkl__sync_fetch_and_or(long *ptr, long value)
9+
{
10+
lkl_ops->sem_down(atomic_lock);
11+
*ptr = value;
12+
lkl_ops->sem_up(atomic_lock);
13+
return 0;
14+
}
15+
16+
long lkl__sync_fetch_and_and(long *ptr, long value)
17+
{
18+
int tmp;
19+
20+
lkl_ops->sem_down(atomic_lock);
21+
tmp = *ptr;
22+
*ptr *= value;
23+
lkl_ops->sem_up(atomic_lock);
24+
return tmp;
25+
}
26+
27+
int lkl__sync_fetch_and_add(int *ptr, int value)
28+
{
29+
int tmp;
30+
31+
lkl_ops->sem_down(atomic_lock);
32+
tmp = *ptr;
33+
*ptr += value;
34+
lkl_ops->sem_up(atomic_lock);
35+
return tmp;
36+
}
37+
38+
int lkl__sync_fetch_and_sub(int *ptr, int value)
39+
{
40+
int tmp;
41+
42+
lkl_ops->sem_down(atomic_lock);
43+
tmp = *ptr;
44+
*ptr -= value;
45+
lkl_ops->sem_up(atomic_lock);
46+
return tmp;
47+
}
48+
49+
void lkl__sync_synchronize(void)
50+
{
51+
}
52+
53+
static int __init lkl_atomic_ops_init(void)
54+
{
55+
atomic_lock = lkl_ops->sem_alloc(1);
56+
return 0;
57+
}
58+
early_initcall(lkl_atomic_ops_init);
59+
60+
#else
61+
long lkl__sync_fetch_and_or(long *ptr, long value)
62+
{
63+
return __sync_fetch_and_or(ptr, value);
64+
}
65+
66+
long lkl__sync_fetch_and_and(long *ptr, long value)
67+
{
68+
return __sync_fetch_and_and(ptr, value);
69+
}
70+
71+
int lkl__sync_fetch_and_add(int *ptr, int value)
72+
{
73+
return __sync_fetch_and_add(ptr, value);
74+
}
75+
76+
int lkl__sync_fetch_and_sub(int *ptr, int value)
77+
{
78+
return __sync_fetch_and_sub(ptr, value);
79+
}
80+
81+
void lkl__sync_synchronize(void)
82+
{
83+
return __sync_synchronize();
84+
}
85+
#endif
86+

arch/lkl/kernel/cpu.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int __cpu_try_get_lock(int n)
6666
{
6767
lkl_thread_t self;
6868

69-
if (__sync_fetch_and_add(&cpu.shutdown_gate, n) >= MAX_THREADS)
69+
if (lkl__sync_fetch_and_add(&cpu.shutdown_gate, n) >= MAX_THREADS)
7070
return -2;
7171

7272
lkl_ops->mutex_lock(cpu.lock);
@@ -89,7 +89,7 @@ static void __cpu_try_get_unlock(int lock_ret, int n)
8989
{
9090
if (lock_ret >= -1)
9191
lkl_ops->mutex_unlock(cpu.lock);
92-
__sync_fetch_and_sub(&cpu.shutdown_gate, n);
92+
lkl__sync_fetch_and_sub(&cpu.shutdown_gate, n);
9393
}
9494

9595
void lkl_cpu_change_owner(lkl_thread_t owner)
@@ -173,7 +173,7 @@ int lkl_cpu_try_run_irq(int irq)
173173

174174
void lkl_cpu_shutdown(void)
175175
{
176-
__sync_fetch_and_add(&cpu.shutdown_gate, MAX_THREADS);
176+
lkl__sync_fetch_and_add(&cpu.shutdown_gate, MAX_THREADS);
177177
}
178178

179179
void lkl_cpu_wait_shutdown(void)
@@ -184,7 +184,7 @@ void lkl_cpu_wait_shutdown(void)
184184

185185
static void lkl_cpu_cleanup(bool shutdown)
186186
{
187-
while (__sync_fetch_and_add(&cpu.shutdown_gate, 0) > MAX_THREADS)
187+
while (lkl__sync_fetch_and_add(&cpu.shutdown_gate, 0) > MAX_THREADS)
188188
;
189189

190190
if (shutdown)

arch/lkl/kernel/irq.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ static inline unsigned long test_and_clear_irq_index_status(void)
2727
{
2828
if (!irq_index_status)
2929
return 0;
30-
return __sync_fetch_and_and(&irq_index_status, 0);
30+
return lkl__sync_fetch_and_and(&irq_index_status, 0);
3131
}
3232

3333
static inline unsigned long test_and_clear_irq_status(int index)
3434
{
3535
if (!&irq_status[index])
3636
return 0;
37-
return __sync_fetch_and_and(&irq_status[index], 0);
37+
return lkl__sync_fetch_and_and(&irq_status[index], 0);
3838
}
3939

4040
void set_irq_pending(int irq)
4141
{
4242
int index = irq / IRQ_STATUS_BITS;
4343
int bit = irq % IRQ_STATUS_BITS;
4444

45-
__sync_fetch_and_or(&irq_status[index], BIT(bit));
46-
__sync_fetch_and_or(&irq_index_status, BIT(index));
45+
lkl__sync_fetch_and_or(&irq_status[index], BIT(bit));
46+
lkl__sync_fetch_and_or(&irq_index_status, BIT(index));
4747
}
4848

4949
static struct irq_info {

arch/lkl/kernel/threads.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct task_struct *__switch_to(struct task_struct *prev,
123123
}
124124

125125
if (_prev->dead) {
126-
__sync_fetch_and_sub(&threads_counter, 1);
126+
lkl__sync_fetch_and_sub((int *)&threads_counter, 1);
127127
lkl_ops->thread_exit();
128128
}
129129

@@ -193,7 +193,7 @@ int copy_thread(unsigned long clone_flags, unsigned long esp,
193193
return -ENOMEM;
194194
}
195195

196-
__sync_fetch_and_add(&threads_counter, 1);
196+
lkl__sync_fetch_and_add((int *)&threads_counter, 1);
197197

198198
return 0;
199199
}
@@ -220,7 +220,7 @@ void threads_init(void)
220220

221221
void threads_cnt_dec(void)
222222
{
223-
__sync_fetch_and_sub(&threads_counter, 1);
223+
lkl__sync_fetch_and_sub((int *)&threads_counter, 1);
224224
}
225225

226226
void threads_cleanup(void)

0 commit comments

Comments
 (0)