The Pedigree Project 0.1
hosted/SchedulerTimer.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "SchedulerTimer.h"
21#include "pedigree/kernel/Log.h"
22#include "pedigree/kernel/compiler.h"
23#include "pedigree/kernel/machine/Machine.h"
24#include "pedigree/kernel/machine/SchedulerTimerDispatchCleanup.h"
25#include "pedigree/kernel/machine/SchedulerTimerHandler.h"
26#include "pedigree/kernel/processor/Processor.h"
27
28#include "IrqManager.h"
29
31#define ONE_SECOND 1000000000
32#define HZ 10
33
34uint64_t HostedSchedulerTimer::nominalQuantumNs() const {
35 return uint64_t(ONE_SECOND) / HZ;
36}
37
39
40#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
41HostedSchedulerTimer::HardContextHook HostedSchedulerTimer::m_HardContextHook = nullptr;
42
43void HostedSchedulerTimer::setHardContextHookForTest(HardContextHook hook) {
44 __atomic_store_n(&m_HardContextHook, hook, __ATOMIC_RELEASE);
45}
46
47uintptr_t HostedSchedulerTimer::sourceForTest() {
48 return reinterpret_cast<uintptr_t>(&m_Instance);
49}
50
51SchedulerTimerHandler* HostedSchedulerTimer::publishedHandlerForTest() {
52 return m_Instance.m_Handler.publishedHandlerForTest(Processor::id());
53}
54
55size_t HostedSchedulerTimer::activeDispatchesForTest() {
57}
58
59bool HostedSchedulerTimer::directRoutePublishedForTest() {
60 return HostedIrqManager::schedulerIrqHandlerForTest(1) == &m_Instance;
61}
62
63bool HostedSchedulerTimer::queueTickForTest() {
64 return m_Instance.m_TickSource.queueExpirationForTest();
65}
66#endif
67
71
75
79
81 if (!m_TickSource.prepare(SIGUSR2, this)) {
82 return false;
83 }
84
85 IrqManager& irqManager = *Machine::instance().getIrqManager();
86 m_IrqId = irqManager.registerSchedulerIrqHandler(1, this, IrqPolicy::syntheticHard());
87 if (m_IrqId == 0) {
88 m_TickSource.destroy();
89 return false;
90 }
91
92 // Publish the direct route before arming the signal source so the first
93 // scheduler tick cannot arrive on an unregistered line.
94 if (!m_TickSource.arm(ONE_SECOND / HZ)) {
95 if (!irqManager.unregisterSchedulerIrqHandler(m_IrqId, this)) {
96 FATAL(
97 "HostedSchedulerTimer could not roll back its IRQ "
98 "registration");
99 }
100 m_IrqId = 0;
101 m_TickSource.destroy();
102 return false;
103 }
104
105 m_bInitialized = true;
106 return true;
107}
109 if (!m_bInitialized) {
110 return;
111 }
112 m_bInitialized = false;
113
114 if (!m_TickSource.disarm()) {
115 FATAL("HostedSchedulerTimer could not disarm its signal source");
116 }
117
118 // Free the IRQ
119 if (m_IrqId != 0) {
120 IrqManager& irqManager = *Machine::instance().getIrqManager();
121 if (!irqManager.unregisterSchedulerIrqHandler(m_IrqId, this)) {
122 FATAL(
123 "HostedSchedulerTimer teardown could not remove its direct "
124 "IRQ route");
125 }
126 m_IrqId = 0;
127 }
128 m_TickSource.destroy();
129
130 // Source teardown does not confer ownership of the scheduler callback.
131 // Its exact owner remains responsible for unpublishing it.
132}
133
135 : m_IrqId(0), m_TickSource(), m_Handler(), m_bInitialized(false) {}
136
137void HostedSchedulerTimer::schedulerIrq(irq_id_t number, InterruptState& state) {
138 const siginfo_t* signalInfo = reinterpret_cast<const siginfo_t*>(state.getRegister(1));
139 if (number != 1 || state.getInterruptNumber() != SIGUSR2) {
140 return;
141 }
142
143 size_t expirations = 0;
144 const HostedTickSource::TakeResult result = m_TickSource.takeExpirations(signalInfo, expirations);
145 if (result == HostedTickSource::TakeResult::NotSource) {
146 return;
147 }
148 if (result == HostedTickSource::TakeResult::Invalid) {
149 FATAL_NOLOCK("HostedSchedulerTimer received invalid tick-source metadata");
150 return;
151 }
152 if (!expirations) {
153 return;
154 }
155
156 constexpr uint64_t Interval = ONE_SECOND / HZ;
157 if (expirations > (~static_cast<uint64_t>(0) / Interval)) {
158 FATAL_NOLOCK("HostedSchedulerTimer elapsed-time batch overflowed");
159 return;
160 }
161 const uint64_t delta = static_cast<uint64_t>(expirations) * Interval;
162
164 if (LIKELY(m_Handler.beginDispatch(Processor::id(), dispatch))) {
165 SchedulerTimerDispatchCleanup dispatchCleanup(dispatch);
166 ExecutionContextGuard schedulerContext(ExecutionContext::SchedulerIrq);
167#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
168 // Test only admitted callbacks. Keeping this hook outside the slot
169 // previously let a dropped scheduler tick look like real progress.
170 HardContextHook hook = __atomic_load_n(&m_HardContextHook, __ATOMIC_ACQUIRE);
171 if (hook) {
172 hook(delta, state);
173 }
174#endif
175 dispatch.handler()->timer(delta, state);
176 }
177}
HostedSchedulerTimer() INITIALISATION_ONLY
bool initialise() INITIALISATION_ONLY
virtual bool registerHandler(SchedulerTimerHandler *handler)
virtual bool removeHandler(SchedulerTimerHandler *handler)
virtual irq_id_t registerSchedulerIrqHandler(uint8_t irq, SchedulerIrqHandler *handler, const IrqPolicy &policy)
Definition IrqManager.cc:25
virtual bool unregisterSchedulerIrqHandler(irq_id_t Id, SchedulerIrqHandler *handler)
Definition IrqManager.cc:29
static ProcessorId id()
bool beginDispatch(size_t owner, DispatchGuard &guard)
bool publish(size_t owner, SchedulerTimerHandler *handler)
bool unpublish(size_t owner, SchedulerTimerHandler *handler)
virtual void timer(uint64_t delta, InterruptState &state)=0
static bool canRemoveHandlerInCurrentContext()