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