Support for blocking and restarting uthreads (XCC)
[akaros.git] / kern / include / ros / syscall.h
1 #ifndef ROS_INCLUDE_SYSCALL_H
2 #define ROS_INCLUDE_SYSCALL_H
3
4 #include <arch/arch.h>
5 #include <ros/bits/syscall.h>
6 #include <ros/arch/syscall.h>
7 #include <ros/event.h>
8
9 /* Flags for an individual syscall */
10 #define SC_DONE                                 0x0001          /* SC is done */
11 #define SC_PROGRESS                             0x0002          /* SC made progress */
12 #define SC_UEVENT                               0x0004          /* user has an ev_q */
13
14 struct syscall {
15         unsigned int                            num;
16         long                                            retval;
17         int                                                     err;                    /* errno */
18         int                                                     flags;
19         struct event_queue                      *ev_q;
20         void                                            *u_data;
21         long                                            arg0;
22         long                                            arg1;
23         long                                            arg2;
24         long                                            arg3;
25         long                                            arg4;
26         long                                            arg5;
27 };
28
29 #ifndef ROS_KERNEL
30
31 /* Attempts to block on sysc, returning when it is done or progress has been
32  * made. */
33 void ros_syscall_blockon(struct syscall *sysc);
34
35 /* This weak version is meant to work if there is no 2LS.  For now we just
36  * spin, but in the future we could block the whole process. */
37 static inline void __ros_syscall_blockon(struct syscall *sysc)
38 {
39         while (!(sysc->flags & (SC_DONE | SC_PROGRESS)))
40                 cpu_relax();
41 }
42 weak_alias(__ros_syscall_blockon, ros_syscall_blockon);
43
44 /* TODO: make variants of __ros_syscall() based on the number of args (0 - 6) */
45 /* These are simple synchronous system calls, built on top of the kernel's async
46  * interface.  This version makes no assumptions about errno.  You usually don't
47  * want this. */
48 static inline long __ros_syscall(unsigned int _num, long _a0, long _a1, long _a2,
49                                  long _a3, long _a4, long _a5, int *errno_loc)
50 {
51         int num_started;        /* not used yet */
52         struct syscall sysc = {0};
53         sysc.num = _num;
54         sysc.ev_q = 0;
55         sysc.arg0 = _a0;
56         sysc.arg1 = _a1;
57         sysc.arg2 = _a2;
58         sysc.arg3 = _a3;
59         sysc.arg4 = _a4;
60         sysc.arg5 = _a5;
61         num_started = __ros_arch_syscall(&sysc, 1);
62         /* Don't proceed til we are done */
63         while (!(sysc.flags & SC_DONE))
64                 ros_syscall_blockon(&sysc);
65         if (errno_loc)
66                 *errno_loc = sysc.err;
67         return sysc.retval;
68 }
69
70 #include <errno.h>
71
72 /* This version knows about errno and will handle it. */
73 static inline long __ros_syscall_errno(unsigned int _num, long _a0, long _a1,
74                                        long _a2, long _a3, long _a4, long _a5)
75 {
76         return __ros_syscall(_num, _a0, _a1, _a2, _a3, _a4, _a5, &errno);
77 }
78
79 /* Convenience wrapper for __ros_syscall */
80 #define ros_syscall(which, a0, a1, a2, a3, a4, a5) \
81    __ros_syscall_errno(which, (long)(a0), (long)(a1), (long)(a2), (long)(a3), \
82                        (long)(a4), (long)(a5))
83
84 #endif /* ifndef ROS_KERNEL */
85
86 #endif /* !ROS_INCLUDE_SYSCALL_H */