Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 231 additions & 0 deletions kernel/arch/hpet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,182 @@ pub fn frequency_hz() -> Option<u64> {
HPET_STATE.get().map(|info| info.frequency_hz)
}

/// N2.6b: Read monotonic nanoseconds elapsed since HPET initialization.
///
/// Only uses 64-bit HPET counters so counter wraparound (~40,000+ years at
/// 14.318 MHz) cannot regress monotonic time. Returns `None` if HPET is
/// unavailable or 32-bit-only (caller falls back to 1 kHz tick time).
#[inline]
pub fn elapsed_ns() -> Option<u64> {
let info = HPET_STATE.get()?;
if !info.counter_64bit || info.period_fs == 0 {
return None;
}
let counter = read_main_counter()?;
// counter * period_fs / 1_000_000 (1 ns = 10^6 fs).
Some(((counter as u128).saturating_mul(info.period_fs as u128) / 1_000_000) as u64)
}

/// N2.6b: Effective HPET clock resolution in nanoseconds (at least 1 ns).
#[inline]
pub fn resolution_ns() -> Option<u64> {
let info = HPET_STATE.get()?;
if !info.counter_64bit || info.period_fs == 0 {
return None;
}
Some((info.period_fs / 1_000_000).max(1))
}

// ============================================================================
// N2.6b: MC146818 CMOS RTC Wall-Clock Reader (x86_64 ports 0x70 / 0x71)
// ============================================================================

