Implemented the new profiler
[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 #pragma once
8
9 #include <ros/common.h>
10 #include <ros/atomic.h>
11 #include <ros/trapframe.h>
12 /* #include <ros/ucq.h> included below */
13 /* #include <ros/evbitmap.h> included below */
14
15 /* Event Delivery Flags from the process to the kernel */
16 #define EVENT_IPI                               0x00001 /* IPI the vcore (usually with INDIR) */
17 #define EVENT_SPAM_PUBLIC               0x00002 /* spam the msg to public vcpd mboxes */
18 #define EVENT_INDIR                             0x00004 /* send an indirection event to vcore */
19 #define EVENT_VCORE_PRIVATE             0x00008 /* Will go to the private VCPD mbox */
20 #define EVENT_SPAM_INDIR                0x00010 /* spam INDIRs if the vcore's offline */
21 #define EVENT_VCORE_MUST_RUN    0x00020 /* spams go to a vcore that will run */
22 #define EVENT_NOTHROTTLE                0x00040 /* send all INDIRs (no throttling) */
23 #define EVENT_ROUNDROBIN                0x00080 /* pick a vcore, RR style */
24 #define EVENT_VCORE_APPRO               0x00100 /* send to where the kernel wants */
25 #define EVENT_WAKEUP                    0x00200 /* wake up the process after sending */
26
27 /* Event Message Types */
28 #define EV_NONE                                  0
29 #define EV_PREEMPT_PENDING               1
30 #define EV_GANG_PREMPT_PENDING   2
31 #define EV_VCORE_PREEMPT                 3
32 #define EV_GANG_RETURN                   4
33 #define EV_USER_IPI                              5
34 #define EV_PAGE_FAULT                    6
35 #define EV_ALARM                                 7
36 #define EV_EVENT                                 8
37 #define EV_FREE_APPLE_PIE                9
38 #define EV_SYSCALL                              10
39 #define EV_CHECK_MSGS                   11
40 #define EV_POSIX_SIGNAL                 12
41 #define NR_EVENT_TYPES                  25 /* keep me last (and 1 > the last one) */
42
43 /* Will probably have dynamic notifications later */
44 #define MAX_NR_DYN_EVENT                25
45 #define MAX_NR_EVENT                    (NR_EVENT_TYPES + MAX_NR_DYN_EVENT)
46
47 /* Want to keep this small and generic, but add items as you need them.  One
48  * item some will need is an expiration time, which ought to be put in the 64
49  * bit arg.  Will need tweaking / thought as we come up with events.  These are
50  * what get put on the per-core queue in procdata. */
51 struct event_msg {
52         uint16_t                                        ev_type;
53         uint16_t                                        ev_arg1;
54         uint32_t                                        ev_arg2;
55         void                                            *ev_arg3;
56         uint64_t                                        ev_arg4;
57 };
58
59 /* Include here since the mboxes need to know event.h basics (e.g. event_msg) */
60 #include <ros/ucq.h>
61 #include <ros/evbitmap.h>
62 #include <ros/ceq.h>
63
64 #define EV_MBOX_UCQ                             1
65 #define EV_MBOX_BITMAP                  2
66 #define EV_MBOX_CEQ                             3
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         int                                             type;
73         union {
74                 struct ucq                              ucq;
75                 struct evbitmap                 evbm;
76                 struct ceq                              ceq;
77         };
78 };
79
80 /* The kernel sends messages to this structure, which describes how and where
81  * to receive messages, including optional IPIs. */
82 struct event_queue {
83         struct event_mbox                       *ev_mbox;
84         int                                                     ev_flags;
85         bool                                            ev_alert_pending;
86         uint32_t                                        ev_vcore;
87         void                                            (*ev_handler)(struct event_queue *);
88         void                                            *ev_udata;
89 };
90
91 /* Big version, contains storage space for the ev_mbox.  Never access the
92  * internal mbox directly. */
93 struct event_queue_big {
94         struct event_mbox                       *ev_mbox;
95         int                                                     ev_flags;
96         bool                                            ev_alert_pending;
97         uint32_t                                        ev_vcore;
98         void                                            (*ev_handler)(struct event_queue *);
99         void                                            *ev_udata;
100         struct event_mbox                       ev_imbox;
101 };
102
103 /* Vcore state flags.  K_LOCK means the kernel is writing */
104 #define VC_K_LOCK                               0x001                           /* CASing with the kernel */
105 #define VC_PREEMPTED                    0x002                           /* VC is preempted */
106 #define VC_CAN_RCV_MSG                  0x004                           /* someone will get msg */
107 #define VC_UTHREAD_STEALING             0x008                           /* Uthread being stolen */
108 #define VC_SCP_NOVCCTX                  0x010                           /* can't go into vc ctx */
109
110 /* Racy flags, where we don't need the atomics */
111 #define VC_FPU_SAVED                    0x1000                          /* valid FPU state in anc */
112
113 /* Per-core data about preemptions and notifications */
114 struct preempt_data {
115         struct user_context                     vcore_ctx;                      /* for preemptions */
116         struct ancillary_state          preempt_anc;
117         struct user_context                     uthread_ctx;            /* for preempts or notifs */
118         uintptr_t                                       vcore_entry;            /* advertised by the user */
119         uintptr_t                                       vcore_stack;            /* advertised by the user */
120         uintptr_t                                       vcore_tls_desc;         /* advertised by the user */
121         atomic_t                                        flags;
122         int                                                     rflags;                         /* racy flags */
123         bool                                            notif_disabled;         /* vcore unwilling to recv*/
124         bool                                            notif_pending;          /* notif k_msg on the way */
125         struct event_mbox                       ev_mbox_public;         /* can be read remotely */
126         struct event_mbox                       ev_mbox_private;        /* for this vcore only */
127 };