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