1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ftrace.h>
3 #include <linux/tracepoint.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/rv.h>
8 #include <rv/instrumentation.h>
9 #include <rv/da_monitor.h>
10 
11 #define MODULE_NAME "tss"
12 
13 #include <trace/events/sched.h>
14 #include <rv_trace.h>
15 #include <monitors/sched/sched.h>
16 
17 #include "tss.h"
18 
19 static struct rv_monitor rv_tss;
20 DECLARE_DA_MON_PER_CPU(tss, unsigned char);
21 
22 static void handle_sched_switch(void *data, bool preempt,
23 				struct task_struct *prev,
24 				struct task_struct *next,
25 				unsigned int prev_state)
26 {
27 	da_handle_event_tss(sched_switch_tss);
28 }
29 
30 static void handle_schedule_entry(void *data, bool preempt, unsigned long ip)
31 {
32 	da_handle_event_tss(schedule_entry_tss);
33 }
34 
35 static void handle_schedule_exit(void *data, bool is_switch, unsigned long ip)
36 {
37 	da_handle_start_event_tss(schedule_exit_tss);
38 }
39 
40 static int enable_tss(void)
41 {
42 	int retval;
43 
44 	retval = da_monitor_init_tss();
45 	if (retval)
46 		return retval;
47 
48 	rv_attach_trace_probe("tss", sched_switch, handle_sched_switch);
49 	rv_attach_trace_probe("tss", sched_entry_tp, handle_schedule_entry);
50 	rv_attach_trace_probe("tss", sched_exit_tp, handle_schedule_exit);
51 
52 	return 0;
53 }
54 
55 static void disable_tss(void)
56 {
57 	rv_tss.enabled = 0;
58 
59 	rv_detach_trace_probe("tss", sched_switch, handle_sched_switch);
60 	rv_detach_trace_probe("tss", sched_entry_tp, handle_schedule_entry);
61 	rv_detach_trace_probe("tss", sched_exit_tp, handle_schedule_exit);
62 
63 	da_monitor_destroy_tss();
64 }
65 
66 static struct rv_monitor rv_tss = {
67 	.name = "tss",
68 	.description = "task switch while scheduling.",
69 	.enable = enable_tss,
70 	.disable = disable_tss,
71 	.reset = da_monitor_reset_all_tss,
72 	.enabled = 0,
73 };
74 
75 static int __init register_tss(void)
76 {
77 	rv_register_monitor(&rv_tss, &rv_sched);
78 	return 0;
79 }
80 
81 static void __exit unregister_tss(void)
82 {
83 	rv_unregister_monitor(&rv_tss);
84 }
85 
86 module_init(register_tss);
87 module_exit(unregister_tss);
88 
89 MODULE_LICENSE("GPL");
90 MODULE_AUTHOR("Gabriele Monaco <gmonaco@redhat.com>");
91 MODULE_DESCRIPTION("tss: task switch while scheduling.");
92