The Pedigree Project 0.1
kernel/machine/mach_virt/Timer.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#ifndef KERNEL_MACHINE_VIRT_TIMER_H
9#define KERNEL_MACHINE_VIRT_TIMER_H
10
11#include "pedigree/kernel/machine/SplitIrqHandler.h"
12#include "pedigree/kernel/machine/Timer.h"
13#include "pedigree/kernel/machine/TimerHandlerRegistry.h"
14#include "pedigree/kernel/utilities/List.h"
15
16class Event;
17class Thread;
18
20class VirtTimer : public Timer, private SplitIrqHandler {
21 public:
22 static VirtTimer& instance();
23
24 bool initialise1();
25 bool initialise3();
26 void uninitialise();
27
28 size_t getYear() override;
29 uint8_t getMonth() override;
30 uint8_t getDayOfMonth() override;
31 uint8_t getDayOfWeek() override;
32 uint8_t getHour() override;
33 uint8_t getMinute() override;
34 uint8_t getSecond() override;
35 uint64_t getNanosecond() override;
36 uint64_t getTickCount() override;
37 uint64_t getTickCountNano() override;
38 uint64_t getTickCountNanoFast() override;
39 Time::Timestamp getUnixTimestamp() override;
40 void synchronise(bool tohw = false) override;
41
42 bool registerHandler(TimerHandler* handler) override;
43 bool unregisterHandler(TimerHandler* handler) override;
44 void addAlarm(Event* event, size_t seconds, size_t microseconds = 0) override;
45 void removeAlarm(Event* event) override;
46 size_t removeAlarm(Event* event, bool returnZero) override;
47
48 private:
49 struct Alarm {
50 Event* event;
51 Thread* thread;
52 uint64_t deadline;
53 };
54
55 VirtTimer();
56 ~VirtTimer() override;
57
58 HardStageDisposition hardIrq(irq_id_t number, InterruptState& state, size_t& work) override;
59 void threadedIrq(size_t work) override;
60 bool quiesceIrqSources() override;
61 void rearmIrqSources(size_t work) override;
62
63 static uint64_t counter();
64 uint64_t ticksToNanoseconds(uint64_t ticks) const;
65 void processElapsed(uint64_t nanoseconds);
66 void updateCivilTime(uint64_t seconds);
67
68 uint64_t m_Frequency;
69 uint64_t m_BootCount;
70 uint64_t m_LastCount;
71 uint64_t m_RtcBaseCount;
72 uint64_t m_RtcBaseSeconds;
73 uint64_t m_ElapsedSinceSync;
74 uint32_t m_IntervalTicks;
75 irq_id_t m_IrqId;
76 size_t m_Year;
77 uint8_t m_Month;
78 uint8_t m_Day;
79 uint8_t m_DayOfWeek;
80 uint8_t m_Hour;
81 uint8_t m_Minute;
82 uint8_t m_Second;
83 bool m_Prepared;
84 bool m_Initialised;
85 TimerHandlerRegistry m_Handlers;
86 List<Alarm*> m_Alarms;
87 Spinlock m_AlarmLock;
88
89 static VirtTimer m_Instance;
90};
91
92#endif
Definition Event.h:49
Definition List.h:61
void removeAlarm(Event *event) override
size_t getYear() override
void threadedIrq(size_t work) override
uint8_t getDayOfWeek() override
void addAlarm(Event *event, size_t seconds, size_t microseconds=0) override
HardStageDisposition hardIrq(irq_id_t number, InterruptState &state, size_t &work) override
Time::Timestamp getUnixTimestamp() override
void synchronise(bool tohw=false) override
Synchronises the timer with the hardware. Useful for updating the fields returned by get*,...
uint64_t getTickCountNano() override
uint64_t getTickCount() override
bool quiesceIrqSources() override
uint8_t getHour() override
uint8_t getMinute() override
uint8_t getSecond() override
void rearmIrqSources(size_t work) override
uint64_t getNanosecond() override
uint8_t getDayOfMonth() override
uint64_t getTickCountNanoFast() override
uint8_t getMonth() override