forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscalls.c
More file actions
241 lines (189 loc) · 4.78 KB
/
syscalls.c
File metadata and controls
241 lines (189 loc) · 4.78 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <linux/stat.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/jhash.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/net.h>
#include <linux/task_work.h>
#include <linux/syscalls.h>
#include <linux/kthread.h>
#include <linux/platform_device.h>
#include <asm/host_ops.h>
#include <asm/syscalls.h>
#include <asm/syscalls_32.h>
#include <asm/cpu.h>
#include <asm/sched.h>
static asmlinkage long sys_virtio_mmio_device_add(long base, long size,
unsigned int irq);
typedef long (*syscall_handler_t)(long arg1, ...);
#undef __SYSCALL
#define __SYSCALL(nr, sym) [nr] = (syscall_handler_t)sym,
syscall_handler_t syscall_table[__NR_syscalls] = {
[0 ... __NR_syscalls - 1] = (syscall_handler_t)sys_ni_syscall,
#include <asm/unistd.h>
#if __BITS_PER_LONG == 32
#include <asm/unistd_32.h>
#endif
};
static long run_syscall(long no, long *params)
{
long ret;
if (no < 0 || no >= __NR_syscalls)
return -ENOSYS;
ret = syscall_table[no](params[0], params[1], params[2], params[3],
params[4], params[5]);
task_work_run();
return ret;
}
#define CLONE_FLAGS (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_THREAD | \
CLONE_SIGHAND | SIGCHLD)
static int host_task_id;
static struct task_struct *host0;
static int new_host_task(struct task_struct **task)
{
pid_t pid;
switch_to_host_task(host0);
pid = kernel_thread(host_task_stub, NULL, CLONE_FLAGS);
if (pid < 0)
return pid;
rcu_read_lock();
*task = find_task_by_pid_ns(pid, &init_pid_ns);
rcu_read_unlock();
host_task_id++;
snprintf((*task)->comm, sizeof((*task)->comm), "host%d", host_task_id);
return 0;
}
static void exit_task(void)
{
do_exit(0);
}
static void del_host_task(void *arg)
{
struct task_struct *task = (struct task_struct *)arg;
struct thread_info *ti = task_thread_info(task);
if (lkl_cpu_get() < 0)
return;
switch_to_host_task(task);
host_task_id--;
set_ti_thread_flag(ti, TIF_SCHED_JB);
lkl_ops->jmp_buf_set(&ti->sched_jb, exit_task);
}
static struct lkl_tls_key *task_key;
long lkl_syscall(long no, long *params)
{
struct task_struct *task = host0;
long ret;
ret = lkl_cpu_get();
if (ret < 0)
return ret;
if (lkl_ops->tls_get) {
task = lkl_ops->tls_get(task_key);
if (!task) {
ret = new_host_task(&task);
if (ret)
goto out;
lkl_ops->tls_set(task_key, task);
}
}
switch_to_host_task(task);
ret = run_syscall(no, params);
if (no == __NR_reboot) {
thread_sched_jb();
return ret;
}
out:
lkl_cpu_put();
return ret;
}
static struct task_struct *idle_host_task;
/* called from idle, don't failed, don't block */
void wakeup_idle_host_task(void)
{
if (!need_resched() && idle_host_task)
wake_up_process(idle_host_task);
}
static int idle_host_task_loop(void *unused)
{
struct thread_info *ti = task_thread_info(current);
snprintf(current->comm, sizeof(current->comm), "idle_host_task");
set_thread_flag(TIF_HOST_THREAD);
idle_host_task = current;
for (;;) {
lkl_cpu_put();
lkl_ops->sem_down(ti->sched_sem);
if (idle_host_task == NULL) {
lkl_ops->thread_exit();
return 0;
}
schedule_tail(ti->prev_sched);
}
}
int syscalls_init(void)
{
snprintf(current->comm, sizeof(current->comm), "host0");
set_thread_flag(TIF_HOST_THREAD);
host0 = current;
if (lkl_ops->tls_alloc) {
task_key = lkl_ops->tls_alloc(del_host_task);
if (!task_key)
return -1;
}
if (kernel_thread(idle_host_task_loop, NULL, CLONE_FLAGS) < 0) {
if (lkl_ops->tls_free)
lkl_ops->tls_free(task_key);
return -1;
}
return 0;
}
void syscalls_cleanup(void)
{
if (idle_host_task) {
struct thread_info *ti = task_thread_info(idle_host_task);
idle_host_task = NULL;
lkl_ops->sem_up(ti->sched_sem);
lkl_ops->thread_join(ti->tid);
}
if (lkl_ops->tls_free)
lkl_ops->tls_free(task_key);
}
SYSCALL_DEFINE3(virtio_mmio_device_add, long, base, long, size, unsigned int,
irq)
{
struct platform_device *pdev;
int ret;
struct resource res[] = {
[0] = {
.start = base,
.end = base + size - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = irq,
.end = irq,
.flags = IORESOURCE_IRQ,
},
};
pdev = platform_device_alloc("virtio-mmio", PLATFORM_DEVID_AUTO);
if (!pdev) {
dev_err(&pdev->dev, "%s: Unable to device alloc for virtio-mmio\n", __func__);
return -ENOMEM;
}
ret = platform_device_add_resources(pdev, res, ARRAY_SIZE(res));
if (ret) {
dev_err(&pdev->dev, "%s: Unable to add resources for %s%d\n", __func__, pdev->name, pdev->id);
goto exit_device_put;
}
ret = platform_device_add(pdev);
if (ret < 0) {
dev_err(&pdev->dev, "%s: Unable to add %s%d\n", __func__, pdev->name, pdev->id);
goto exit_release_pdev;
}
return pdev->id;
exit_release_pdev:
platform_device_del(pdev);
exit_device_put:
platform_device_put(pdev);
return ret;
}