menios icon indicating copy to clipboard operation
menios copied to clipboard

RTC boot-time synchronization

Open pbalduino opened this issue 3 months ago • 0 comments

Goal

Integrate RTC hardware clock reading at boot time to initialize system time.

Context

Although we can read CMOS time (src/kernel/timer/rtc.c:16), the kernel never uses it. We need proper RTC integration at boot.

Current State

  • RTC driver exists and can read hardware clock
  • Limine bootloader provides timestamp, but we have no fallback
  • System time initialization happens without consulting RTC

What's Needed

  1. Boot-time sync path: Read RTC at boot if Limine timestamp is absent
  2. Write path: Implement clock_settime() persistence to hardware
  3. Integration point: src/kernel/init/main.c should call rtc_sync_system_time()

Implementation

// src/kernel/timer/rtc.c
void rtc_sync_system_time(void) {
    struct rtc_time hw_time;
    time_t unix_time;
    
    // Read hardware clock
    if (rtc_read_time(&hw_time) < 0) {
        kprintf("RTC: Failed to read hardware clock\n");
        return;
    }
    
    // Convert to Unix timestamp
    unix_time = rtc_to_unix_time(&hw_time);
    
    // Update system time if Limine didn't provide one
    if (system_boot_time == 0) {
        system_boot_time = unix_time;
        kprintf("RTC: System time initialized from hardware clock\n");
    }
}

// Write system time back to RTC
int rtc_set_time_from_unix(time_t unix_time) {
    struct rtc_time hw_time;
    unix_to_rtc_time(unix_time, &hw_time);
    return rtc_write_time(&hw_time);
}

Files to Modify

  • src/kernel/timer/rtc.c - Add sync and write functions
  • src/kernel/init/main.c - Call rtc_sync_system_time() early in boot
  • src/kernel/syscall/syscall.c - Update sys_clock_settime() to persist to RTC

Testing

  • Boot without Limine timestamp, verify RTC fallback
  • Set system time with clock_settime(), verify RTC update
  • Reboot and verify time persists
  • Test with invalid RTC values

Dependencies

  • #286 - Kernel RTC driver (✅ complete)
  • #239 - Kernel time syscalls (✅ complete)

Parent Issue

  • #226 - RTC and time management

pbalduino avatar Oct 20 '25 15:10 pbalduino