xref: /openbmc/linux/arch/um/kernel/time.c (revision f5cc14e4)
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  * Copyright (C) 2019 Intel Corporation
8  */
9 
10 #include <linux/clockchips.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/jiffies.h>
14 #include <linux/mm.h>
15 #include <linux/sched.h>
16 #include <linux/spinlock.h>
17 #include <linux/threads.h>
18 #include <asm/irq.h>
19 #include <asm/param.h>
20 #include <kern_util.h>
21 #include <os.h>
22 #include <linux/time-internal.h>
23 #include <linux/um_timetravel.h>
24 #include <shared/init.h>
25 
26 #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
27 enum time_travel_mode time_travel_mode;
28 EXPORT_SYMBOL_GPL(time_travel_mode);
29 
30 static bool time_travel_start_set;
31 static unsigned long long time_travel_start;
32 static unsigned long long time_travel_time;
33 static LIST_HEAD(time_travel_events);
34 static LIST_HEAD(time_travel_irqs);
35 static unsigned long long time_travel_timer_interval;
36 static unsigned long long time_travel_next_event;
37 static struct time_travel_event time_travel_timer_event;
38 static int time_travel_ext_fd = -1;
39 static unsigned int time_travel_ext_waiting;
40 static bool time_travel_ext_prev_request_valid;
41 static unsigned long long time_travel_ext_prev_request;
42 static bool time_travel_ext_free_until_valid;
43 static unsigned long long time_travel_ext_free_until;
44 
45 static void time_travel_set_time(unsigned long long ns)
46 {
47 	if (unlikely(ns < time_travel_time))
48 		panic("time-travel: time goes backwards %lld -> %lld\n",
49 		      time_travel_time, ns);
50 	else if (unlikely(ns >= S64_MAX))
51 		panic("The system was going to sleep forever, aborting");
52 
53 	time_travel_time = ns;
54 }
55 
56 enum time_travel_message_handling {
57 	TTMH_IDLE,
58 	TTMH_POLL,
59 	TTMH_READ,
60 };
61 
62 static void time_travel_handle_message(struct um_timetravel_msg *msg,
63 				       enum time_travel_message_handling mode)
64 {
65 	struct um_timetravel_msg resp = {
66 		.op = UM_TIMETRAVEL_ACK,
67 	};
68 	int ret;
69 
70 	/*
71 	 * Poll outside the locked section (if we're not called to only read
72 	 * the response) so we can get interrupts for e.g. virtio while we're
73 	 * here, but then we need to lock to not get interrupted between the
74 	 * read of the message and write of the ACK.
75 	 */
76 	if (mode != TTMH_READ) {
77 		bool disabled = irqs_disabled();
78 
79 		BUG_ON(mode == TTMH_IDLE && !disabled);
80 
81 		if (disabled)
82 			local_irq_enable();
83 		while (os_poll(1, &time_travel_ext_fd) != 0) {
84 			/* nothing */
85 		}
86 		if (disabled)
87 			local_irq_disable();
88 	}
89 
90 	ret = os_read_file(time_travel_ext_fd, msg, sizeof(*msg));
91 
92 	if (ret == 0)
93 		panic("time-travel external link is broken\n");
94 	if (ret != sizeof(*msg))
95 		panic("invalid time-travel message - %d bytes\n", ret);
96 
97 	switch (msg->op) {
98 	default:
99 		WARN_ONCE(1, "time-travel: unexpected message %lld\n",
100 			  (unsigned long long)msg->op);
101 		break;
102 	case UM_TIMETRAVEL_ACK:
103 		return;
104 	case UM_TIMETRAVEL_RUN:
105 		time_travel_set_time(msg->time);
106 		break;
107 	case UM_TIMETRAVEL_FREE_UNTIL:
108 		time_travel_ext_free_until_valid = true;
109 		time_travel_ext_free_until = msg->time;
110 		break;
111 	}
112 
113 	resp.seq = msg->seq;
114 	os_write_file(time_travel_ext_fd, &resp, sizeof(resp));
115 }
116 
117 static u64 time_travel_ext_req(u32 op, u64 time)
118 {
119 	static int seq;
120 	int mseq = ++seq;
121 	struct um_timetravel_msg msg = {
122 		.op = op,
123 		.time = time,
124 		.seq = mseq,
125 	};
126 	unsigned long flags;
127 
128 	/*
129 	 * We need to save interrupts here and only restore when we
130 	 * got the ACK - otherwise we can get interrupted and send
131 	 * another request while we're still waiting for an ACK, but
132 	 * the peer doesn't know we got interrupted and will send
133 	 * the ACKs in the same order as the message, but we'd need
134 	 * to see them in the opposite order ...
135 	 *
136 	 * This wouldn't matter *too* much, but some ACKs carry the
137 	 * current time (for UM_TIMETRAVEL_GET) and getting another
138 	 * ACK without a time would confuse us a lot!
139 	 *
140 	 * The sequence number assignment that happens here lets us
141 	 * debug such message handling issues more easily.
142 	 */
143 	local_irq_save(flags);
144 	os_write_file(time_travel_ext_fd, &msg, sizeof(msg));
145 
146 	while (msg.op != UM_TIMETRAVEL_ACK)
147 		time_travel_handle_message(&msg, TTMH_READ);
148 
149 	if (msg.seq != mseq)
150 		panic("time-travel: ACK message has different seqno! op=%d, seq=%d != %d time=%lld\n",
151 		      msg.op, msg.seq, mseq, msg.time);
152 
153 	if (op == UM_TIMETRAVEL_GET)
154 		time_travel_set_time(msg.time);
155 	local_irq_restore(flags);
156 
157 	return msg.time;
158 }
159 
160 void __time_travel_wait_readable(int fd)
161 {
162 	int fds[2] = { fd, time_travel_ext_fd };
163 	int ret;
164 
165 	if (time_travel_mode != TT_MODE_EXTERNAL)
166 		return;
167 
168 	while ((ret = os_poll(2, fds))) {
169 		struct um_timetravel_msg msg;
170 
171 		if (ret == 1)
172 			time_travel_handle_message(&msg, TTMH_READ);
173 	}
174 }
175 EXPORT_SYMBOL_GPL(__time_travel_wait_readable);
176 
177 static void time_travel_ext_update_request(unsigned long long time)
178 {
179 	if (time_travel_mode != TT_MODE_EXTERNAL)
180 		return;
181 
182 	/* asked for exactly this time previously */
183 	if (time_travel_ext_prev_request_valid &&
184 	    time == time_travel_ext_prev_request)
185 		return;
186 
187 	/*
188 	 * if we're running and are allowed to run past the request
189 	 * then we don't need to update it either
190 	 */
191 	if (!time_travel_ext_waiting && time_travel_ext_free_until_valid &&
192 	    time < time_travel_ext_free_until)
193 		return;
194 
195 	time_travel_ext_prev_request = time;
196 	time_travel_ext_prev_request_valid = true;
197 	time_travel_ext_req(UM_TIMETRAVEL_REQUEST, time);
198 }
199 
200 void __time_travel_propagate_time(void)
201 {
202 	static unsigned long long last_propagated;
203 
204 	if (last_propagated == time_travel_time)
205 		return;
206 
207 	time_travel_ext_req(UM_TIMETRAVEL_UPDATE, time_travel_time);
208 	last_propagated = time_travel_time;
209 }
210 EXPORT_SYMBOL_GPL(__time_travel_propagate_time);
211 
212 /* returns true if we must do a wait to the simtime device */
213 static bool time_travel_ext_request(unsigned long long time)
214 {
215 	/*
216 	 * If we received an external sync point ("free until") then we
217 	 * don't have to request/wait for anything until then, unless
218 	 * we're already waiting.
219 	 */
220 	if (!time_travel_ext_waiting && time_travel_ext_free_until_valid &&
221 	    time < time_travel_ext_free_until)
222 		return false;
223 
224 	time_travel_ext_update_request(time);
225 	return true;
226 }
227 
228 static void time_travel_ext_wait(bool idle)
229 {
230 	struct um_timetravel_msg msg = {
231 		.op = UM_TIMETRAVEL_ACK,
232 	};
233 
234 	time_travel_ext_prev_request_valid = false;
235 	time_travel_ext_free_until_valid = false;
236 	time_travel_ext_waiting++;
237 
238 	time_travel_ext_req(UM_TIMETRAVEL_WAIT, -1);
239 
240 	/*
241 	 * Here we are deep in the idle loop, so we have to break out of the
242 	 * kernel abstraction in a sense and implement this in terms of the
243 	 * UML system waiting on the VQ interrupt while sleeping, when we get
244 	 * the signal it'll call time_travel_ext_vq_notify_done() completing the
245 	 * call.
246 	 */
247 	while (msg.op != UM_TIMETRAVEL_RUN)
248 		time_travel_handle_message(&msg, idle ? TTMH_IDLE : TTMH_POLL);
249 
250 	time_travel_ext_waiting--;
251 
252 	/* we might request more stuff while polling - reset when we run */
253 	time_travel_ext_prev_request_valid = false;
254 }
255 
256 static void time_travel_ext_get_time(void)
257 {
258 	time_travel_ext_req(UM_TIMETRAVEL_GET, -1);
259 }
260 
261 static void __time_travel_update_time(unsigned long long ns, bool idle)
262 {
263 	if (time_travel_mode == TT_MODE_EXTERNAL && time_travel_ext_request(ns))
264 		time_travel_ext_wait(idle);
265 	else
266 		time_travel_set_time(ns);
267 }
268 
269 static struct time_travel_event *time_travel_first_event(void)
270 {
271 	return list_first_entry_or_null(&time_travel_events,
272 					struct time_travel_event,
273 					list);
274 }
275 
276 static void __time_travel_add_event(struct time_travel_event *e,
277 				    unsigned long long time)
278 {
279 	struct time_travel_event *tmp;
280 	bool inserted = false;
281 
282 	if (e->pending)
283 		return;
284 
285 	e->pending = true;
286 	e->time = time;
287 
288 	list_for_each_entry(tmp, &time_travel_events, list) {
289 		/*
290 		 * Add the new entry before one with higher time,
291 		 * or if they're equal and both on stack, because
292 		 * in that case we need to unwind the stack in the
293 		 * right order, and the later event (timer sleep
294 		 * or such) must be dequeued first.
295 		 */
296 		if ((tmp->time > e->time) ||
297 		    (tmp->time == e->time && tmp->onstack && e->onstack)) {
298 			list_add_tail(&e->list, &tmp->list);
299 			inserted = true;
300 			break;
301 		}
302 	}
303 
304 	if (!inserted)
305 		list_add_tail(&e->list, &time_travel_events);
306 
307 	tmp = time_travel_first_event();
308 	time_travel_ext_update_request(tmp->time);
309 	time_travel_next_event = tmp->time;
310 }
311 
312 static void time_travel_add_event(struct time_travel_event *e,
313 				  unsigned long long time)
314 {
315 	if (WARN_ON(!e->fn))
316 		return;
317 
318 	__time_travel_add_event(e, time);
319 }
320 
321 void time_travel_periodic_timer(struct time_travel_event *e)
322 {
323 	time_travel_add_event(&time_travel_timer_event,
324 			      time_travel_time + time_travel_timer_interval);
325 	deliver_alarm();
326 }
327 
328 void deliver_time_travel_irqs(void)
329 {
330 	struct time_travel_event *e;
331 	unsigned long flags;
332 
333 	/*
334 	 * Don't do anything for most cases. Note that because here we have
335 	 * to disable IRQs (and re-enable later) we'll actually recurse at
336 	 * the end of the function, so this is strictly necessary.
337 	 */
338 	if (likely(list_empty(&time_travel_irqs)))
339 		return;
340 
341 	local_irq_save(flags);
342 	irq_enter();
343 	while ((e = list_first_entry_or_null(&time_travel_irqs,
344 					     struct time_travel_event,
345 					     list))) {
346 		WARN(e->time != time_travel_time,
347 		     "time moved from %lld to %lld before IRQ delivery\n",
348 		     time_travel_time, e->time);
349 		list_del(&e->list);
350 		e->pending = false;
351 		e->fn(e);
352 	}
353 	irq_exit();
354 	local_irq_restore(flags);
355 }
356 
357 static void time_travel_deliver_event(struct time_travel_event *e)
358 {
359 	if (e == &time_travel_timer_event) {
360 		/*
361 		 * deliver_alarm() does the irq_enter/irq_exit
362 		 * by itself, so must handle it specially here
363 		 */
364 		e->fn(e);
365 	} else if (irqs_disabled()) {
366 		list_add_tail(&e->list, &time_travel_irqs);
367 		/*
368 		 * set pending again, it was set to false when the
369 		 * event was deleted from the original list, but
370 		 * now it's still pending until we deliver the IRQ.
371 		 */
372 		e->pending = true;
373 	} else {
374 		unsigned long flags;
375 
376 		local_irq_save(flags);
377 		irq_enter();
378 		e->fn(e);
379 		irq_exit();
380 		local_irq_restore(flags);
381 	}
382 }
383 
384 static bool time_travel_del_event(struct time_travel_event *e)
385 {
386 	if (!e->pending)
387 		return false;
388 	list_del(&e->list);
389 	e->pending = false;
390 	return true;
391 }
392 
393 static void time_travel_update_time(unsigned long long next, bool idle)
394 {
395 	struct time_travel_event ne = {
396 		.onstack = true,
397 	};
398 	struct time_travel_event *e;
399 	bool finished = idle;
400 
401 	/* add it without a handler - we deal with that specifically below */
402 	__time_travel_add_event(&ne, next);
403 
404 	do {
405 		e = time_travel_first_event();
406 
407 		BUG_ON(!e);
408 		__time_travel_update_time(e->time, idle);
409 
410 		/* new events may have been inserted while we were waiting */
411 		if (e == time_travel_first_event()) {
412 			BUG_ON(!time_travel_del_event(e));
413 			BUG_ON(time_travel_time != e->time);
414 
415 			if (e == &ne) {
416 				finished = true;
417 			} else {
418 				if (e->onstack)
419 					panic("On-stack event dequeued outside of the stack! time=%lld, event time=%lld, event=%pS\n",
420 					      time_travel_time, e->time, e);
421 				time_travel_deliver_event(e);
422 			}
423 		}
424 
425 		e = time_travel_first_event();
426 		if (e)
427 			time_travel_ext_update_request(e->time);
428 	} while (ne.pending && !finished);
429 
430 	time_travel_del_event(&ne);
431 }
432 
433 void time_travel_ndelay(unsigned long nsec)
434 {
435 	time_travel_update_time(time_travel_time + nsec, false);
436 }
437 EXPORT_SYMBOL(time_travel_ndelay);
438 
439 void time_travel_add_irq_event(struct time_travel_event *e)
440 {
441 	BUG_ON(time_travel_mode != TT_MODE_EXTERNAL);
442 
443 	time_travel_ext_get_time();
444 	/*
445 	 * We could model interrupt latency here, for now just
446 	 * don't have any latency at all and request the exact
447 	 * same time (again) to run the interrupt...
448 	 */
449 	time_travel_add_event(e, time_travel_time);
450 }
451 EXPORT_SYMBOL_GPL(time_travel_add_irq_event);
452 
453 static void time_travel_oneshot_timer(struct time_travel_event *e)
454 {
455 	deliver_alarm();
456 }
457 
458 void time_travel_sleep(void)
459 {
460 	/*
461 	 * Wait "forever" (using S64_MAX because there are some potential
462 	 * wrapping issues, especially with the current TT_MODE_EXTERNAL
463 	 * controller application.
464 	 */
465 	unsigned long long next = S64_MAX;
466 
467 	if (time_travel_mode == TT_MODE_BASIC)
468 		os_timer_disable();
469 
470 	time_travel_update_time(next, true);
471 
472 	if (time_travel_mode == TT_MODE_BASIC &&
473 	    time_travel_timer_event.pending) {
474 		if (time_travel_timer_event.fn == time_travel_periodic_timer) {
475 			/*
476 			 * This is somewhat wrong - we should get the first
477 			 * one sooner like the os_timer_one_shot() below...
478 			 */
479 			os_timer_set_interval(time_travel_timer_interval);
480 		} else {
481 			os_timer_one_shot(time_travel_timer_event.time - next);
482 		}
483 	}
484 }
485 
486 static void time_travel_handle_real_alarm(void)
487 {
488 	time_travel_set_time(time_travel_next_event);
489 
490 	time_travel_del_event(&time_travel_timer_event);
491 
492 	if (time_travel_timer_event.fn == time_travel_periodic_timer)
493 		time_travel_add_event(&time_travel_timer_event,
494 				      time_travel_time +
495 				      time_travel_timer_interval);
496 }
497 
498 static void time_travel_set_interval(unsigned long long interval)
499 {
500 	time_travel_timer_interval = interval;
501 }
502 
503 static int time_travel_connect_external(const char *socket)
504 {
505 	const char *sep;
506 	unsigned long long id = (unsigned long long)-1;
507 	int rc;
508 
509 	if ((sep = strchr(socket, ':'))) {
510 		char buf[25] = {};
511 		if (sep - socket > sizeof(buf) - 1)
512 			goto invalid_number;
513 
514 		memcpy(buf, socket, sep - socket);
515 		if (kstrtoull(buf, 0, &id)) {
516 invalid_number:
517 			panic("time-travel: invalid external ID in string '%s'\n",
518 			      socket);
519 			return -EINVAL;
520 		}
521 
522 		socket = sep + 1;
523 	}
524 
525 	rc = os_connect_socket(socket);
526 	if (rc < 0) {
527 		panic("time-travel: failed to connect to external socket %s\n",
528 		      socket);
529 		return rc;
530 	}
531 
532 	time_travel_ext_fd = rc;
533 
534 	time_travel_ext_req(UM_TIMETRAVEL_START, id);
535 
536 	return 1;
537 }
538 #else /* CONFIG_UML_TIME_TRAVEL_SUPPORT */
539 #define time_travel_start_set 0
540 #define time_travel_start 0
541 #define time_travel_time 0
542 #define time_travel_ext_waiting 0
543 
544 static inline void time_travel_update_time(unsigned long long ns, bool retearly)
545 {
546 }
547 
548 static inline void time_travel_handle_real_alarm(void)
549 {
550 }
551 
552 static void time_travel_set_interval(unsigned long long interval)
553 {
554 }
555 
556 /* fail link if this actually gets used */
557 extern u64 time_travel_ext_req(u32 op, u64 time);
558 
559 /* these are empty macros so the struct/fn need not exist */
560 #define time_travel_add_event(e, time) do { } while (0)
561 #define time_travel_del_event(e) do { } while (0)
562 #endif
563 
564 void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
565 {
566 	unsigned long flags;
567 
568 	/*
569 	 * In basic time-travel mode we still get real interrupts
570 	 * (signals) but since we don't read time from the OS, we
571 	 * must update the simulated time here to the expiry when
572 	 * we get a signal.
573 	 * This is not the case in inf-cpu mode, since there we
574 	 * never get any real signals from the OS.
575 	 */
576 	if (time_travel_mode == TT_MODE_BASIC)
577 		time_travel_handle_real_alarm();
578 
579 	local_irq_save(flags);
580 	do_IRQ(TIMER_IRQ, regs);
581 	local_irq_restore(flags);
582 }
583 
584 static int itimer_shutdown(struct clock_event_device *evt)
585 {
586 	if (time_travel_mode != TT_MODE_OFF)
587 		time_travel_del_event(&time_travel_timer_event);
588 
589 	if (time_travel_mode != TT_MODE_INFCPU &&
590 	    time_travel_mode != TT_MODE_EXTERNAL)
591 		os_timer_disable();
592 
593 	return 0;
594 }
595 
596 static int itimer_set_periodic(struct clock_event_device *evt)
597 {
598 	unsigned long long interval = NSEC_PER_SEC / HZ;
599 
600 	if (time_travel_mode != TT_MODE_OFF) {
601 		time_travel_del_event(&time_travel_timer_event);
602 		time_travel_set_event_fn(&time_travel_timer_event,
603 					 time_travel_periodic_timer);
604 		time_travel_set_interval(interval);
605 		time_travel_add_event(&time_travel_timer_event,
606 				      time_travel_time + interval);
607 	}
608 
609 	if (time_travel_mode != TT_MODE_INFCPU &&
610 	    time_travel_mode != TT_MODE_EXTERNAL)
611 		os_timer_set_interval(interval);
612 
613 	return 0;
614 }
615 
616 static int itimer_next_event(unsigned long delta,
617 			     struct clock_event_device *evt)
618 {
619 	delta += 1;
620 
621 	if (time_travel_mode != TT_MODE_OFF) {
622 		time_travel_del_event(&time_travel_timer_event);
623 		time_travel_set_event_fn(&time_travel_timer_event,
624 					 time_travel_oneshot_timer);
625 		time_travel_add_event(&time_travel_timer_event,
626 				      time_travel_time + delta);
627 	}
628 
629 	if (time_travel_mode != TT_MODE_INFCPU &&
630 	    time_travel_mode != TT_MODE_EXTERNAL)
631 		return os_timer_one_shot(delta);
632 
633 	return 0;
634 }
635 
636 static int itimer_one_shot(struct clock_event_device *evt)
637 {
638 	return itimer_next_event(0, evt);
639 }
640 
641 static struct clock_event_device timer_clockevent = {
642 	.name			= "posix-timer",
643 	.rating			= 250,
644 	.cpumask		= cpu_possible_mask,
645 	.features		= CLOCK_EVT_FEAT_PERIODIC |
646 				  CLOCK_EVT_FEAT_ONESHOT,
647 	.set_state_shutdown	= itimer_shutdown,
648 	.set_state_periodic	= itimer_set_periodic,
649 	.set_state_oneshot	= itimer_one_shot,
650 	.set_next_event		= itimer_next_event,
651 	.shift			= 0,
652 	.max_delta_ns		= 0xffffffff,
653 	.max_delta_ticks	= 0xffffffff,
654 	.min_delta_ns		= TIMER_MIN_DELTA,
655 	.min_delta_ticks	= TIMER_MIN_DELTA, // microsecond resolution should be enough for anyone, same as 640K RAM
656 	.irq			= 0,
657 	.mult			= 1,
658 };
659 
660 static irqreturn_t um_timer(int irq, void *dev)
661 {
662 	if (get_current()->mm != NULL)
663 	{
664         /* userspace - relay signal, results in correct userspace timers */
665 		os_alarm_process(get_current()->mm->context.id.u.pid);
666 	}
667 
668 	(*timer_clockevent.event_handler)(&timer_clockevent);
669 
670 	return IRQ_HANDLED;
671 }
672 
673 static u64 timer_read(struct clocksource *cs)
674 {
675 	if (time_travel_mode != TT_MODE_OFF) {
676 		/*
677 		 * We make reading the timer cost a bit so that we don't get
678 		 * stuck in loops that expect time to move more than the
679 		 * exact requested sleep amount, e.g. python's socket server,
680 		 * see https://bugs.python.org/issue37026.
681 		 *
682 		 * However, don't do that when we're in interrupt or such as
683 		 * then we might recurse into our own processing, and get to
684 		 * even more waiting, and that's not good - it messes up the
685 		 * "what do I do next" and onstack event we use to know when
686 		 * to return from time_travel_update_time().
687 		 */
688 		if (!irqs_disabled() && !in_interrupt() && !in_softirq() &&
689 		    !time_travel_ext_waiting)
690 			time_travel_update_time(time_travel_time +
691 						TIMER_MULTIPLIER,
692 						false);
693 		return time_travel_time / TIMER_MULTIPLIER;
694 	}
695 
696 	return os_nsecs() / TIMER_MULTIPLIER;
697 }
698 
699 static struct clocksource timer_clocksource = {
700 	.name		= "timer",
701 	.rating		= 300,
702 	.read		= timer_read,
703 	.mask		= CLOCKSOURCE_MASK(64),
704 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
705 };
706 
707 static void __init um_timer_setup(void)
708 {
709 	int err;
710 
711 	err = request_irq(TIMER_IRQ, um_timer, IRQF_TIMER, "hr timer", NULL);
712 	if (err != 0)
713 		printk(KERN_ERR "register_timer : request_irq failed - "
714 		       "errno = %d\n", -err);
715 
716 	err = os_timer_create();
717 	if (err != 0) {
718 		printk(KERN_ERR "creation of timer failed - errno = %d\n", -err);
719 		return;
720 	}
721 
722 	err = clocksource_register_hz(&timer_clocksource, NSEC_PER_SEC/TIMER_MULTIPLIER);
723 	if (err) {
724 		printk(KERN_ERR "clocksource_register_hz returned %d\n", err);
725 		return;
726 	}
727 	clockevents_register_device(&timer_clockevent);
728 }
729 
730 void read_persistent_clock64(struct timespec64 *ts)
731 {
732 	long long nsecs;
733 
734 	if (time_travel_mode != TT_MODE_OFF)
735 		nsecs = time_travel_start + time_travel_time;
736 	else
737 		nsecs = os_persistent_clock_emulation();
738 
739 	set_normalized_timespec64(ts, nsecs / NSEC_PER_SEC,
740 				  nsecs % NSEC_PER_SEC);
741 }
742 
743 void __init time_init(void)
744 {
745 #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
746 	switch (time_travel_mode) {
747 	case TT_MODE_EXTERNAL:
748 		time_travel_start = time_travel_ext_req(UM_TIMETRAVEL_GET_TOD, -1);
749 		/* controller gave us the *current* time, so adjust by that */
750 		time_travel_ext_get_time();
751 		time_travel_start -= time_travel_time;
752 		break;
753 	case TT_MODE_INFCPU:
754 	case TT_MODE_BASIC:
755 		if (!time_travel_start_set)
756 			time_travel_start = os_persistent_clock_emulation();
757 		break;
758 	case TT_MODE_OFF:
759 		/* we just read the host clock with os_persistent_clock_emulation() */
760 		break;
761 	}
762 #endif
763 
764 	timer_set_signal_handler();
765 	late_time_init = um_timer_setup;
766 }
767 
768 #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
769 unsigned long calibrate_delay_is_known(void)
770 {
771 	if (time_travel_mode == TT_MODE_INFCPU ||
772 	    time_travel_mode == TT_MODE_EXTERNAL)
773 		return 1;
774 	return 0;
775 }
776 
777 int setup_time_travel(char *str)
778 {
779 	if (strcmp(str, "=inf-cpu") == 0) {
780 		time_travel_mode = TT_MODE_INFCPU;
781 		timer_clockevent.name = "time-travel-timer-infcpu";
782 		timer_clocksource.name = "time-travel-clock";
783 		return 1;
784 	}
785 
786 	if (strncmp(str, "=ext:", 5) == 0) {
787 		time_travel_mode = TT_MODE_EXTERNAL;
788 		timer_clockevent.name = "time-travel-timer-external";
789 		timer_clocksource.name = "time-travel-clock-external";
790 		return time_travel_connect_external(str + 5);
791 	}
792 
793 	if (!*str) {
794 		time_travel_mode = TT_MODE_BASIC;
795 		timer_clockevent.name = "time-travel-timer";
796 		timer_clocksource.name = "time-travel-clock";
797 		return 1;
798 	}
799 
800 	return -EINVAL;
801 }
802 
803 __setup("time-travel", setup_time_travel);
804 __uml_help(setup_time_travel,
805 "time-travel\n"
806 "This option just enables basic time travel mode, in which the clock/timers\n"
807 "inside the UML instance skip forward when there's nothing to do, rather than\n"
808 "waiting for real time to elapse. However, instance CPU speed is limited by\n"
809 "the real CPU speed, so e.g. a 10ms timer will always fire after ~10ms wall\n"
810 "clock (but quicker when there's nothing to do).\n"
811 "\n"
812 "time-travel=inf-cpu\n"
813 "This enables time travel mode with infinite processing power, in which there\n"
814 "are no wall clock timers, and any CPU processing happens - as seen from the\n"
815 "guest - instantly. This can be useful for accurate simulation regardless of\n"
816 "debug overhead, physical CPU speed, etc. but is somewhat dangerous as it can\n"
817 "easily lead to getting stuck (e.g. if anything in the system busy loops).\n"
818 "\n"
819 "time-travel=ext:[ID:]/path/to/socket\n"
820 "This enables time travel mode similar to =inf-cpu, except the system will\n"
821 "use the given socket to coordinate with a central scheduler, in order to\n"
822 "have more than one system simultaneously be on simulated time. The virtio\n"
823 "driver code in UML knows about this so you can also simulate networks and\n"
824 "devices using it, assuming the device has the right capabilities.\n"
825 "The optional ID is a 64-bit integer that's sent to the central scheduler.\n");
826 
827 int setup_time_travel_start(char *str)
828 {
829 	int err;
830 
831 	err = kstrtoull(str, 0, &time_travel_start);
832 	if (err)
833 		return err;
834 
835 	time_travel_start_set = 1;
836 	return 1;
837 }
838 
839 __setup("time-travel-start", setup_time_travel_start);
840 __uml_help(setup_time_travel_start,
841 "time-travel-start=<seconds>\n"
842 "Configure the UML instance's wall clock to start at this value rather than\n"
843 "the host's wall clock at the time of UML boot.\n");
844 #endif
845