/// Convert a Gregorian calendar UTC date/time to Unix epoch seconds (1970-01-01).
/// Pure, no-I/O implementation using Howard Hinnant's civil-days algorithm.
pub fn civil_to_unix_secs(
year: i64,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Option<u64> {
if year < 1970
|| !(1..=12).contains(&month)
|| !(1..=31).contains(&day)
|| hour > 23
|| minute > 59
|| second > 59
{
return None;
}
let m = month as i64;
let d = day as i64;
let y = year - if m <= 2 { 1 } else { 0 };
let era = if y >= 0 { y } else { y - 399 } / 400;
let yoe = y - era * 400; // [0, 399]
let mp = m + if m > 2 { -3 } else { 9 }; // [0, 11]
let doy = (153 * mp + 2) / 5 + d - 1; // [0, 365]
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
let days_since_epoch = era * 146097 + doe - 719468;
if days_since_epoch < 0 {
return None;
}
let total_secs = (days_since_epoch as u64)
.checked_mul(86_400)?
.checked_add((hour as u64) * 3_600)?
.checked_add((minute as u64) * 60)?
.checked_add(second as u64)?;
Some(total_secs)
}

#[inline]
fn bcd_to_bin(v: u8) -> Option<u8> {
let lo = v & 0x0F;
let hi = (v >> 4) & 0x0F;
if lo > 9 || hi > 9 {
return None;
}
Some(hi * 10 + lo)
}

/// Decode raw MC146818 CMOS RTC registers into Unix epoch seconds.
pub fn decode_cmos_rtc_fields(
mut sec: u8,
mut min: u8,
mut hour: u8,
mut day: u8,
mut month: u8,
mut year: u8,
mut century: u8,
status_b: u8,
) -> Option<u64> {
let is_binary = (status_b & 0x04) != 0;
let is_24h = (status_b & 0x02) != 0;
let pm = !is_24h && (hour & 0x80) != 0;
hour &= 0x7F;

if !is_binary {
sec = bcd_to_bin(sec)?;
min = bcd_to_bin(min)?;
hour = bcd_to_bin(hour)?;
day = bcd_to_bin(day)?;
month = bcd_to_bin(month)?;
year = bcd_to_bin(year)?;
if century != 0 {
century = bcd_to_bin(century).unwrap_or(0);
}
}

if !is_24h {
if hour == 12 {
hour = if pm { 12 } else { 0 };
} else if pm {
hour = hour.checked_add(12)?;
}
}

let full_year = if (19..=99).contains(&century) {
(century as i64) * 100 + (year as i64)
} else {
2000 + (year as i64)
};
civil_to_unix_secs(full_year, month, day, hour, min, sec)
}

/// Read the x86 MC146818 CMOS RTC at boot and return UTC Unix epoch seconds.
///
/// Bounded retry loop waits for UIP (Update-In-Progress) to clear and requires
/// two identical consecutive reads to avoid mid-second rollover corruption.
/// Returns `None` if the RTC is absent or reports invalid/uninitialized fields.
#[cfg(target_arch = "x86_64")]
pub fn read_cmos_rtc_unix_secs() -> Option<u64> {
use x86_64::instructions::port::Port;

let read_reg = |reg: u8| -> u8 {
// SAFETY: MC146818 CMOS index (0x70) / data (0x71) ports, ring 0 only.
unsafe {
let mut idx: Port<u8> = Port::new(0x70);
let mut data: Port<u8> = Port::new(0x71);
idx.write(reg & 0x7F);
data.read()
}
};

let sample = || -> Option<[u8; 8]> {
for _ in 0..10_000 {
if (read_reg(0x0A) & 0x80) == 0 {
return Some([
read_reg(0x00),
read_reg(0x02),
read_reg(0x04),
read_reg(0x07),
read_reg(0x08),
read_reg(0x09),
read_reg(0x32),
read_reg(0x0B),
]);
}
core::hint::spin_loop();
}
None
};

for _ in 0..8 {
let a = sample()?;
let b = sample()?;
if a == b {
return decode_cmos_rtc_fields(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
}
}
None
}

#[cfg(not(target_arch = "x86_64"))]
pub fn read_cmos_rtc_unix_secs() -> Option<u64> {
None
}

// ============================================================================
// Internal Implementation
// ============================================================================
Expand Down Expand Up @@ -463,3 +639,58 @@ unsafe fn write_reg64(base_virt: u64, offset: usize, value: u64) {
let addr = (base_virt + offset as u64) as *mut u64;
write_volatile(addr, value);
}

#[cfg(test)]
mod n2_6b_rtc_tests {
use super::*;

#[test]
fn n2_6b_civil_to_unix_secs_epoch_and_leap_years() {
assert_eq!(civil_to_unix_secs(1970, 1, 1, 0, 0, 0), Some(0));
assert_eq!(
civil_to_unix_secs(2000, 2, 29, 12, 30, 45),
Some(951_827_445)
);
assert_eq!(civil_to_unix_secs(2024, 3, 1, 0, 0, 0), Some(1_709_251_200));
assert_eq!(
civil_to_unix_secs(2026, 11, 28, 4, 0, 0),
Some(1_795_838_400)
);
// Invalid bounds fail closed.
assert_eq!(civil_to_unix_secs(1969, 12, 31, 23, 59, 59), None);
assert_eq!(civil_to_unix_secs(2026, 13, 1, 0, 0, 0), None);
assert_eq!(civil_to_unix_secs(2026, 1, 1, 24, 0, 0), None);
}

#[test]
fn n2_6b_decode_cmos_rtc_bcd_binary_and_12h() {
// BCD + 24h (status_b = 0x02): 2026-11-28 04:00:00 UTC
assert_eq!(
decode_cmos_rtc_fields(0x00, 0x00, 0x04, 0x28, 0x11, 0x26, 0x20, 0x02),
Some(1_795_838_400)
);
// Binary + 24h (status_b = 0x06), century = 0 defaults to 20xx
assert_eq!(
decode_cmos_rtc_fields(0, 0, 4, 28, 11, 26, 0, 0x06),
Some(1_795_838_400)
);
// Binary + 12h PM (status_b = 0x04): 12:00 PM -> 12:00, 1:00 PM -> 13:00, 12:00 AM -> 00:00
assert_eq!(
decode_cmos_rtc_fields(0, 0, 12 | 0x80, 1, 1, 70, 19, 0x04),
Some(12 * 3600)
);
assert_eq!(
decode_cmos_rtc_fields(0, 0, 1 | 0x80, 1, 1, 70, 19, 0x04),
Some(13 * 3600)
);
assert_eq!(
decode_cmos_rtc_fields(0, 0, 12, 1, 1, 70, 19, 0x04),
Some(0)
);
// Malformed BCD digit rejected.
assert_eq!(
decode_cmos_rtc_fields(0x0A, 0x00, 0x04, 0x28, 0x11, 0x26, 0x20, 0x02),
None
);
}
}
2 changes: 2 additions & 0 deletions kernel/kernel_core/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13108,6 +13108,8 @@ pub fn terminate_process(pid: ProcessId, exit_code: i32) {
// last live member exits, under the group id (an init is always a group
// leader), never when the first of several threads does.
if last_live {
// N2.6b: Reclaim all interval timers and POSIX timers owned by this process.
crate::time::cleanup_all_timers_for_process(tgid);
if tgid == pid {
handle_namespace_init_death(pid, &pid_ns_chain);
} else {
Expand Down
3 changes: 3 additions & 0 deletions kernel/kernel_core/scheduler_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ fn reschedule_if_needed_inner(post_drain: Option<fn()>, forced_origin: Option<Re
// R65-6 FIX: Drain deferred TCP timer work before scheduling check
crate::time::drain_deferred_tcp_timers();

// N2.6b: Deliver expired process itimers and POSIX timers in safe context.
crate::time::drain_deferred_process_timers();

// D3-NETNS-DATAPLANE RX-INGRESS: bounded bring-up RX poll beside the TCP
// drain. Self-throttled inside the net crate (quiesce flag + ~10ms CAS
// window + non-reentrant guard); the ready gate above guarantees
Expand Down
20 changes: 20 additions & 0 deletions kernel/kernel_core/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,26 @@ pub fn send_signal_kernel(pid: ProcessId, signal: Signal) -> Result<SignalAction
send_process_signal(pid, signal, false, None, false)
}

/// N2.6b: Kernel-authoritative process-directed signal with explicit `SigInfo`
/// (e.g. `ITIMER_REAL` / POSIX `timer_create(SIGEV_SIGNAL)` with `SI_TIMER`).
pub fn send_signal_kernel_with_info(
pid: ProcessId,
signal: Signal,
info: SigInfo,
) -> Result<SignalAction, SignalError> {
send_process_signal(pid, signal, false, Some(info), false)
}

/// N2.6b: Kernel-authoritative thread-directed signal with explicit `SigInfo`
/// (e.g. POSIX `timer_create(SIGEV_THREAD_ID)` with `SI_TIMER`).
pub fn send_signal_kernel_thread_with_info(
tid: ProcessId,
signal: Signal,
info: SigInfo,
) -> Result<SignalAction, SignalError> {
send_signal_inner(tid, signal, false, SignalScope::Thread, Some(info), false)
}

/// GAP-5 G7: where a generated signal is queued.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SignalScope {
Expand Down
60 changes: 60 additions & 0 deletions kernel/kernel_core/syscall/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,66 @@ pub(super) struct TimeSpec {
// H.0.1-3: Compile-time ABI size assertion.
const _: [(); 16] = [(); core::mem::size_of::<TimeSpec>()];

/// struct itimerval (Linux x86_64 ABI, 32 bytes, N2.6b)
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(super) struct ITimerVal {
pub(super) it_interval_sec: i64,
pub(super) it_interval_usec: i64,
pub(super) it_value_sec: i64,
pub(super) it_value_usec: i64,
}

const _: [(); 32] = [(); core::mem::size_of::<ITimerVal>()];
const _: [(); 8] = [(); core::mem::align_of::<ITimerVal>()];
const _: [(); 0] = [(); core::mem::offset_of!(ITimerVal, it_interval_sec)];
const _: [(); 16] = [(); core::mem::offset_of!(ITimerVal, it_value_sec)];

/// struct itimerspec (Linux x86_64 ABI, 32 bytes, N2.6b)
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(super) struct ITimerSpec {
pub(super) it_interval_sec: i64,
pub(super) it_interval_nsec: i64,
pub(super) it_value_sec: i64,
pub(super) it_value_nsec: i64,
}

const _: [(); 32] = [(); core::mem::size_of::<ITimerSpec>()];
const _: [(); 8] = [(); core::mem::align_of::<ITimerSpec>()];
const _: [(); 0] = [(); core::mem::offset_of!(ITimerSpec, it_interval_sec)];
const _: [(); 16] = [(); core::mem::offset_of!(ITimerSpec, it_value_sec)];

/// struct sigevent (Linux x86_64 ABI, 64 bytes, N2.6b)
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct SigEvent {
pub(super) sigev_value: u64,
pub(super) sigev_signo: i32,
pub(super) sigev_notify: i32,
pub(super) sigev_notify_tid: i32,
pub(super) _pad: [u8; 44],
}

impl Default for SigEvent {
fn default() -> Self {
Self {
sigev_value: 0,
sigev_signo: 0,
sigev_notify: 0,
sigev_notify_tid: 0,
_pad: [0; 44],
}
}
}

const _: [(); 64] = [(); core::mem::size_of::<SigEvent>()];
const _: [(); 8] = [(); core::mem::align_of::<SigEvent>()];
const _: [(); 0] = [(); core::mem::offset_of!(SigEvent, sigev_value)];
const _: [(); 8] = [(); core::mem::offset_of!(SigEvent, sigev_signo)];
const _: [(); 12] = [(); core::mem::offset_of!(SigEvent, sigev_notify)];
const _: [(); 16] = [(); core::mem::offset_of!(SigEvent, sigev_notify_tid)];

/// struct utsname (Linux ABI, fixed-size strings)
///
/// D2-ABI-STAT-LAYOUT (LOW leg): Linux `new_utsname` is 6 x [u8;65] = 390
Expand Down
7 changes: 7 additions & 0 deletions kernel/kernel_core/syscall/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ pub(super) fn exec_from_bytes(
cloexec_cgroup_id,
cloexec_closed,
old_shared_regions,
exec_tgid,
) = {
let mut proc = process.lock();

Expand Down Expand Up @@ -1199,6 +1200,7 @@ pub(super) fn exec_from_bytes(
proc.poll_restore_blocked = None;
// N2.6a: POSIX/Linux execve(2) disables any established alternate signal stack.
proc.sigaltstack = crate::signal::SigAltStack::disabled();
let exec_tgid = proc.tgid;
// Robust-list registration is address-space-local. Clear the old
// image's pointer before publishing the replacement so exit cleanup
// cannot dereference stale or newly-reused virtual memory.
Expand All @@ -1223,9 +1225,14 @@ pub(super) fn exec_from_bytes(
cloexec_cgroup_id,
cloexec_closed,
old_shared_regions,
exec_tgid,
)
};

// N2.6b: POSIX execve(2) deletes POSIX timer_create timers while preserving
// ITIMER_REAL / alarm. Run outside the Process lock.
crate::time::cleanup_posix_timers_for_exec(exec_tgid);

// R169-4 FIX (HIGH, lock inversion): Drop the closed CLOEXEC
// FileDescriptors and uncharge the per-cgroup FD budget OUTSIDE the Process
// lock. SocketFile::Drop → socket close → wake_all (re-locks PROCESS_TABLE +
Expand Down
Loading