This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppleI386PlatformExpert.cpp
360 lines (284 loc) · 10 KB
/
AppleI386PlatformExpert.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
* Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* This file was modified by William Kent in 2017 to support the PureDarwin
* project. This notice is included in support of clause 2.2(b) of the License.
*/
#include <IOKit/IOLib.h>
#include <IOKit/assert.h>
#include <IOKit/system.h>
#include <IOKit/IORegistryEntry.h>
#include <IOKit/platform/ApplePlatformExpert.h>
#include <libkern/c++/OSContainers.h>
#include <libkern/c++/OSUnserialize.h>
#include <pexpert/i386/boot.h>
extern "C" {
#include <i386/cpuid.h>
#include <pexpert/i386/protos.h>
}
#include "AppleI386PlatformExpert.h"
enum {
kIRQAvailable = 0,
kIRQExclusive = 1,
kIRQSharable = 2,
kSystemIRQCount = 16
};
static struct {
UInt16 consumers;
UInt16 status;
} IRQ[kSystemIRQCount];
static IOLock *ResourceLock;
class AppleI386PlatformExpertGlobals {
public:
bool isValid;
AppleI386PlatformExpertGlobals();
~AppleI386PlatformExpertGlobals();
};
static AppleI386PlatformExpertGlobals AppleI386PlatformExpertGlobals;
AppleI386PlatformExpertGlobals::AppleI386PlatformExpertGlobals() {
ResourceLock = IOLockAlloc();
bzero(IRQ, sizeof(IRQ));
}
AppleI386PlatformExpertGlobals::~AppleI386PlatformExpertGlobals() {
if (ResourceLock) IOLockFree(ResourceLock);
}
#pragma mark -
#define super IOPlatformExpert
OSDefineMetaClassAndStructors(AppleI386PlatformExpert, IOPlatformExpert);
IOService *AppleI386PlatformExpert::probe(IOService *provider, SInt32 *score) {
if (score != 0) *score = 10000;
return this;
}
bool AppleI386PlatformExpert::init(OSDictionary *properties) {
if (!super::init()) return false;
OSString *name = (OSString *)getProperty("InterruptControllerName");
if (name == 0) name = OSString::withCStringNoCopy("AppleI386CPUInterruptController");
_interruptControllerName = OSSymbol::withString(name);
return true;
}
bool AppleI386PlatformExpert::start(IOService *provider) {
setBootROMType(kBootROMTypeNewWorld);
if (!super::start(provider)) return false;
PE_halt_restart = handlePEHaltRestart;
registerService();
// Hack: Initialize AppleI386CPU ourself because no one else will.
bootCPU = new AppleI386CPU;
if (bootCPU == 0) return false;
bootCPU->init();
bootCPU->attach(0);
if (!bootCPU->startCommon()) return false;
return true;
}
bool AppleI386PlatformExpert::configure(IOService *provider) {
OSArray *topLevel;
OSDictionary *dict;
IOService *nub;
topLevel = OSDynamicCast(OSArray, getProperty("top-level"));
if (topLevel) {
while ((dict = OSDynamicCast(OSDictionary, topLevel->getObject(0)))) {
dict->retain();
topLevel->removeObject(0);
nub = createNub(dict);
if (nub == 0) continue;
dict->release();
nub->attach(this);
nub->registerService();
}
}
return true;
}
bool AppleI386PlatformExpert::matchNubWithPropertyTable(IOService *nub, OSDictionary *table) {
OSString *nameProp;
OSString *match;
if ((nameProp = (OSString *)nub->getProperty(gIONameKey)) == 0) return false;
if ((match = (OSString *)table->getObject(gIONameMatchKey)) == 0) return false;
return match->isEqualTo(nameProp);
}
IOService *AppleI386PlatformExpert::createNub(OSDictionary *from) {
IOService *nub;
nub = super::createNub(from);
if (nub) {
const char *name = nub->getName();
if (strcmp(name, "pci") == 0) {
// TODO: Get the PCI info from the boot args
// and set it as the `pci-bus-info` property in the `from` dict.
} else if (strcmp(name, "bios") == 0) {
setupBIOS(nub);
} else if (strcmp(name, "8259-pic") == 0) {
setupPIC(nub);
}
}
return nub;
}
void AppleI386PlatformExpert::setupPIC(IOService *nub) {
int i;
OSDictionary *propTable;
OSArray *controller;
OSArray *specifier;
OSData *tmpData;
long tmpLong;
propTable = nub->getPropertyTable();
// For the moment... assume a classic 8259 interrupt controller
// with 16 interrupts. Later, this will be changed to detect
// an APIC and/or MP-Table and then will set the nubs appropriately.
specifier = OSArray::withCapacity(kSystemIRQCount);
assert(specifier);
for (i = 0; i < kSystemIRQCount; i++) {
tmpLong = i;
tmpData = OSData::withBytes(&tmpLong, sizeof(tmpLong));
specifier->setObject(tmpData);
}
controller = OSArray::withCapacity(kSystemIRQCount);
assert(controller);
for (i = 0; i < kSystemIRQCount; i++) controller->setObject(_interruptControllerName);
propTable->setObject(gIOInterruptControllersKey, controller);
propTable->setObject(gIOInterruptSpecifiersKey, specifier);
specifier->release();
controller->release();
}
void AppleI386PlatformExpert::setupBIOS(IOService *nub) {
// TODO: Implement this function.
// This function is dependent upon being able to retrieve the
// PCI bus data. While the booter does collect some PCI data,
// but it does not include the data needed here.
}
bool AppleI386PlatformExpert::getMachineName(char *name, int maxLength) {
strncpy(name, "x86", maxLength);
return true;
}
bool AppleI386PlatformExpert::getModelName(char *name, int maxLengh) {
i386_cpu_info_t *cpuid_cpu_info = cpuid_info();
if (cpuid_cpu_info->cpuid_brand_string[0] != '\0') {
strncpy(name, cpuid_cpu_info->cpuid_brand_string, maxLengh);
} else {
strncpy(name, cpuid_cpu_info->cpuid_model_string, maxLengh);
}
return true;
}
int AppleI386PlatformExpert::handlePEHaltRestart(unsigned int type) {
int ret = -1;
int temporary_sum = 0;
switch (type) {
case kPERestartCPU:
// Note: This code may or may not work reliably on all systems.
// The original author of it indicated that it should work on any
// system with a compliant PCI controller.
// Obtained from: http://smackerelofopinion.blogspot.nl/2009/06/rebooting-pc.html
outb(0xCF9, 0x02);
// A delay of some sort is required here.
temporary_sum = 2;
temporary_sum += 2;
outb(0xCF9, 0x04);
// This should not be reached, but just in case...
break;
case kPEHaltCPU:
default:
ret = -1;
break;
}
return ret;
}
bool AppleI386PlatformExpert::setNubInterruptVectors(IOService *nub, const UInt32 *vectors, UInt32 vectorCount) {
OSArray *controller = 0;
OSArray *specifier = 0;
bool success = false;
if (vectorCount == 0) {
nub->removeProperty(gIOInterruptControllersKey);
nub->removeProperty(gIOInterruptSpecifiersKey);
return true;
}
specifier = OSArray::withCapacity(vectorCount);
controller = OSArray::withCapacity(vectorCount);
if (!specifier || !controller) goto done;
for (UInt32 i = 0; i < vectorCount; i++) {
OSData *data = OSData::withBytes(&vectors[i], sizeof(vectors[i]));
specifier->setObject(data);
controller->setObject(_interruptControllerName);
if (data) data->release();
}
nub->setProperty(gIOInterruptControllersKey, controller);
nub->setProperty(gIOInterruptSpecifiersKey, specifier);
success = true;
done:
if (specifier) specifier->release();
if (controller) controller->release();
return success;
}
bool AppleI386PlatformExpert::setNubInterruptVector(IOService *nub, UInt32 vector) {
return setNubInterruptVectors(nub, &vector, 1);
}
IOReturn AppleI386PlatformExpert::callPlatformFunction(const OSSymbol *functionName, bool waitForFunction, void *param1, void *param2, void *param3, void *param4) {
bool ok;
if (functionName->isEqualTo("SetDeviceInterrupts")) {
IOService *nub = (IOService *)param1;
UInt32 *vectors = (UInt32 *)param2;
UInt32 vectorCount = (UInt32)((UInt64)param3);
bool exclusive = (bool)param4;
if (vectorCount != 1) return kIOReturnBadArgument;
ok = reserveSystemInterrupt(nub, vectors[0], exclusive);
if (ok == false) return kIOReturnNoResources;
ok = setNubInterruptVector(nub, vectors[0]);
if (ok == false) releaseSystemInterrupt(nub, vectors[0], exclusive);
return ok ? kIOReturnSuccess : kIOReturnNoMemory;
} else if (functionName->isEqualTo("SetBusClockRateMHz")) {
UInt32 rateMHz = (UInt32)((UInt64)param1);
gPEClockFrequencyInfo.bus_clock_rate_hz = rateMHz * 1000000;
return kIOReturnSuccess;
} else if (functionName->isEqualTo("SetCPUClockRateMHz")) {
UInt32 rateMHz = (UInt32)((UInt64)param1);
gPEClockFrequencyInfo.cpu_clock_rate_hz = rateMHz * 1000000;
return kIOReturnSuccess;
}
return super::callPlatformFunction(functionName, waitForFunction, param1, param2, param3, param4);
}
bool AppleI386PlatformExpert::reserveSystemInterrupt(IOService *client, UInt32 vectorNumber, bool exclusive) {
bool ok = false;
if (vectorNumber >= kSystemIRQCount) return ok;
IOLockLock(ResourceLock);
if (exclusive) {
if (IRQ[vectorNumber].status == kIRQAvailable) {
IRQ[vectorNumber].status = kIRQExclusive;
IRQ[vectorNumber].consumers = 1;
ok = true;
}
} else {
if (IRQ[vectorNumber].status == kIRQAvailable || IRQ[vectorNumber].status == kIRQSharable) {
IRQ[vectorNumber].status = kIRQSharable;
IRQ[vectorNumber].consumers++;
ok = true;
}
}
IOLockUnlock(ResourceLock);
return ok;
}
void AppleI386PlatformExpert::releaseSystemInterrupt(IOService *client, UInt32 vectorNumber, bool exclusive) {
if (vectorNumber >= kSystemIRQCount) return;
IOLockLock(ResourceLock);
if (exclusive) {
if (IRQ[vectorNumber].status == kIRQExclusive) {
IRQ[vectorNumber].status = kIRQAvailable;
IRQ[vectorNumber].consumers = 0;
}
} else {
if (IRQ[vectorNumber].status == kIRQSharable && --IRQ[vectorNumber].consumers == 0) {
IRQ[vectorNumber].status = kIRQAvailable;
}
}
IOLockUnlock(ResourceLock);
}