The Pedigree Project 0.1
LocalApic.h
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#ifndef KERNEL_MACHINE_X86_COMMON_LOCAL_APIC_H
21#define KERNEL_MACHINE_X86_COMMON_LOCAL_APIC_H
22#include <config.h>
23
24#if MULTIPROCESSOR
25
26#include "pedigree/kernel/Atomic.h"
27#include "pedigree/kernel/compiler.h"
28#include "pedigree/kernel/machine/SchedulerTimer.h"
29#include "pedigree/kernel/processor/InterruptHandler.h"
30#include "pedigree/kernel/processor/MemoryMappedIo.h"
31#include "pedigree/kernel/processor/Processor.h"
32#include "pedigree/kernel/processor/ProcessorInformation.h"
33#include "pedigree/kernel/processor/state_forward.h"
34#include "pedigree/kernel/processor/types.h"
35
36#include "LocalApicTimerHandlerSlots.h"
37#include "LocalApicTlbShootdown.h"
38
40
41#define IPI_TLB_SHOOTDOWN_VECTOR 0xF9
42#define IPI_RESCHEDULE_VECTOR 0xFA
43#define IPI_PROCESSOR_CONTROL_VECTOR 0xFB
44#define ERROR_VECTOR 0xFC
45#define SPURIOUS_VECTOR 0xFD
46#define TIMER_VECTOR 0xFE
47
53class LocalApic : public SchedulerTimer, private InterruptHandler {
54 private:
55 enum class ProcessorControlState : size_t { Idle, Paused, Unavailable, Terminal };
56 enum class ProcessorControlOwnership { Fresh, Quiesced, Failed, Terminal };
57
58 public:
60 inline LocalApic()
61 : m_IoSpace("Local APIC"),
62 m_PhysicalAddress(0),
63 m_X2Apic(false),
64 m_Handlers(),
65 m_BusFrequency(0),
66 m_TimerState(),
67 m_ProcessorControlOwner(),
68 m_TlbMutations(),
69 m_TlbTerminalFailure(),
70 m_TlbShootdown(),
71 m_ProcessorControlState(static_cast<size_t>(ProcessorControlState::Idle)),
72 m_ControlledProcessorCount(0),
73 m_TerminalProcessorCount(0),
74 m_TerminalProcessorMask(0) {}
76 inline virtual ~LocalApic() {}
77
83 bool initialise(uint64_t physicalAddress) INITIALISATION_ONLY;
85 void prepareApplicationProcessor() INITIALISATION_ONLY;
90 bool initialiseProcessor() INITIALISATION_ONLY;
91
93 enum {
94 deliveryModeFixed = 0,
95 deliveryModeLowestPriority = 1,
96 deliveryModeSmi = 2,
97 deliveryModeNmi = 4,
98 deliveryModeInit = 5,
99 deliveryModeStartup = 6,
100 deliveryModeExtInt = 7
101 };
102
109 MUST_USE_RESULT bool interProcessorInterrupt(uint8_t destinationApicId, uint8_t vector,
110 size_t deliveryMode, bool bAssert,
111 bool bLevelTriggered);
112
117 MUST_USE_RESULT bool interProcessorInterruptAllExcludingThis(uint8_t vector, size_t deliveryMode);
118
124 MUST_USE_RESULT TlbInvalidationResult invalidateAllProcessors(void* address,
125 uint64_t processors = ~uint64_t(0));
126
128 MUST_USE_RESULT TlbInvalidationResult beginTlbInvalidation(bool& global);
129
131 void endTlbInvalidation();
132
134 MUST_USE_RESULT bool closeTlbInvalidationAdmissionForTerminalFailure(
135 TlbInvalidationResult result);
136
137 bool tlbInvalidationFailureActive() const;
138 bool tlbInvalidationTerminal() const;
139
141 void servicePendingTlbShootdown();
142
144 void servicePendingTerminalProcessorControl();
145
146 enum class ProcessorControlResult {
147 Success,
148 InvalidState,
149 SubmissionFailed,
150 AcknowledgementTimedOut,
151 DrainTimedOut
152 };
153
155 MUST_USE_RESULT ProcessorControlResult quiesceAllOtherProcessors(size_t expectedProcessors);
156
158 MUST_USE_RESULT ProcessorControlResult resumeAllOtherProcessors();
159
162 MUST_USE_RESULT ProcessorControlResult haltAllOtherProcessors(size_t expectedProcessors);
163
166 uint8_t getId();
167
168 //
169 // SchedulerTimer interface
170 //
171 uint64_t nominalQuantumNs() const override;
172 bool supportsOneShot() const override {
173 return true;
174 }
175 bool armDeadline(uint64_t absoluteMonotonicNs) override;
176 void disarm() override;
177 virtual bool registerHandler(SchedulerTimerHandler* handler) {
178 // Logical Processor::id() is assigned after early BSP timer setup and
179 // can change during topology construction. The LAPIC's raw physical
180 // identifier is already stable on both sides of that transition.
181 return m_Handlers.registerHandler(getId(), handler);
182 }
183
184 virtual bool removeHandler(SchedulerTimerHandler* handler) {
185 return canRemoveHandlerInCurrentContext() && m_Handlers.removeHandler(getId(), handler);
186 }
187
188 void ack();
189
190 private:
193 LocalApic(const LocalApic&);
196 LocalApic& operator=(const LocalApic&);
197
202 bool check(uint64_t physicalAddress) INITIALISATION_ONLY;
203
205 bool waitForIcrIdle();
206 bool submitIcr(uint32_t high, uint32_t low);
207 uint32_t readRegister(uint32_t offset);
208 void writeRegister(uint32_t value, uint32_t offset);
209
210 ProcessorControlState processorControlState() const;
211 bool acquireProcessorControlOwner(bool acceptRetained, ProcessorControlOwnership& ownership);
212 bool releaseProcessorControlOwner();
213 ProcessorControlResult completeTerminalControl(size_t expectedProcessors);
214 ProcessorControlResult retainTerminalControl(ProcessorControlResult result);
215 bool acquireTlbShootdownBarrier();
216 bool releaseTlbShootdownBarrier();
217 bool adoptTerminalTlbShootdownBarrier();
218 bool waitForTlbMutationDrain();
219 bool waitForProcessorCount(size_t expectedProcessors);
220 bool waitForTerminalProcessorCount(size_t expectedProcessors);
221 bool waitForProcessorDrain();
222 ProcessorControlResult retainFailedControl(ProcessorControlResult result);
223 ProcessorControlResult releaseProcessorControlOrRetainFailure(ProcessorControlResult released,
224 ProcessorControlResult retained);
225 ProcessorControlResult retainQuiescedControl(ProcessorControlResult result);
226 ProcessorControlResult finishFailedQuiesce(ProcessorControlResult result);
227 ProcessorControlResult unwindQuiesce(ProcessorControlResult failure);
228 bool markTerminalProcessor(size_t processor);
229 void enterTerminalProcessorControl() NORETURN;
230
231 //
232 // InterruptHandler interface
233 //
234 virtual void interrupt(size_t nInterruptNumber, InterruptState& state);
235
237 MemoryMappedIo m_IoSpace;
238 uint64_t m_PhysicalAddress;
239 bool m_X2Apic;
240
243
245 size_t m_BusFrequency;
246
247 struct TimerState {
248 uint64_t lastInterruptNs;
249 bool oneShot;
250 uint64_t armedDeadlineNs;
251 };
252 TimerState m_TimerState[LocalApicTimerHandlerSlots::Capacity];
253
255 LocalApicProcessorControlOwner m_ProcessorControlOwner;
256
258 LocalApicTlbMutationGate m_TlbMutations;
259 LocalApicTlbTerminalFailure m_TlbTerminalFailure;
260 LocalApicTlbShootdown m_TlbShootdown;
261
263 Atomic<size_t> m_ProcessorControlState;
264
266 Atomic<size_t> m_ControlledProcessorCount;
267
269 Atomic<size_t> m_TerminalProcessorCount;
270 Atomic<uint64_t> m_TerminalProcessorMask;
271};
272
275#endif
276
277#endif
Abstract base class for interrupt-handlers.
Memory mapped I/O range.
SchedulerTimer & operator=(const SchedulerTimer &)
static bool canRemoveHandlerInCurrentContext()
virtual bool registerHandler(SchedulerTimerHandler *handler)=0
virtual bool removeHandler(SchedulerTimerHandler *handler)=0
TlbInvalidationResult
Definition Processor.h:56
uintptr_t physicalAddress(physical_uintptr_t address) PURE
Definition utils.h:39