forked from lkl/lkl-linux-2.6
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syscalls.c
618 lines (501 loc) · 13 KB
/
syscalls.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include <linux/syscalls.h>
#include <asm/unistd.h>
#include <asm/callbacks.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/syscalls.h>
#include <linux/net.h>
typedef long (*syscall_handler_t)(long arg1, ...);
syscall_handler_t syscall_table[NR_syscalls];
static struct syscall_queue {
struct list_head lh;
wait_queue_head_t wqh;
void *thread;
struct hlist_node hn;
} *default_queue;
#define SYSCALL_QUEUE_HASH_SIZE 113
static struct hlist_head syscall_queue_hash[SYSCALL_QUEUE_HASH_SIZE];
static void* syscall_queue_hash_lock;
static inline struct hlist_head *syscall_queue_hash_head(void *thread)
{
int i=jhash_2words(((u64)(u32)thread) >> 32,
((u64)(u32)thread)&0xffffffff,
0) % SYSCALL_QUEUE_HASH_SIZE;
return &syscall_queue_hash[i];
}
static struct syscall_queue* get_syscall_queue(void *thread)
{
struct hlist_node *i;
struct hlist_head *head=syscall_queue_hash_head(thread);
lkl_nops->sem_down(syscall_queue_hash_lock);
hlist_for_each(i, head) {
struct syscall_queue *sq=hlist_entry(i, struct syscall_queue,
hn);
if (sq->thread == thread) {
lkl_nops->sem_up(syscall_queue_hash_lock);
return sq;
}
}
lkl_nops->sem_up(syscall_queue_hash_lock);
return default_queue;
}
static struct syscall_queue* new_syscall_queue(void *thread)
{
struct syscall_queue *sq=kmalloc(sizeof(*sq), GFP_KERNEL);
if (!sq)
return NULL;
INIT_LIST_HEAD(&sq->lh);
init_waitqueue_head(&sq->wqh);
sq->thread=thread;
lkl_nops->sem_down(syscall_queue_hash_lock);
hlist_add_head(&sq->hn, syscall_queue_hash_head(thread));
lkl_nops->sem_up(syscall_queue_hash_lock);
return sq;
}
static void del_syscall_queue(struct syscall_queue *sq)
{
lkl_nops->sem_down(syscall_queue_hash_lock);
hlist_del(&sq->hn);
lkl_nops->sem_up(syscall_queue_hash_lock);
kfree(sq);
}
struct syscall {
long no, params[6], ret;
void *sem;
struct syscall_queue *queue;
union {
struct list_head lh;
struct work_struct work;
};
};
static struct syscall* dequeue_syscall(struct list_head *head)
{
struct syscall *s=NULL;
if (!list_empty(head)) {
s=list_first_entry(head, typeof(*s), lh);
list_del(&s->lh);
}
return s;
}
static long run_syscall(struct syscall *s)
{
int ret;
if (s->no < 0 || s->no >= NR_syscalls ||
syscall_table[s->no] == NULL)
ret=-ENOSYS;
else
ret=syscall_table[s->no](s->params[0], s->params[1],
s->params[2], s->params[3],
s->params[4], s->params[5]);
s->ret=ret;
if (s->sem)
lkl_nops->sem_up(s->sem);
return ret;
}
static void run_syscall_work(struct work_struct *work)
{
struct syscall *s=container_of(work, struct syscall, work);
run_syscall(s);
}
int run_syscalls(void *_sq)
{
struct syscall_queue *sq;
struct syscall *s;
if (!_sq)
sq=default_queue;
else
sq=(struct syscall_queue*)_sq;
snprintf(current->comm, sizeof(current->comm), "ksyscalld%d",
current->pid);
while (1) {
wait_event(sq->wqh, (s=dequeue_syscall(&sq->lh)) != NULL);
if (s->no ==__NR_reboot)
break;
run_syscall(s);
}
del_syscall_queue(sq);
s->ret=0;
lkl_nops->sem_up(s->sem);
if (sq != default_queue)
do_exit(0);
return 0;
}
static irqreturn_t syscall_irq(int irq, void *dev_id)
{
struct pt_regs *regs=get_irq_regs();
struct syscall *s=regs->irq_data;
if (!s->queue) {
INIT_WORK(&s->work, run_syscall_work);
schedule_work(&s->work);
} else {
list_add(&s->lh, &s->queue->lh);
wake_up(&s->queue->wqh);
}
return IRQ_HANDLED;
}
static struct irqaction syscall_irqaction = {
.handler = syscall_irq,
.flags = IRQF_DISABLED | IRQF_NOBALANCING,
.mask = CPU_MASK_CPU0,
.dev_id = &syscall_irqaction,
.name = "syscall"
};
#define SYSCALL_VIA_QUEUE(_queue, _syscall, _params...) \
({ \
struct syscall s = { \
.no = __NR_##_syscall, \
.params = { _params }, \
.queue = _queue \
}; \
if (!(s.sem=lkl_nops->sem_alloc(0))) \
return -ENOMEM; \
lkl_trigger_irq_with_data(SYSCALL_IRQ, &s); \
lkl_nops->sem_down(s.sem); \
lkl_nops->sem_free(s.sem); \
s.ret; \
})
#define SYSCALL(_syscall, _params...) \
({ \
int ret; \
if (lkl_nops->thread_id) \
ret=SYSCALL_VIA_QUEUE(get_syscall_queue(lkl_nops->thread_id()), _syscall, _params); \
else \
ret=SYSCALL_VIA_QUEUE(default_queue, _syscall, _params); \
ret; \
})
#define SYSCALL_VIA_WORKQUEUE(_syscall, _params...) \
SYSCALL_VIA_QUEUE(NULL, _syscall, _params)
static asmlinkage long sys_call(long _f, long arg1, long arg2, long arg3,
long arg4, long arg5)
{
long (*f)(long arg1, long arg2, long arg3, long arg4, long arg5)=
(long (*)(long, long, long, long, long))_f;
return f(arg1, arg2, arg3, arg4, arg5);
}
/*
* sys_mount (is sloppy?? and) copies a full page from dev_name, type and data
* which can trigger page faults (which a normal kernel can safely
* ignore). Make sure we don't trigger page fauls.
*/
asmlinkage long sys_safe_mount(char __user *dev_name, char __user *dir_name,
char __user *type, unsigned long flags,
void __user *data)
{
int err;
unsigned long _dev_name=0, _type=0, _data=0;
err=-ENOMEM;
if (dev_name) {
_dev_name=__get_free_page(GFP_KERNEL);
if (!_dev_name)
goto out_free;
strcpy((char*)_dev_name, dev_name);
}
if (type) {
_type=__get_free_page(GFP_KERNEL);
if (!_type)
goto out_free;
strcpy((char*)_type, type);
}
if (data) {
_data=__get_free_page(GFP_KERNEL);
if (!_data)
goto out_free;
strcpy((char*)_data, data);
}
err=sys_mount((char __user*)_dev_name, dir_name, (char __user*)_type,
flags, (char __user*)_data);
out_free:
if (_dev_name)
free_page(_dev_name);
if (_type)
free_page(_type);
if (_data)
free_page(_data);
return err;
}
ssize_t sys_lkl_pwrite64(unsigned int fd, const char *buf, size_t count,
off_t pos_hi, off_t pos_lo)
{
return sys_pwrite64(fd, buf, count, ((loff_t)pos_hi<<32)+pos_lo);
}
ssize_t sys_lkl_pread64(unsigned int fd, const char *buf, size_t count,
off_t pos_hi, off_t pos_lo)
{
return sys_pwrite64(fd, buf, count, ((loff_t)pos_hi<<32)+pos_lo);
}
#define INIT_STE(x) syscall_table[__NR_##x]=(syscall_handler_t)sys_##x;
void init_syscall_table(void)
{
INIT_STE(ni_syscall);
INIT_STE(sync);
INIT_STE(reboot);
INIT_STE(write);
INIT_STE(close);
INIT_STE(unlink);
INIT_STE(open);
INIT_STE(poll);
INIT_STE(read);
INIT_STE(lseek);
INIT_STE(rename);
INIT_STE(flock);
INIT_STE(newfstat);
INIT_STE(chmod);
INIT_STE(newlstat);
INIT_STE(mkdir);
INIT_STE(rmdir);
INIT_STE(getdents);
INIT_STE(newstat);
INIT_STE(utimes);
INIT_STE(utime);
INIT_STE(nanosleep);
INIT_STE(mknod);
INIT_STE(safe_mount);
INIT_STE(umount);
INIT_STE(chdir);
INIT_STE(statfs);
INIT_STE(chroot);
INIT_STE(getcwd);
INIT_STE(chown);
INIT_STE(umask);
INIT_STE(getuid);
INIT_STE(getgid);
#ifdef CONFIG_NET
INIT_STE(socketcall);
#endif
INIT_STE(ioctl);
INIT_STE(call);
INIT_STE(access);
INIT_STE(truncate);
INIT_STE(lkl_pwrite64);
INIT_STE(lkl_pwrite64);
}
long lkl_sys_sync(void)
{
return SYSCALL(sync);
}
long lkl_sys_umount(const char *path, int flags)
{
return SYSCALL(umount, (long)path, flags);
}
ssize_t lkl_sys_write(unsigned int fd, const char *buf, size_t count)
{
return SYSCALL(write, fd, (long)buf, count);
}
long lkl_sys_close(unsigned int fd)
{
return SYSCALL(close, fd);
}
long lkl_sys_unlink(const char *pathname)
{
return SYSCALL(unlink, (long)pathname);
}
long lkl_sys_open(const char *filename, int flags, int mode)
{
return SYSCALL(open, (long)filename, flags, mode);
}
long lkl_sys_poll(struct pollfd *ufds, unsigned int nfds, long timeout)
{
return SYSCALL(poll, (long)ufds, nfds, timeout);
}
ssize_t lkl_sys_read(unsigned int fd, char *buf, size_t count)
{
return SYSCALL(read, fd, (long)buf, count);
}
off_t lkl_sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
{
return SYSCALL(lseek, fd, offset, origin);
}
long lkl_sys_rename(const char *oldname, const char *newname)
{
return SYSCALL(rename, (long)oldname, (long)newname);
}
long lkl_sys_flock(unsigned int fd, unsigned int cmd)
{
return SYSCALL(flock, fd, cmd);
}
long lkl_sys_newfstat(unsigned int fd, struct stat *statbuf)
{
return SYSCALL(newfstat, fd, (long)statbuf);
}
long lkl_sys_chmod(const char *filename, mode_t mode)
{
return SYSCALL(chmod, (long)filename, mode);
}
long lkl_sys_newlstat(char *filename, struct stat *statbuf)
{
return SYSCALL(newlstat, (long)filename, (long)statbuf);
}
long lkl_sys_mkdir(const char *pathname, int mode)
{
return SYSCALL(mkdir, (long)pathname, mode);
}
long lkl_sys_rmdir(const char *pathname)
{
return SYSCALL(rmdir, (long)pathname);
}
long lkl_sys_getdents(unsigned int fd, struct linux_dirent *dirent, unsigned int count)
{
return SYSCALL(getdents, fd, (long)dirent, count);
}
long lkl_sys_newstat(char *filename, struct stat *statbuf)
{
return SYSCALL(newstat, (long)filename, (long)statbuf);
}
long lkl_sys_utimes(const char *filename, struct timeval *utimes)
{
return SYSCALL(utime, (long)filename, (long)utimes);
}
long lkl_sys_mount(const char *dev, const char *mnt_point, const char *fs, int flags, void *data)
{
return SYSCALL(safe_mount, (long)dev, (long)mnt_point, (long)fs, flags, (long)data);
}
long lkl_sys_chdir(const char *dir)
{
return SYSCALL(chdir, (long)dir);
}
long lkl_sys_mknod(const char *filename, int mode, unsigned dev)
{
return SYSCALL(mknod, (long)filename, mode, dev);
}
long lkl_sys_chroot(const char *dir)
{
return SYSCALL(chroot, (long)dir);
}
long lkl_sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp)
{
return SYSCALL(nanosleep, (long)rqtp, (long)rmtp);
}
long lkl_sys_getcwd(char *buf, unsigned long size)
{
return SYSCALL(getcwd, (long)buf, (long) size);
}
long lkl_sys_utime(const char *filename, const struct utimbuf *buf)
{
return SYSCALL(utime, (long)filename, (long)buf);
}
long lkl_sys_socket(int family, int type, int protocol)
{
long args[6]={family, type, protocol};
return SYSCALL(socketcall, SYS_SOCKET, (long)args);
}
long lkl_sys_send(int sock, void *buffer, size_t size, unsigned flags)
{
long args[6]={sock, (long)buffer, size, flags};
return SYSCALL(socketcall, SYS_SEND, (long)args);
}
long lkl_sys_recv(int sock, void *buffer, size_t size, unsigned flags)
{
long args[6]={sock, (long)buffer, size, flags};
return SYSCALL(socketcall, SYS_RECV, (long)args);
}
long lkl_sys_connect(int sock, struct sockaddr *saddr, int len)
{
long args[6]={sock, (long)saddr, len};
return SYSCALL(socketcall, SYS_CONNECT, (long)args);
}
long lkl_sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
{
return SYSCALL(ioctl, fd, cmd, arg);
}
long lkl_sys_umask(int mask)
{
return SYSCALL(umask, mask);
}
long lkl_sys_getuid(void)
{
return SYSCALL(getuid);
}
long lkl_sys_getgid(void)
{
return SYSCALL(getgid);
}
long lkl_sys_call(long f, long arg1, long arg2, long arg3, long arg4, long arg5)
{
return SYSCALL(call, f, arg1, arg2, arg3, arg4, arg5);
}
long lkl_sys_statfs(const char *path, struct statfs *buf)
{
return SYSCALL(statfs, (long)path, (long)buf);
}
long lkl_sys_access(const char *filename, int mode)
{
return SYSCALL(access, (long)filename, mode);
}
long lkl_sys_truncate(const char *path, unsigned long length)
{
return SYSCALL(truncate, (long)path, length);
}
ssize_t lkl_sys_pwrite64(unsigned int fd, const char *buf, size_t count, loff_t pos)
{
return SYSCALL(lkl_pwrite64, fd, (long)buf, count, (pos >> 32),(pos & 0xffffffff));
}
ssize_t lkl_sys_pread64(unsigned int fd, char *buf, size_t count, loff_t pos)
{
return SYSCALL(lkl_pwrite64, fd, (long)buf, count, (pos >> 32),(pos & 0xffffffff));
}
/*
* Halt is special as we want to call syscall_done after the kernel has been
* halted, in linux_start_kernel.
*/
void *halt_sem;
long lkl_sys_halt(void)
{
int err;
if (!(halt_sem=lkl_nops->sem_alloc(0)))
return -ENOMEM;
err=SYSCALL_VIA_QUEUE(default_queue, reboot, 0, 0, 0, 0, 0, 0);
if (err < 0)
return err;
lkl_nops->sem_down(halt_sem);
lkl_nops->sem_free(halt_sem);
return 0;
}
int lkl_syscall_thread_init(void)
{
void *thread;
int err;
struct syscall_queue *sq;
if (!lkl_nops->thread_id)
return -EINVAL;
thread=lkl_nops->thread_id();
if (get_syscall_queue(thread) != default_queue)
return -EBUSY;
if (!(sq=new_syscall_queue(thread)))
return -ENOMEM;
err=SYSCALL_VIA_WORKQUEUE(call, (long)kernel_thread,
(long)run_syscalls, (long)sq, 0, 0);
if (err < 0) {
del_syscall_queue(sq);
return err;
}
return 0;
}
int lkl_syscall_thread_cleanup(void)
{
struct syscall_queue *sq;
void *thread;
if (!lkl_nops->thread_id)
return -EINVAL;
thread=lkl_nops->thread_id();
if ((sq=get_syscall_queue(thread)) == default_queue)
return -EINVAL;
return SYSCALL_VIA_QUEUE(sq, reboot, 0, 0, 0, 0, 0, 0);
}
int __init syscall_init(void)
{
init_syscall_table();
BUG_ON((syscall_queue_hash_lock=lkl_nops->sem_alloc(1)) == NULL);
setup_irq(SYSCALL_IRQ, &syscall_irqaction);
BUG_ON((default_queue=new_syscall_queue(NULL)) == NULL);
printk(KERN_INFO "lkl: syscall interface initialized\n");
return 0;
}
late_initcall(syscall_init);
void free_syscall(void)
{
lkl_nops->sem_free(syscall_queue_hash_lock);
}