1 // SPDX-License-Identifier: GPL-2.0
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)
9 #include <linux/clockchips.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/jiffies.h>
14 #include <linux/sched.h>
15 #include <linux/spinlock.h>
16 #include <linux/threads.h>
18 #include <asm/param.h>
19 #include <kern_util.h>
21 #include <timer-internal.h>
22 #include <shared/init.h>
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
;
31 static bool time_travel_start_set
;
32 static unsigned long long time_travel_start
;
34 #define time_travel_start_set 0
35 #define time_travel_start 0
38 void timer_handler(int sig
, struct siginfo
*unused_si
, struct uml_pt_regs
*regs
)
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
47 * This is not the case in inf-cpu mode, since there we
48 * never get any real signals from the OS.
50 if (time_travel_mode
== TT_MODE_BASIC
)
51 time_travel_set_time(time_travel_timer_expiry
);
53 local_irq_save(flags
);
54 do_IRQ(TIMER_IRQ
, regs
);
55 local_irq_restore(flags
);
58 static int itimer_shutdown(struct clock_event_device
*evt
)
60 if (time_travel_mode
!= TT_MODE_OFF
)
61 time_travel_set_timer_mode(TT_TMR_DISABLED
);
63 if (time_travel_mode
!= TT_MODE_INFCPU
)
69 static int itimer_set_periodic(struct clock_event_device
*evt
)
71 unsigned long long interval
= NSEC_PER_SEC
/ HZ
;
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
);
79 if (time_travel_mode
!= TT_MODE_INFCPU
)
80 os_timer_set_interval(interval
);
85 static int itimer_next_event(unsigned long delta
,
86 struct clock_event_device
*evt
)
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
);
95 if (time_travel_mode
!= TT_MODE_INFCPU
)
96 return os_timer_one_shot(delta
);
101 static int itimer_one_shot(struct clock_event_device
*evt
)
103 return itimer_next_event(0, evt
);
106 static struct clock_event_device timer_clockevent
= {
107 .name
= "posix-timer",
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
,
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
125 static irqreturn_t
um_timer(int irq
, void *dev
)
127 if (get_current()->mm
!= NULL
)
129 /* userspace - relay signal, results in correct userspace timers */
130 os_alarm_process(get_current()->mm
->context
.id
.u
.pid
);
133 (*timer_clockevent
.event_handler
)(&timer_clockevent
);
138 static u64
timer_read(struct clocksource
*cs
)
140 if (time_travel_mode
!= TT_MODE_OFF
) {
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.
147 time_travel_set_time(time_travel_time
+ TIMER_MULTIPLIER
);
148 return time_travel_time
/ TIMER_MULTIPLIER
;
151 return os_nsecs() / TIMER_MULTIPLIER
;
154 static struct clocksource timer_clocksource
= {
158 .mask
= CLOCKSOURCE_MASK(64),
159 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
162 static void __init
um_timer_setup(void)
166 err
= request_irq(TIMER_IRQ
, um_timer
, IRQF_TIMER
, "hr timer", NULL
);
168 printk(KERN_ERR
"register_timer : request_irq failed - "
169 "errno = %d\n", -err
);
171 err
= os_timer_create();
173 printk(KERN_ERR
"creation of timer failed - errno = %d\n", -err
);
177 err
= clocksource_register_hz(&timer_clocksource
, NSEC_PER_SEC
/TIMER_MULTIPLIER
);
179 printk(KERN_ERR
"clocksource_register_hz returned %d\n", err
);
182 clockevents_register_device(&timer_clockevent
);
185 void read_persistent_clock64(struct timespec64
*ts
)
189 if (time_travel_start_set
)
190 nsecs
= time_travel_start
+ time_travel_time
;
192 nsecs
= os_persistent_clock_emulation();
194 set_normalized_timespec64(ts
, nsecs
/ NSEC_PER_SEC
,
195 nsecs
% NSEC_PER_SEC
);
198 void __init
time_init(void)
200 timer_set_signal_handler();
201 late_time_init
= um_timer_setup
;
204 #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
205 unsigned long calibrate_delay_is_known(void)
207 if (time_travel_mode
== TT_MODE_INFCPU
)
212 int setup_time_travel(char *str
)
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";
222 time_travel_mode
= TT_MODE_BASIC
;
223 timer_clockevent
.name
= "time-travel-timer";
224 timer_clocksource
.name
= "time-travel-clock";
231 __setup("time-travel", setup_time_travel
);
232 __uml_help(setup_time_travel
,
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"
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");
247 int setup_time_travel_start(char *str
)
251 err
= kstrtoull(str
, 0, &time_travel_start
);
255 time_travel_start_set
= 1;
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");