moto-sys 0.2.0

Motor OS system crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use core::arch::asm;
use core::sync::atomic::*;
use core::time::Duration;

use super::KernelStaticPage;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant {
    tsc_val: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime {
    nanos: u64, // Note that SystemTime uses nanos vs Instant which uses tsc.
}

#[allow(unused)]
pub const UNIX_EPOCH: SystemTime = SystemTime { nanos: 0u64 };
pub const NANOS_IN_SEC: u64 = 1_000_000_000;

impl Instant {
    pub const fn nan() -> Self {
        Instant { tsc_val: 0 }
    }
    pub fn is_nan(&self) -> bool {
        self.tsc_val == 0
    }

    pub fn from_u64(val: u64) -> Self {
        Instant { tsc_val: val }
    }

    pub fn as_u64(&self) -> u64 {
        self.tsc_val
    }

    pub fn from_nanos(nanos: u64) -> Self {
        Instant {
            tsc_val: nanos_to_tsc(nanos),
        }
    }

    pub fn now() -> Self {
        Instant { tsc_val: rdtsc() }
    }

    pub fn raw_tsc(&self) -> u64 {
        self.tsc_val
    }

    pub fn duration_since(&self, earlier: Instant) -> Duration {
        if earlier.tsc_val > self.tsc_val {
            // TODO: figure out why this happens in hyperv + qemu.
            #[cfg(all(not(feature = "rustc-dep-of-std"), feature = "userspace"))]
            super::syscalls::SysMem::log(
                alloc::format!(
                    "time goes back: earlier: {:x} > later: {:x}",
                    earlier.tsc_val,
                    self.tsc_val
                )
                .as_str(),
            )
            .ok();

            #[cfg(feature = "rustc-dep-of-std")]
            super::syscalls::SysMem::log("fros-sys: time: time goes back").ok();
            return Duration::ZERO;
        }

        let tsc_diff = self.tsc_val - earlier.tsc_val;
        if tsc_diff == 0 {
            return Duration::ZERO;
        }

        let tsc_in_sec = KernelStaticPage::get().tsc_in_sec;
        if core::intrinsics::unlikely(tsc_in_sec == 0) {
            return Duration::ZERO;
        }
        let secs = tsc_diff / tsc_in_sec;
        let nanos = tsc_to_nanos(tsc_diff % tsc_in_sec);

        Duration::new(secs, nanos as u32)
    }

    pub fn elapsed(&self) -> Duration {
        Instant::now().duration_since(self.clone())
    }

    pub const fn infinite_future() -> Self {
        Instant { tsc_val: u64::MAX }
    }

    pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
        if *self < *other {
            return None;
        }

        let result_tsc = self.tsc_val - other.tsc_val;
        let result_nanos = tsc_to_nanos_128(result_tsc);
        if result_nanos > (u64::MAX as u128) {
            None
        } else {
            Some(Duration::from_nanos(result_nanos as u64))
        }
    }

    pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
        let tsc_secs = other
            .as_secs()
            .checked_mul(KernelStaticPage::get().tsc_in_sec)?;
        let tsc_diff = nanos_to_tsc(other.subsec_nanos() as u64).checked_add(tsc_secs)?;

        Some(Instant {
            tsc_val: self.tsc_val.checked_add(tsc_diff)?,
        })
    }

    pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
        let tsc_secs = other
            .as_secs()
            .checked_mul(KernelStaticPage::get().tsc_in_sec)?;
        let tsc_diff = nanos_to_tsc(other.subsec_nanos() as u64).checked_add(tsc_secs)?;

        if tsc_diff > self.tsc_val {
            None
        } else {
            Some(Instant {
                tsc_val: self.tsc_val - tsc_diff,
            })
        }
    }
}

impl core::ops::Add<Duration> for Instant {
    type Output = Instant;

    fn add(self, other: Duration) -> Instant {
        let tsc_secs = other.as_secs() * KernelStaticPage::get().tsc_in_sec;
        let tsc_diff = nanos_to_tsc(other.subsec_nanos() as u64) + tsc_secs;

        Instant {
            tsc_val: self.tsc_val + tsc_diff,
        }
    }
}

impl core::ops::Sub<Duration> for Instant {
    type Output = Instant;

    fn sub(self, other: Duration) -> Self::Output {
        let tsc_secs = other.as_secs() * KernelStaticPage::get().tsc_in_sec;
        let tsc_diff = nanos_to_tsc(other.subsec_nanos() as u64) + tsc_secs;

        Instant {
            tsc_val: self.tsc_val - tsc_diff,
        }
    }
}

pub fn system_start_time() -> super::time::Instant {
    Instant {
        tsc_val: KernelStaticPage::get().system_start_time_tsc,
    }
}

pub fn since_system_start() -> Duration {
    Instant::now()
        .checked_sub_instant(&Instant {
            tsc_val: KernelStaticPage::get().system_start_time_tsc,
        })
        .unwrap()
}

#[allow(unused)]
impl SystemTime {
    pub fn now() -> Self {
        SystemTime {
            nanos: abs_nanos_from_tsc(rdtsc()),
        }
    }

    pub fn from_u64(val: u64) -> Self {
        Self { nanos: val }
    }

    pub fn as_u64(&self) -> u64 {
        self.nanos
    }

    pub fn as_unix_ts(&self) -> u64 {
        self.nanos
    }

    pub fn from_unix_ts(val: u64) -> Self {
        Self { nanos: val }
    }

    pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
        if self.nanos >= other.nanos {
            Ok(Duration::from_nanos(self.nanos - other.nanos))
        } else {
            Err(Duration::from_nanos(other.nanos - self.nanos))
        }
    }

    pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
        let result_nanos = self.nanos as u128 + other.as_nanos();
        if result_nanos > (u64::MAX as u128) {
            None
        } else {
            Some(Self {
                nanos: result_nanos as u64,
            })
        }
    }

    pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
        let other_nanos = other.as_nanos();
        if self.nanos as u128 >= other_nanos {
            Some(Self {
                nanos: self.nanos - (other_nanos as u64),
            })
        } else {
            None
        }
    }
}

fn abs_nanos_from_tsc(tsc_val: u64) -> u64 {
    /*  see https://www.kernel.org/doc/Documentation/virt/kvm/msr.rst
        time = (current_tsc - tsc_timestamp)
        if (tsc_shift >= 0)
            time <<= tsc_shift;
        else
            time >>= -tsc_shift;
        time = (time * tsc_to_system_mul) >> 32
        time = time + system_time
    */
    fence(Ordering::Acquire);
    let page = KernelStaticPage::get();
    let mut time = tsc_val - page.tsc_ts;
    let tsc_shift = page.tsc_shift;
    if tsc_shift >= 0 {
        time <<= tsc_shift;
    } else {
        time >>= -tsc_shift;
    }

    // TODO: sometimes this overflows in debug mode.
    let (mul, _overflow) = time.overflowing_mul(page.tsc_mul as u64);

    time = mul >> 32;
    time += page.system_time;

    page.base_nsec + time
}

fn rdtsc() -> u64 {
    let mut eax: u32;
    let mut edx: u32;

    unsafe {
        asm!(
            "lfence",  // Prevent the CPU from reordering.
            "rdtsc",
            lateout("eax") eax,
            lateout("edx") edx,
            options(nostack)  // Don't say "nomem", otherwise the compiler might reorder.
        );
    }
    ((edx as u64) << 32) | (eax as u64)
}

fn tsc_to_nanos_128(tsc: u64) -> u128 {
    fence(Ordering::Acquire);
    let page = KernelStaticPage::get();

    let mut nanos = tsc as u128;
    let tsc_shift = page.tsc_shift;
    if tsc_shift >= 0 {
        nanos <<= tsc_shift;
    } else {
        nanos >>= -tsc_shift;
    }

    nanos * (page.tsc_mul as u128) >> 32
}

fn tsc_to_nanos(tsc: u64) -> u64 {
    fence(Ordering::Acquire);
    let page = KernelStaticPage::get();

    let mut nanos = tsc;
    let tsc_shift = page.tsc_shift;
    if tsc_shift >= 0 {
        nanos <<= tsc_shift;
    } else {
        nanos >>= -tsc_shift;
    }

    // TODO: this may overflow and panic. Fix.
    let (mul, _overflow) = nanos.overflowing_mul(page.tsc_mul as u64);
    mul >> 32
}

fn nanos_to_tsc(nanos: u64) -> u64 {
    fence(Ordering::Acquire);
    let page = KernelStaticPage::get();

    let tsc_shift = page.tsc_shift;

    // TODO: optimize?
    // TODO: fix panic on overflow.
    let mut res = if nanos >= (1u64 << 32) {
        (nanos >> 4) * ((1u64 << 36) / (page.tsc_mul as u64))
    } else {
        (nanos << 32) / (page.tsc_mul as u64)
    };

    if tsc_shift >= 0 {
        res >>= tsc_shift;
    } else {
        res <<= -tsc_shift;
    }

    return res;
}

#[derive(Debug)]
pub struct UtcDateTime {
    pub year: u32,
    pub month: u8, // starts with 1
    pub day: u8,   // starts with 1
    pub hour: u8,
    pub minute: u8,
    pub second: u8,
    pub nanosecond: u32,
}

impl core::fmt::Display for UtcDateTime {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "{}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}Z",
            self.year,
            self.month,
            self.day,
            self.hour,
            self.minute,
            self.second,
            self.nanosecond / (1000 * 1000)
        )
    }
}

impl UtcDateTime {
    pub fn from_unix_nanos(nanos: u128) -> Self {
        let st = nanos as u64;
        let nanosecond = (st % (1000 * 1000 * 1000)) as u32;

        let seconds = (st - (nanosecond as u64)) / (1000 * 1000 * 1000);
        let time = seconds % (24 * 60 * 60);

        let second = (time % 60) as u8;
        let minutes = (time - (second as u64)) / 60;
        let minute = (minutes % 60) as u8;
        let hour = ((minutes - (minute as u64)) / 60) as u8;

        let mut days = (seconds - time) / (24 * 60 * 60);
        let mut year: u32 = 1970;

        fn leap_year(year: u32) -> bool {
            (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
        }

        // Find the year.
        loop {
            if leap_year(year) {
                if days < 366 {
                    break;
                }
                days -= 366; // leap year
            } else if days < 365 {
                break;
            } else {
                days -= 365; // normal year
            }
            year += 1;
        }

        // Find the month and day.
        const MONTHS: [u8; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        let mut month: u8 = 0;
        loop {
            if month == 1 {
                if leap_year(year) {
                    if days < 29 {
                        break;
                    }
                    days -= 29;
                    month += 1;
                    continue;
                } else if days < 28 {
                    break;
                }
                days -= 28;
                month += 1;
                continue;
            }
            if days < MONTHS[month as usize] as u64 {
                break;
            }
            days -= MONTHS[month as usize] as u64;
            month += 1;
        }

        Self {
            year,
            month: month + 1,
            day: (days + 1) as u8,
            hour,
            minute,
            second,
            nanosecond,
        }
    }
}