Skip to main content

atmos/kernel/
timer.rs

1// timer.rs - ARM Generic Timer (Non-Secure Physical Timer CNTP) Driver
2#![allow(dead_code)]
3use alloc::string::String;
4use core::sync::atomic::{AtomicUsize, Ordering};
5
6// `init()`/`init_secondary()` は全4コアそれぞれの CNTP ローカルタイマーを
7// 個別に有効化し、各コアの `rust_irq_handler` が自コアのタイマー割り込み
8// ごとに `handle_timer_irq()` を呼ぶ(`interrupt.rs` の `int_src` チェックに
9// コア限定は無い)。そのため `SYSTEM_TICKS` は最大4コアから並行に
10// インクリメントされ得る。かつては非アトミックな `static mut usize` の
11// `+= 1` だったため、read-modify-write の競合で更新ロストが起こり得た
12// (2026-07-14 発見。`spec/TODO.md`「デバッグ・検証候補」参照。
13// `AtomicUsize` 化はこの更新ロストのみを解消するもので、下記の
14// レート問題とは別)。
15//
16// **レート問題(2026-07-14 に追加修正)**: 4コアそれぞれが独立に約10ms
17// 周期でカウントアップすると、消費側(`layout.rs` の CSS アニメーション
18// 時計・`main.rs` の `ticks==500`/sysmon 間隔判定等)が前提とする
19// 「1 tick = 10ms」という設計が崩れ、実質最大4倍速で進んでしまう。
20// `handle_timer_irq()` 側で `Core 0` のみがインクリメントするよう
21// ガードして解消した。`watchdog::check_from_irq()`
22// (`handle_timer_irq()` から全コアで無条件に呼ばれる)はこの
23// `SYSTEM_TICKS` を一切参照せず `get_system_time_ms()`(コア数に
24// 非依存のカウンタレジスタ直読み)のみを使うため、このガードによる
25// 影響は無いことをコードで確認済み。
26static SYSTEM_TICKS: AtomicUsize = AtomicUsize::new(0);
27static mut TIMER_PERIOD_TICKS: u64 = 0;
28
29// --- 壁時計(NTP で同期した実時刻)---
30// NTP 同期時の Unix 時刻(秒, UTC)と、その時点のシステム稼働ミリ秒を保持する。
31// `set_wall_clock()` の書き手は `net_service_thread`(Core 2 固定、NTP 同期
32// 契機)と Aura の日時設定コマンド(`os_lib/aura/builtins.rs`、Core 0 固定の
33// `gui_shell_process` から)の2箇所存在し、読み手(`get_unix_time()` 等、
34// UI の時計表示等)は Core 0 から呼ばれる。3変数(Unix秒/基準ms/設定済み
35// フラグ)を個別の `static mut` として非アトミックに更新していたため、
36// Core 2 が書き込み中に Core 0 が読むと三つ組が不整合な状態
37// (例: `WALL_SET=true` が先に見えるが `WALL_BASE_MS` はまだ更新前)で
38// 読める可能性があった(2026-07-14 発見。`spec/TODO.md`「デバッグ・
39// 検証候補」節の `static mut` SMP 横断監査で確認)。3値をまとめて
40// `spin::Mutex` で保護し、読み書きの原子性を保証する。
41struct WallClock {
42    base_unix: u64,
43    base_ms: u64,
44    set: bool,
45}
46static WALL_CLOCK: spin::Mutex<WallClock> = spin::Mutex::new(WallClock {
47    base_unix: 0,
48    base_ms: 0,
49    set: false,
50});
51
52/// JST のタイムゾーンオフセット(秒)
53const JST_OFFSET_SECS: u64 = 9 * 3600;
54/// NTP epoch (1900-01-01) と Unix epoch (1970-01-01) の差(秒)
55pub const NTP_UNIX_OFFSET: u64 = 2_208_988_800;
56
57/// ARM Generic Timer を 10ms 間隔で初期化します
58pub fn init() {
59    unsafe {
60        let freq: u64;
61        core::arch::asm!("mrs {}, cntfrq_el0", out(reg) freq);
62        TIMER_PERIOD_TICKS = freq / 100;
63        core::arch::asm!("msr cntp_tval_el0, {}", in(reg) TIMER_PERIOD_TICKS);
64        let ctrl: u64 = 1;
65        core::arch::asm!("msr cntp_ctl_el0, {}", in(reg) ctrl);
66    }
67    SYSTEM_TICKS.store(0, Ordering::Relaxed);
68    crate::debug!("[TIMER] ARM Generic Timer enabled at 10ms intervals");
69}
70
71pub fn init_secondary() {
72    unsafe {
73        core::arch::asm!("msr cntp_tval_el0, {}", in(reg) TIMER_PERIOD_TICKS);
74        let ctrl: u64 = 1;
75        core::arch::asm!("msr cntp_ctl_el0, {}", in(reg) ctrl);
76    }
77}
78
79/// タイマー割り込みハンドラから呼び出され、次の 10ms の割り込みを設定します
80pub fn handle_timer_irq() {
81    unsafe {
82        // 次の 10ms のカウントを設定
83        core::arch::asm!("msr cntp_tval_el0, {}", in(reg) TIMER_PERIOD_TICKS);
84    }
85
86    // `SYSTEM_TICKS` は「1 tick ≈ 10ms」という消費側の前提を保つため、
87    // 常時稼働する Core 0 のローカルタイマー割り込みのみでカウントする
88    // (他コアの割り込みでも進めると最大4倍速になってしまうレート問題の
89    // 対策。上のコメント参照)。
90    if crate::kernel::scheduler::core_id() == 0 {
91        SYSTEM_TICKS.fetch_add(1, Ordering::Relaxed);
92    }
93
94    // アプリ描画ウォッチドッグ: メインスレッドが draw に張り付いていないか確認(ハング検知)
95    // `SYSTEM_TICKS` を参照しないため上記のコアガードとは無関係に全コアで実行する。
96    crate::kernel::watchdog::check_from_irq();
97    // OS のメインループが止まっていないか(TODO R4)。
98    crate::kernel::watchdog::check_os_loop_from_irq();
99}
100
101/// 起動時からのシステムティック(10ms単位)を取得します。
102/// ARM64 システムカウンタ (`cntpct_el0`) 直読の高精度ミリ秒から算出するため、
103/// タイマー割り込みのドリフトやスキップに左右されず、常に正確な実時間経過を返します(tickless idle 前提対応)。
104pub fn get_ticks() -> usize {
105    (get_system_time_ms() / 10) as usize
106}
107
108/// 割り込みハンドラ経由の生ティックカウンタ値(診断・監査用)
109pub fn get_raw_ticks() -> usize {
110    SYSTEM_TICKS.load(Ordering::Relaxed)
111}
112
113/// システムカウンターから現在時刻をマイクロ秒単位で取得します。
114/// 【2026-11-04】以前は `count * 1_000_000 / freq` を u128 で計算しており、u128 の割り算はソフトウェアの
115/// 関数(`u128_div_rem`)になるため、JS のコアの実行時間の約 3% を占めていた(`[PCPROF]`)。商と余りに分けると
116/// u64 の割り算 2 回で同じ値になる(余り × 10^6 は freq × 10^6 未満で u64 に収まる)。
117pub fn get_system_time_us() -> u64 {
118    let count: u64;
119    let freq: u64;
120    unsafe {
121        core::arch::asm!("mrs {}, cntpct_el0", out(reg) count);
122        core::arch::asm!("mrs {}, cntfrq_el0", out(reg) freq);
123    }
124    if freq == 0 {
125        return 0;
126    }
127    let secs = count / freq;
128    let rem = count % freq;
129    secs.wrapping_mul(1_000_000).wrapping_add(rem * 1_000_000 / freq)
130}
131
132/// システムカウンターから現在時刻をミリ秒単位で取得します
133pub fn get_system_time_ms() -> u64 {
134    get_system_time_us() / 1000
135}
136
137// ---------------------------------------------------------------------------
138// 壁時計(NTP 同期した実時刻)
139// ---------------------------------------------------------------------------
140
141/// NTP 等で取得した Unix 時刻(UTC 秒)を基準として登録する。
142pub fn set_wall_clock(unix_secs: u64) {
143    let now_ms = get_system_time_ms();
144    let mut w = WALL_CLOCK.lock();
145    w.base_unix = unix_secs;
146    w.base_ms = now_ms;
147    w.set = true;
148}
149
150/// 壁時計が同期済みかどうか
151pub fn is_wall_clock_set() -> bool {
152    WALL_CLOCK.lock().set
153}
154
155/// 現在の Unix 時刻(UTC 秒)を返す。未同期なら None。
156pub fn get_unix_time() -> Option<u64> {
157    let w = WALL_CLOCK.lock();
158    if !w.set {
159        return None;
160    }
161    let elapsed_ms = get_system_time_ms().wrapping_sub(w.base_ms);
162    Some(w.base_unix + elapsed_ms / 1000)
163}
164
165/// 現在の Unix 時刻(UTC ミリ秒)を返す。未同期なら None。
166/// 【2026-11-28】JS の `Date.now()` 用。以前は `get_unix_time()`(秒)を 1000 倍していたため、
167/// `Date.now()` が 1 秒刻みでしか進まず、ページ内の経過時間の計測が 0 になっていた。
168pub fn get_unix_time_ms() -> Option<u64> {
169    let w = WALL_CLOCK.lock();
170    if !w.set {
171        return None;
172    }
173    let elapsed_ms = get_system_time_ms().wrapping_sub(w.base_ms);
174    Some(w.base_unix.saturating_mul(1000).saturating_add(elapsed_ms))
175}
176
177/// 日数(1970-01-01 からの経過日数)を (年, 月, 日) に変換する。
178/// Howard Hinnant の civil_from_days アルゴリズム。
179fn civil_from_days(z: i64) -> (i64, u32, u32) {
180    let z = z + 719468;
181    let era = (if z >= 0 { z } else { z - 146096 }) / 146097;
182    let doe = z - era * 146097; // [0, 146096]
183    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
184    let y = yoe + era * 400;
185    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
186    let mp = (5 * doy + 2) / 153; // [0, 11]
187    let d = (doy - (153 * mp + 2) / 5 + 1) as u32; // [1, 31]
188    let m = (if mp < 10 { mp + 3 } else { mp - 9 }) as u32; // [1, 12]
189    (if m <= 2 { y + 1 } else { y }, m, d)
190}
191
192/// 現在の JST 日時を (年, 月, 日, 時, 分, 秒) で返す。未同期なら None。
193pub fn get_datetime_jst() -> Option<(i64, u32, u32, u32, u32, u32)> {
194    let unix = get_unix_time()?;
195    let local = unix + JST_OFFSET_SECS;
196    let days = (local / 86400) as i64;
197    let secs_of_day = local % 86400;
198    let (y, mo, d) = civil_from_days(days);
199    let hh = (secs_of_day / 3600) as u32;
200    let mm = ((secs_of_day % 3600) / 60) as u32;
201    let ss = (secs_of_day % 60) as u32;
202    Some((y, mo, d, hh, mm, ss))
203}
204
205/// 現在の JST 日時を "YYYY-MM-DD  HH:mm" 形式で返す。未同期なら None。
206pub fn format_datetime_jst() -> Option<String> {
207    let (y, mo, d, hh, mm, _ss) = get_datetime_jst()?;
208    Some(alloc::format!(
209        "{:04}-{:02}-{:02}  {:02}:{:02}",
210        y,
211        mo,
212        d,
213        hh,
214        mm
215    ))
216}
217
218/// 現在の JST 時刻を "HH:MM:SS" で返す(コマンド実行時刻の記録用)。未同期なら None。
219pub fn format_time_hms_jst() -> Option<String> {
220    let (_y, _mo, _d, hh, mm, ss) = get_datetime_jst()?;
221    Some(alloc::format!("{:02}:{:02}:{:02}", hh, mm, ss))
222}