xref: /openbmc/linux/arch/um/kernel/time.c (revision e52a6321)
1 /*
2  * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4  * Copyright (C) 2012-2014 Cisco Systems
5  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6  * Licensed under the GPL
7  */
8 
9 #include <linux/clockchips.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/jiffies.h>
13 #include <linux/mm.h>
14 #include <linux/sched.h>
15 #include <linux/spinlock.h>
16 #include <linux/threads.h>
17 #include <asm/irq.h>
18 #include <asm/param.h>
19 #include <kern_util.h>
20 #include <os.h>
21 #include <timer-internal.h>
22 #include <shared/init.h>
23 
24 #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
25 enum time_travel_mode time_travel_mode;
26 unsigned long long time_travel_time;
27 enum time_travel_timer_mode time_travel_timer_mode;
28 unsigned long long time_travel_timer_expiry;
29 unsigned long long time_travel_timer_interval;
30 
31 static bool time_travel_start_set;
32 static unsigned long long time_travel_start;
33 #else
34 #define time_travel_start_set 0
35 #define time_travel_start 0
36 #endif
37 
38 void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
39 {
40 	unsigned long flags;
41 
42 	if (time_travel_mode != TT_MODE_OFF)
43 		time_travel_set_time(time_travel_timer_expiry);
44 
45 	local_irq_save(flags);
46 	do_IRQ(TIMER_IRQ, regs);
47 	local_irq_restore(flags);
48 }
49 
50 static int itimer_shutdown(struct clock_event_device *evt)
51 {
52 	if (time_travel_mode != TT_MODE_OFF)
53 		time_travel_set_timer(TT_TMR_DISABLED, 0);
54 
55 	if (time_travel_mode != TT_MODE_INFCPU)
56 		os_timer_disable();
57 
58 	return 0;
59 }
60 
61 static int itimer_set_periodic(struct clock_event_device *evt)
62 {
63 	unsigned long long interval = NSEC_PER_SEC / HZ;
64 
65 	if (time_travel_mode != TT_MODE_OFF)
66 		time_travel_set_timer(TT_TMR_PERIODIC,
67 				      time_travel_time + interval);
68 
69 	if (time_travel_mode != TT_MODE_INFCPU)
70 		os_timer_set_interval(interval);
71 
72 	return 0;
73 }
74 
75 static int itimer_next_event(unsigned long delta,
76 			     struct clock_event_device *evt)
77 {
78 	delta += 1;
79 
80 	if (time_travel_mode != TT_MODE_OFF)
81 		time_travel_set_timer(TT_TMR_ONESHOT,
82 				      time_travel_time + delta);
83 
84 	if (time_travel_mode != TT_MODE_INFCPU)
85 		return os_timer_one_shot(delta);
86 
87 	return 0;
88 }
89 
90 static int itimer_one_shot(struct clock_event_device *evt)
91 {
92 	return itimer_next_event(0, evt);
93 }
94 
95 static struct clock_event_device timer_clockevent = {
96 	.name			= "posix-timer",
97 	.rating			= 250,
98 	.cpumask		= cpu_possible_mask,
99 	.features		= CLOCK_EVT_FEAT_PERIODIC |
100 				  CLOCK_EVT_FEAT_ONESHOT,
101 	.set_state_shutdown	= itimer_shutdown,
102 	.set_state_periodic	= itimer_set_periodic,
103 	.set_state_oneshot	= itimer_one_shot,
104 	.set_next_event		= itimer_next_event,
105 	.shift			= 0,
106 	.max_delta_ns		= 0xffffffff,
107 	.max_delta_ticks	= 0xffffffff,
108 	.min_delta_ns		= TIMER_MIN_DELTA,
109 	.min_delta_ticks	= TIMER_MIN_DELTA, // microsecond resolution should be enough for anyone, same as 640K RAM
110 	.irq			= 0,
111 	.mult			= 1,
112 };
113 
114 static irqreturn_t um_timer(int irq, void *dev)
115 {
116 	if (get_current()->mm != NULL)
117 	{
118         /* userspace - relay signal, results in correct userspace timers */
119 		os_alarm_process(get_current()->mm->context.id.u.pid);
120 	}
121 
122 	(*timer_clockevent.event_handler)(&timer_clockevent);
123 
124 	return IRQ_HANDLED;
125 }
126 
127 static u64 timer_read(struct clocksource *cs)
128 {
129 	if (time_travel_mode != TT_MODE_OFF) {
130 		/*
131 		 * We make reading the timer cost a bit so that we don't get
132 		 * stuck in loops that expect time to move more than the
133 		 * exact requested sleep amount, e.g. python's socket server,
134 		 * see https://bugs.python.org/issue37026.
135 		 */
136 		time_travel_set_time(time_travel_time + TIMER_MULTIPLIER);
137 		return time_travel_time / TIMER_MULTIPLIER;
138 	}
139 
140 	return os_nsecs() / TIMER_MULTIPLIER;
141 }
142 
143 static struct clocksource timer_clocksource = {
144 	.name		= "timer",
145 	.rating		= 300,
146 	.read		= timer_read,
147 	.mask		= CLOCKSOURCE_MASK(64),
148 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
149 };
150 
151 static void __init um_timer_setup(void)
152 {
153 	int err;
154 
155 	err = request_irq(TIMER_IRQ, um_timer, IRQF_TIMER, "hr timer", NULL);
156 	if (err != 0)
157 		printk(KERN_ERR "register_timer : request_irq failed - "
158 		       "errno = %d\n", -err);
159 
160 	err = os_timer_create();
161 	if (err != 0) {
162 		printk(KERN_ERR "creation of timer failed - errno = %d\n", -err);
163 		return;
164 	}
165 
166 	err = clocksource_register_hz(&timer_clocksource, NSEC_PER_SEC/TIMER_MULTIPLIER);
167 	if (err) {
168 		printk(KERN_ERR "clocksource_register_hz returned %d\n", err);
169 		return;
170 	}
171 	clockevents_register_device(&timer_clockevent);
172 }
173 
174 void read_persistent_clock64(struct timespec64 *ts)
175 {
176 	long long nsecs;
177 
178 	if (time_travel_start_set)
179 		nsecs = time_travel_start + time_travel_time;
180 	else
181 		nsecs = os_persistent_clock_emulation();
182 
183 	set_normalized_timespec64(ts, nsecs / NSEC_PER_SEC,
184 				  nsecs % NSEC_PER_SEC);
185 }
186 
187 void __init time_init(void)
188 {
189 	timer_set_signal_handler();
190 	late_time_init = um_timer_setup;
191 }
192 
193 #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
194 unsigned long calibrate_delay_is_known(void)
195 {
196 	if (time_travel_mode == TT_MODE_INFCPU)
197 		return 1;
198 	return 0;
199 }
200 
201 int setup_time_travel(char *str)
202 {
203 	if (strcmp(str, "=inf-cpu") == 0) {
204 		time_travel_mode = TT_MODE_INFCPU;
205 		timer_clockevent.name = "time-travel-timer-infcpu";
206 		timer_clocksource.name = "time-travel-clock";
207 		return 1;
208 	}
209 
210 	if (!*str) {
211 		time_travel_mode = TT_MODE_BASIC;
212 		timer_clockevent.name = "time-travel-timer";
213 		timer_clocksource.name = "time-travel-clock";
214 		return 1;
215 	}
216 
217 	return -EINVAL;
218 }
219 
220 __setup("time-travel", setup_time_travel);
221 __uml_help(setup_time_travel,
222 "time-travel\n"
223 "This option just enables basic time travel mode, in which the clock/timers\n"
224 "inside the UML instance skip forward when there's nothing to do, rather than\n"
225 "waiting for real time to elapse. However, instance CPU speed is limited by\n"
226 "the real CPU speed, so e.g. a 10ms timer will always fire after ~10ms wall\n"
227 "clock (but quicker when there's nothing to do).\n"
228 "\n"
229 "time-travel=inf-cpu\n"
230 "This enables time travel mode with infinite processing power, in which there\n"
231 "are no wall clock timers, and any CPU processing happens - as seen from the\n"
232 "guest - instantly. This can be useful for accurate simulation regardless of\n"
233 "debug overhead, physical CPU speed, etc. but is somewhat dangerous as it can\n"
234 "easily lead to getting stuck (e.g. if anything in the system busy loops).\n");
235 
236 int setup_time_travel_start(char *str)
237 {
238 	int err;
239 
240 	err = kstrtoull(str, 0, &time_travel_start);
241 	if (err)
242 		return err;
243 
244 	time_travel_start_set = 1;
245 	return 1;
246 }
247 
248 __setup("time-travel-start", setup_time_travel_start);
249 __uml_help(setup_time_travel_start,
250 "time-travel-start=<seconds>\n"
251 "Configure the UML instance's wall clock to start at this value rather than\n"
252 "the host's wall clock at the time of UML boot.\n");
253 #endif
254