hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
/*
 * SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2016 Philippe Gerum  <rpm@xenomai.org>.
 */
#ifndef _LINUX_DOVETAIL_H
#define _LINUX_DOVETAIL_H
 
#ifdef CONFIG_DOVETAIL
 
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/thread_info.h>
#include <linux/irqstage.h>
#include <uapi/asm-generic/dovetail.h>
#include <asm/dovetail.h>
 
struct pt_regs;
struct task_struct;
struct file;
struct files_struct;
 
enum inband_event_type {
   INBAND_TASK_SIGNAL,
   INBAND_TASK_MIGRATION,
   INBAND_TASK_EXIT,
   INBAND_TASK_RETUSER,
   INBAND_TASK_PTSTEP,
   INBAND_TASK_PTSTOP,
   INBAND_TASK_PTCONT,
   INBAND_PROCESS_CLEANUP,
};
 
struct dovetail_migration_data {
   struct task_struct *task;
   int dest_cpu;
};
 
struct dovetail_altsched_context {
   struct task_struct *task;
   struct mm_struct *active_mm;
   bool borrowed_mm;
};
 
#define protect_inband_mm(__flags)            \
   do {                        \
       (__flags) = hard_cond_local_irq_save();    \
       barrier();                \
   } while (0)                    \
 
#define unprotect_inband_mm(__flags)            \
   do {                        \
       barrier();                \
       hard_cond_local_irq_restore(__flags);    \
   } while (0)                    \
 
void inband_task_init(struct task_struct *p);
 
int pipeline_syscall(unsigned int nr, struct pt_regs *regs);
 
void __oob_trap_notify(unsigned int exception,
              struct pt_regs *regs);
 
static __always_inline void oob_trap_notify(unsigned int exception,
                   struct pt_regs *regs)
{
   if (running_oob() && !test_thread_local_flags(_TLF_OOBTRAP))
       __oob_trap_notify(exception, regs);
}
 
void __oob_trap_unwind(unsigned int exception,
       struct pt_regs *regs);
 
static __always_inline void oob_trap_unwind(unsigned int exception,
                   struct pt_regs *regs)
{
   if (test_thread_local_flags(_TLF_OOBTRAP))
       __oob_trap_unwind(exception, regs);
}
 
void inband_event_notify(enum inband_event_type,
            void *data);
 
void inband_clock_was_set(void);
 
static inline void inband_signal_notify(struct task_struct *p)
{
   if (test_ti_local_flags(task_thread_info(p), _TLF_DOVETAIL))
       inband_event_notify(INBAND_TASK_SIGNAL, p);
}
 
static inline void inband_migration_notify(struct task_struct *p, int cpu)
{
   if (test_ti_local_flags(task_thread_info(p), _TLF_DOVETAIL)) {
       struct dovetail_migration_data d = {
           .task = p,
           .dest_cpu = cpu,
       };
       inband_event_notify(INBAND_TASK_MIGRATION, &d);
   }
}
 
static inline void inband_exit_notify(void)
{
   inband_event_notify(INBAND_TASK_EXIT, NULL);
}
 
static inline void inband_cleanup_notify(struct mm_struct *mm)
{
   /*
    * Notify regardless of _TLF_DOVETAIL: current may have
    * resources to clean up although it might not be interested
    * in other kernel events.
    */
   inband_event_notify(INBAND_PROCESS_CLEANUP, mm);
}
 
static inline void inband_ptstop_notify(void)
{
   if (test_thread_local_flags(_TLF_DOVETAIL))
       inband_event_notify(INBAND_TASK_PTSTOP, current);
}
 
static inline void inband_ptcont_notify(void)
{
   if (test_thread_local_flags(_TLF_DOVETAIL))
       inband_event_notify(INBAND_TASK_PTCONT, current);
}
 
static inline void inband_ptstep_notify(struct task_struct *tracee)
{
   if (test_ti_local_flags(task_thread_info(tracee), _TLF_DOVETAIL))
       inband_event_notify(INBAND_TASK_PTSTEP, tracee);
}
 
static inline
void prepare_inband_switch(struct task_struct *next)
{
   struct task_struct *prev = current;
 
   if (test_ti_local_flags(task_thread_info(next), _TLF_DOVETAIL))
       __this_cpu_write(irq_pipeline.rqlock_owner, prev);
}
 
void inband_retuser_notify(void);
 
bool inband_switch_tail(void);
 
void oob_trampoline(void);
 
void arch_inband_task_init(struct task_struct *p);
 
int dovetail_start(void);
 
void dovetail_stop(void);
 
void dovetail_init_altsched(struct dovetail_altsched_context *p);
 
void dovetail_start_altsched(void);
 
void dovetail_stop_altsched(void);
 
