Remote mbox can deal with recursive calls (XCC)
[akaros.git] / kern / include / ros / event.h
1 /* Copyright (c) 2010-2011 The Regents of the University of California
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * Kernel interface for event/notification delivery and preemption. */
6
7 #ifndef ROS_INC_EVENT_H
8 #define ROS_INC_EVENT_H
9
10 #include <ros/common.h>
11 #include <ros/atomic.h>
12 #include <ros/arch/trapframe.h>
13 /* #include <ros/ucq.h> included below */
14
15 /* Event Delivery Flags from the process to the kernel */
16 #define EVENT_IPI                               0x001   /* IPI the vcore (usually with INDIR) */
17 #define EVENT_INDIR                             0x002   /* send an indirection event to vcore */
18 #define EVENT_FALLBACK                  0x004   /* pick another vcore if it's offline */
19 #define EVENT_NOMSG                             0x008   /* just send the bit, not the msg */
20 #define EVENT_NOTHROTTLE                0x010   /* send all alerts (no throttling) */
21 #define EVENT_VCORE_MUST_RUN    0x020   /* Alerts go to a vcore that will run */
22 /* Not seriously used flags */
23 #define EVENT_ROUNDROBIN                0x040   /* pick a vcore, RR style */
24 #define EVENT_VCORE_APPRO               0x080   /* send to where the kernel wants */
25 #define EVENT_VCORE_PRIVATE             0x100   /* Will go to the private VCPD mbox */
26
27 /* Flags from the program to the 2LS */
28 #define EVENT_JUSTHANDLEIT              0x200   /* 2LS should handle the ev_q */
29 #define EVENT_THREAD                    0x400   /* spawn thread to handle ev_q */
30
31 /* Certain event flags apply to spam/fallback messages */
32 #define EVENT_SPAM_FLAGS                (EVENT_IPI | EVENT_VCORE_MUST_RUN)
33
34 /* Event Message Types */
35 #define EV_NONE                                  0
36 #define EV_PREEMPT_PENDING               1
37 #define EV_GANG_PREMPT_PENDING   2
38 #define EV_VCORE_PREEMPT                 3
39 #define EV_GANG_RETURN                   4
40 #define EV_USER_IPI                              5
41 #define EV_PAGE_FAULT                    6
42 #define EV_ALARM                                 7
43 #define EV_EVENT                                 8
44 #define EV_FREE_APPLE_PIE                9
45 #define EV_SYSCALL                              10
46 #define EV_CHECK_MSGS                   11
47 #define NR_EVENT_TYPES                  12 /* keep me last */
48
49 /* Will probably have dynamic notifications later */
50 #define MAX_NR_DYN_EVENT                25
51 #define MAX_NR_EVENT                    (NR_EVENT_TYPES + MAX_NR_DYN_EVENT)
52
53 /* Want to keep this small and generic, but add items as you need them.  One
54  * item some will need is an expiration time, which ought to be put in the 64
55  * bit arg.  Will need tweaking / thought as we come up with events.  These are
56  * what get put on the per-core queue in procdata. */
57 struct event_msg {
58         uint16_t                                        ev_type;
59         uint16_t                                        ev_arg1;
60         uint32_t                                        ev_arg2;
61         void                                            *ev_arg3;
62         uint64_t                                        ev_arg4;
63 };
64
65 /* Including here since ucq.h needs to know about struct event_msg */
66 #include <ros/ucq.h>
67
68 /* Structure for storing / receiving event messages.  An overflow causes the
69  * bit of the event to get set in the bitmap.  You can also have just the bit
70  * sent (and no message). */
71 struct event_mbox {
72         struct ucq                                      ev_msgs;
73         bool                                            ev_check_bits;
74         uint8_t                                         ev_bitmap[(MAX_NR_EVENT - 1) / 8 + 1];
75 };
76
77 /* The kernel sends messages to this structure, which describes how and where
78  * to receive messages, including optional IPIs. */
79 struct event_queue {
80         struct event_mbox                       *ev_mbox;
81         int                                                     ev_flags;
82         bool                                            ev_alert_pending;
83         uint32_t                                        ev_vcore;
84         void                                            (*ev_handler)(struct event_queue *);
85 };
86
87 /* Big version, contains storage space for the ev_mbox.  Never access the
88  * internal mbox directly. */
89 struct event_queue_big {
90         struct event_mbox                       *ev_mbox;
91         int                                                     ev_flags;
92         uint32_t                                        ev_vcore;
93         void                                            (*ev_handler)(struct event_queue *);
94         struct event_mbox                       ev_imbox;
95 };
96
97 /* Vcore state flags.  K_LOCK means the kernel is writing */
98 #define VC_K_LOCK                               0x001                           /* CASing with the kernel */
99 #define VC_PREEMPTED                    0x002                           /* VC is preempted */
100 #define VC_RECOVERING                   0x004                           /* VC being recovered */
101 #define VC_UTHREAD_STEALING             0x008                           /* Uthread being stolen */
102
103 /* Per-core data about preemptions and notifications */
104 struct preempt_data {
105         struct user_trapframe           preempt_tf;                     /* slot for vcore ctx */
106         struct ancillary_state          preempt_anc;
107         struct user_trapframe           notif_tf;                       /* slot for uthread ctx */
108         uintptr_t                                       transition_stack;       /* advertised by the user */
109         atomic_t                                        flags;
110         bool                                            notif_disabled;         /* vcore unwilling to recv*/
111         bool                                            notif_pending;          /* notif k_msg on the way */
112         bool                                            can_rcv_msg;            /* can receive FALLBACK */
113         struct event_mbox                       ev_mbox_public;         /* can be read remotely */
114         struct event_mbox                       ev_mbox_private;        /* for this vcore only */
115 };
116
117 #endif /* ROS_INC_EVENT_H */