Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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