__must_check int dovetail_leave_inband(void);
 
static inline void dovetail_leave_oob(void)
{
   clear_thread_local_flags(_TLF_OOB|_TLF_OFFSTAGE);
   clear_thread_flag(TIF_MAYDAY);
}
 
void dovetail_resume_inband(void);
 
bool dovetail_context_switch(struct dovetail_altsched_context *out,
           struct dovetail_altsched_context *in,
           bool leave_inband);
 
static inline
struct oob_thread_state *dovetail_current_state(void)
{
   return &current_thread_info()->oob_state;
}
 
static inline
struct oob_thread_state *dovetail_task_state(struct task_struct *p)
{
   return &task_thread_info(p)->oob_state;
}
 
static inline
struct oob_mm_state *dovetail_mm_state(void)
{
   if (current->flags & PF_KTHREAD)
       return NULL;
 
   return &current->mm->oob_state;
}
 
void dovetail_call_mayday(struct pt_regs *regs);
 
static inline void dovetail_send_mayday(struct task_struct *castaway)
{
   struct thread_info *ti = task_thread_info(castaway);
 
   if (test_ti_local_flags(ti, _TLF_DOVETAIL))
       set_ti_thread_flag(ti, TIF_MAYDAY);
}
 
static inline void dovetail_request_ucall(struct task_struct *task)
{
   struct thread_info *ti = task_thread_info(task);
 
   if (test_ti_local_flags(ti, _TLF_DOVETAIL))
       set_ti_thread_flag(ti, TIF_RETUSER);
}
 
static inline void dovetail_clear_ucall(void)
{
   if (test_thread_flag(TIF_RETUSER))
       clear_thread_flag(TIF_RETUSER);
}
 
void install_inband_fd(unsigned int fd, struct file *file,
              struct files_struct *files);
 
void uninstall_inband_fd(unsigned int fd, struct file *file,
            struct files_struct *files);
 
void replace_inband_fd(unsigned int fd, struct file *file,
              struct files_struct *files);
 
#else    /* !CONFIG_DOVETAIL */
 
/* We may have arch-specific placeholders. */
#include <asm/dovetail.h>
 
struct files_struct;
 
#define protect_inband_mm(__flags)    \
   do { (void)(__flags); } while (0)
 
#define unprotect_inband_mm(__flags)    \
   do { (void)(__flags); } while (0)
 
static inline
void inband_task_init(struct task_struct *p) { }
 
static inline void arch_dovetail_exec_prepare(void)
{ }
 
/*
 * Keep the trap helpers as macros, we might not be able to resolve
 * trap numbers if CONFIG_DOVETAIL is off.
 */
#define oob_trap_notify(__exception, __regs)    do { } while (0)
#define oob_trap_unwind(__exception, __regs)    do { } while (0)
 
static inline
int pipeline_syscall(unsigned int nr, struct pt_regs *regs)
{
   return 0;
}
 
static inline void inband_signal_notify(struct task_struct *p) { }
 
static inline
void inband_migration_notify(struct task_struct *p, int cpu) { }
 
static inline void inband_exit_notify(void) { }
 
static inline void inband_cleanup_notify(struct mm_struct *mm) { }
 
static inline void inband_retuser_notify(void) { }
 
static inline void inband_ptstop_notify(void) { }
 
static inline void inband_ptcont_notify(void) { }
 
static inline void inband_ptstep_notify(struct task_struct *tracee) { }
 
static inline void oob_trampoline(void) { }
 
static inline void prepare_inband_switch(struct task_struct *next) { }
 
static inline bool inband_switch_tail(void)
{
   /* Matches converse disabling in prepare_task_switch(). */
   hard_cond_local_irq_enable();
   return false;
}
 
static inline void dovetail_request_ucall(struct task_struct *task) { }
 
static inline void dovetail_clear_ucall(void) { }
 
static inline void inband_clock_was_set(void) { }
 
static inline
void install_inband_fd(unsigned int fd, struct file *file,
              struct files_struct *files) { }
 
static inline
void uninstall_inband_fd(unsigned int fd, struct file *file,
            struct files_struct *files) { }
 
static inline
void replace_inband_fd(unsigned int fd, struct file *file,
              struct files_struct *files) { }
 
#endif    /* !CONFIG_DOVETAIL */
 
static __always_inline bool dovetailing(void)
{
   return IS_ENABLED(CONFIG_DOVETAIL);
}
 
static __always_inline bool dovetail_debug(void)
{
   return IS_ENABLED(CONFIG_DEBUG_DOVETAIL);
}
 
#ifndef arch_dovetail_is_syscall
#define arch_dovetail_is_syscall(__nr)    ((__nr) == __NR_prctl)
#endif
 
#endif /* _LINUX_DOVETAIL_H */