VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/DevSmc.cpp@ 98063

Last change on this file since 98063 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 56.9 KB
Line 
1/* $Id: DevSmc.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * DevSmc - Apple System Management Controller.
4 *
5 * The SMC is controlling power, fans, take measurements (voltage, temperature,
6 * fan speed, ++), and lock Mac OS X to Apple hardware. For more details see:
7 * - http://en.wikipedia.org/wiki/System_Management_Controller
8 * - http://www.parhelia.ch/blog/statics/k3_keys.html
9 * - http://www.nosuchcon.org/talks/D1_02_Alex_Ninjas_and_Harry_Potter.pdf
10 */
11
12/*
13 * Copyright (C) 2013-2022 Oracle and/or its affiliates.
14 *
15 * This file is part of VirtualBox base platform packages, as
16 * available from https://www.virtualbox.org.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation, in version 3 of the
21 * License.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, see <https://www.gnu.org/licenses>.
30 *
31 * SPDX-License-Identifier: GPL-3.0-only
32 */
33
34
35/*********************************************************************************************************************************
36* Header Files *
37*********************************************************************************************************************************/
38#define LOG_GROUP LOG_GROUP_DEV_SMC
39#include <VBox/vmm/pdmdev.h>
40#include <VBox/log.h>
41#include <VBox/err.h>
42#include <iprt/assert.h>
43#include <iprt/string.h>
44#if defined(IN_RING0) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
45# include <iprt/asm-amd64-x86.h>
46# include <iprt/once.h>
47#endif
48#if defined(RT_OS_DARWIN) && defined(IN_RING3) && !defined(VBOX_DEVICE_STRUCT_TESTCASE) /* drags in bad page size define */
49# include "IOKit/IOKitLib.h"
50#endif
51
52#include "VBoxDD.h"
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58/** The current version of the saved state. */
59#define SMC_SAVED_STATE_VERSION 1 /** @todo later 2 */
60/** Empty saved state version. */
61#define SMC_SAVED_STATE_VERSION_BAKA 1
62
63/** The ring-0 operation number that attempts to get OSK0 and OSK1 from the real
64 * SMC. */
65#define SMC_CALLR0_READ_OSK 1
66
67
68/** @name Apple SMC port and register definitions.
69 * @{ */
70
71/** The first Apple SMC port. */
72#define SMC_PORT_FIRST 0x0300
73/** The number of registers (also ports). */
74#define SMC_REG_COUNT 0x0020
75
76/** The data register. */
77#define SMC_REG_DATA 0x00
78#define SMC_PORT_DATA (SMC_PORT_FIRST + SMC_REG_DATA)
79
80/** The command register. */
81#define SMC_REG_CMD 0x04
82#define SMC_PORT_CMD (SMC_PORT_FIRST + SMC_REG_CMD)
83
84/** Status code register. */
85#define SMC_REG_STATUS_CODE 0x1e
86#define SMC_PORT_STATUS_CODE (SMC_PORT_FIRST + SMC_REG_STATUS_CODE)
87/** @} */
88
89/** @name Apple SMC Commands.
90 * @{ */
91#define SMC_CMD_GET_KEY_VALUE 0x10
92#define SMC_CMD_PUT_KEY 0x11
93#define SMC_CMD_GET_KEY_BY_INDEX 0x12
94#define SMC_CMD_GET_KEY_INFO 0x13
95/** @} */
96
97/** @name Apple SMC Status Codes.
98 * @{ */
99#define SMC_STATUS_CD_SUCCESS UINT8_C(0x00)
100#define SMC_STATUS_CD_COMM_COLLISION UINT8_C(0x80)
101#define SMC_STATUS_CD_SPURIOUS_DATA UINT8_C(0x81)
102#define SMC_STATUS_CD_BAD_COMMAND UINT8_C(0x82)
103#define SMC_STATUS_CD_BAD_PARAMETER UINT8_C(0x83)
104#define SMC_STATUS_CD_KEY_NOT_FOUND UINT8_C(0x84)
105#define SMC_STATUS_CD_KEY_NOT_READABLE UINT8_C(0x85)
106#define SMC_STATUS_CD_KEY_NOT_WRITABLE UINT8_C(0x86)
107#define SMC_STATUS_CD_KEY_SIZE_MISMATCH UINT8_C(0x87)
108#define SMC_STATUS_CD_FRAMING_ERROR UINT8_C(0x88)
109#define SMC_STATUS_CD_BAD_ARGUMENT_ERROR UINT8_C(0x89)
110#define SMC_STATUS_CD_TIMEOUT_ERROR UINT8_C(0xb7)
111#define SMC_STATUS_CD_KEY_INDEX_RANGE_ERROR UINT8_C(0xb8)
112#define SMC_STATUS_CD_BAD_FUNC_PARAMETER UINT8_C(0xc0)
113#define SMC_STATUS_CD_EVENT_BUFF_WRONG_ORDER UINT8_C(0x??)
114#define SMC_STATUS_CD_EVENT_BUFF_READ_ERROR UINT8_C(0x??)
115#define SMC_STATUS_CD_DEVICE_ACCESS_ERROR UINT8_C(0xc7)
116#define SMC_STATUS_CD_UNSUPPORTED_FEATURE UINT8_C(0xcb)
117#define SMC_STATUS_CD_SMB_ACCESS_ERROR UINT8_C(0xcc)
118/** @} */
119
120/** @name Apple SMC Key Attributes.
121 * @{ */
122#define SMC_KEY_ATTR_PRIVATE UINT8_C(0x01)
123#define SMC_KEY_ATTR_UKN_0x02 UINT8_C(0x02)
124#define SMC_KEY_ATTR_UKN_0x04 UINT8_C(0x04)
125#define SMC_KEY_ATTR_CONST UINT8_C(0x08)
126#define SMC_KEY_ATTR_FUNCTION UINT8_C(0x10)
127#define SMC_KEY_ATTR_UKN_0x20 UINT8_C(0x20)
128#define SMC_KEY_ATTR_WRITE UINT8_C(0x40)
129#define SMC_KEY_ATTR_READ UINT8_C(0x80)
130/** @} */
131
132
133/** The index of the first enumerable key in g_aSmcKeys. */
134#define SMC_KEYIDX_FIRST_ENUM 2
135
136/** Macro for emitting a static DEVSMC4CHID initializer. */
137#define SMC4CH(ch1, ch2, ch3, ch4) { { ch1, ch2, ch3, ch4 } }
138
139/**
140 * Macro for comparing DEVSMC4CHID with a string value.
141 * @returns true if equal, false if not.
142 */
143#define SMC4CH_EQ(a_pSmcKey, a_sz4) ( (a_pSmcKey)->u32 == RT_MAKE_U32_FROM_U8(a_sz4[0], a_sz4[1], a_sz4[2], a_sz4[3]) )
144
145/** Indicates the we want a 2.x SMC. */
146#define VBOX_WITH_SMC_2_x
147
148
149/*********************************************************************************************************************************
150* Structures and Typedefs *
151*********************************************************************************************************************************/
152
153/**
154 * 4 char identifier
155 */
156typedef union DEVSMC4CHID
157{
158 /** Byte view. */
159 uint8_t ab[4];
160 /** 32-bit unsigned integer view. */
161 uint32_t u32;
162} DEVSMC4CHID;
163
164
165/**
166 * Current key data area for communicating with the guest.
167 */
168typedef struct DEVSMCCURKEY
169{
170 /** The key. */
171 DEVSMC4CHID Key;
172 /** The data type. */
173 DEVSMC4CHID Type;
174 /** Key attributes. */
175 uint8_t fAttr;
176 /** The value length. */
177 uint8_t cbValue;
178 uint8_t abAlignment[2];
179 /**
180 * The value union. 32 bytes is probably sufficient here, but we provide a
181 * little more room since it doesn't cost us anything. */
182 union
183 {
184 /** Byte view. */
185 uint8_t ab[128];
186 /** 16-bit view. */
187 uint16_t u16;
188 /** 32-bit view. */
189 uint32_t u32;
190 } Value;
191} DEVSMCCURKEY;
192AssertCompileSize(DEVSMCCURKEY, 128+12);
193/** Pointer to the current key buffer. */
194typedef DEVSMCCURKEY *PDEVSMCCURKEY;
195/** Const pointer to the current key buffer. */
196typedef DEVSMCCURKEY const *PCDEVSMCCURKEY;
197
198
199/**
200 * The device
201 */
202typedef struct DEVSMC
203{
204 /** The current command (SMC_PORT_CMD write). */
205 uint8_t bCmd;
206 /** Current key offset. */
207 uint8_t offKey;
208 /** Current value offset. */
209 uint8_t offValue;
210 /** Number of keys in the aKeys array. */
211 uint8_t cKeys;
212
213 /** The current key data the user is accessing. */
214 DEVSMCCURKEY CurKey;
215
216 /**
217 * Generic read/write register values.
218 *
219 * The DATA register entry is not used at all. The CMD register entry contains
220 * the state value.
221 */
222 union
223 {
224 /** Index register view. */
225 uint8_t abRegsRW[SMC_REG_COUNT];
226 /** Named register view. */
227 struct
228 {
229 uint8_t abUnknown0[0x04];
230 /** The current state (SMC_PORT_CMD read). */
231 uint8_t bState;
232 uint8_t abUnknown1[0x1e - 0x05];
233 /** The current status code (SMC_PORT_STATUS_CODE). */
234 uint8_t bStatusCode;
235 uint8_t abUnknown2[1];
236 } s;
237 } u;
238
239 /** @name Key data.
240 * @{ */
241 /** OSK0 and OSK1. */
242 char szOsk0And1[64+1];
243 /** $Num - unknown function. */
244 uint8_t bDollaryNumber;
245 /** MSSD - shutdown reason. */
246 uint8_t bShutdownReason;
247 /** NATJ - Ninja action timer job. */
248 uint8_t bNinjaActionTimerJob;
249 /** @} */
250
251 /** The I/O port registration handle. */
252 IOMIOPORTHANDLE hIoPorts;
253} DEVSMC;
254#ifndef _MSC_VER
255AssertCompileMembersAtSameOffset(DEVSMC, u.abRegsRW[SMC_REG_CMD], DEVSMC, u.s.bState);
256AssertCompileMembersAtSameOffset(DEVSMC, u.abRegsRW[SMC_REG_STATUS_CODE], DEVSMC, u.s.bStatusCode);
257#endif
258
259/** Pointer to the SMC state. */
260typedef DEVSMC *PDEVSMC;
261
262#ifndef VBOX_DEVICE_STRUCT_TESTCASE
263
264
265/**
266 * Method for retriving the key value and/or optionally also attributes.
267 *
268 * @returns Apple SMC Status Code.
269 * @param pThis The SMC instance data.
270 * @param pCurKey The current key structure (input / output).
271 * @param bCmd The current command (mostly for getters that also
272 * provides attributes or type info).
273 * @param pKeyDesc Pointer to the key descriptor so that the getter can
274 * service more than once key.
275 */
276typedef DECLCALLBACKTYPE(uint8_t, FNDEVSMCKEYGETTER,(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd,
277 struct DEVSMCKEYDESC const *pKeyDesc));
278
279/**
280 * Method for setting the key value.
281 *
282 * @returns Apple SMC Status Code.
283 * @param pThis The SMC instance data.
284 * @param pCurKey The current key structure (input / output).
285 * @param bCmd The current command (currently not relevant).
286 * @param pKeyDesc Pointer to the key descriptor so that the getter can
287 * service more than once key.
288 */
289typedef DECLCALLBACKTYPE(uint8_t, FNDEVSMCKEYPUTTER,(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd,
290 struct DEVSMCKEYDESC const *pKeyDesc));
291
292/**
293 * Key descriptor.
294 */
295typedef struct DEVSMCKEYDESC
296{
297 /** The key 4 character identifier. */
298 DEVSMC4CHID Key;
299 /** Type 4 character identifier. 0 means the getter will set it dynamically. */
300 DEVSMC4CHID Type;
301 /** Getter method, see FNDEVSMCKEYGETTER. */
302 FNDEVSMCKEYGETTER *pfnGet;
303 /** Putter method, see FNDEVSMCKEYPUTTER. */
304 FNDEVSMCKEYPUTTER *pfnPut;
305 /** The keyvalue size. If 0 the pfnGet/pfnPut will define/check the size. */
306 uint8_t cbValue;
307 /** Attributes. 0 means the getter will set it dynamically. */
308 uint8_t fAttr;
309} DEVSMCKEYDESC;
310/** Pointer to a constant SMC key descriptor. */
311typedef DEVSMCKEYDESC const *PCDEVSMCKEYDESC;
312
313
314/*********************************************************************************************************************************
315* Internal Functions *
316*********************************************************************************************************************************/
317static FNDEVSMCKEYGETTER scmKeyGetOSKs;
318static FNDEVSMCKEYGETTER scmKeyGetKeyCount;
319static FNDEVSMCKEYGETTER scmKeyGetRevision;
320#ifdef VBOX_WITH_SMC_2_x
321static FNDEVSMCKEYGETTER scmKeyGetDollarAddress;
322static FNDEVSMCKEYGETTER scmKeyGetDollarNumber;
323static FNDEVSMCKEYPUTTER scmKeyPutDollarNumber;
324#endif
325static FNDEVSMCKEYGETTER scmKeyGetShutdownReason;
326static FNDEVSMCKEYPUTTER scmKeyPutShutdownReason;
327static FNDEVSMCKEYGETTER scmKeyGetNinjaTimerAction;
328static FNDEVSMCKEYPUTTER scmKeyPutNinjaTimerAction;
329static FNDEVSMCKEYGETTER scmKeyGetOne;
330static FNDEVSMCKEYGETTER scmKeyGetZero;
331
332
333/*********************************************************************************************************************************
334* Global Variables *
335*********************************************************************************************************************************/
336/**
337 * Apple SMC key descriptor table.
338 */
339static const DEVSMCKEYDESC g_aSmcKeys[] =
340{
341 /* Non-enum keys first. */
342 { SMC4CH('O','S','K','0'), SMC4CH('c','h','8','*'), scmKeyGetOSKs, NULL, 32, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_FUNCTION },
343 { SMC4CH('O','S','K','1'), SMC4CH('c','h','8','*'), scmKeyGetOSKs, NULL, 32, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_FUNCTION },
344
345 /* The first enum key is the #KEY value. */
346 { SMC4CH('#','K','E','Y'), SMC4CH('u','i','3','2'), scmKeyGetKeyCount, NULL, 4, SMC_KEY_ATTR_READ },
347# ifdef VBOX_WITH_SMC_2_x
348 { SMC4CH('$','A','d','r'), SMC4CH('u','i','3','2'), scmKeyGetDollarAddress, NULL, 4, SMC_KEY_ATTR_READ },
349 { SMC4CH('$','N','u','m'), SMC4CH('u','i','8',' '), scmKeyGetDollarNumber, scmKeyPutDollarNumber, 1, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_WRITE | SMC_KEY_ATTR_PRIVATE },
350 { SMC4CH('B','E','M','B'), SMC4CH('f','l','a','g'), scmKeyGetOne, NULL, 1, SMC_KEY_ATTR_READ },
351# else
352 { SMC4CH('L','S','O','F'), SMC4CH('f','l','a','g'), scmKeyGetZero, NULL, 1, SMC_KEY_ATTR_READ },
353# endif
354 { SMC4CH('M','S','S','D'), SMC4CH('s','i','8',' '), scmKeyGetShutdownReason, scmKeyPutShutdownReason, 1, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_WRITE | SMC_KEY_ATTR_PRIVATE },
355 /* MSDS is not present on MacPro3,1 nor MacBookPro10,1, so returning not found is fine. */
356# ifdef VBOX_WITH_SMC_2_x
357 { SMC4CH('M','S','T','f'), SMC4CH('u','i','8',' '), scmKeyGetZero, NULL, 1, SMC_KEY_ATTR_READ },
358# endif
359 { SMC4CH('N','A','T','J'), SMC4CH('u','i','8',' '), scmKeyGetNinjaTimerAction, scmKeyPutNinjaTimerAction, 1, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_WRITE | SMC_KEY_ATTR_PRIVATE },
360 { SMC4CH('R','E','V',' '), SMC4CH('{','r','e','v'), scmKeyGetRevision, NULL, 6, SMC_KEY_ATTR_READ },
361/** @todo MSSP, NTOK and more. */
362};
363
364#if defined(IN_RING0) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
365
366/** Do once for the SMC ring-0 static data (g_abOsk0And1, g_fHaveOsk). */
367static RTONCE g_SmcR0Once = RTONCE_INITIALIZER;
368/** Indicates whether we've successfully queried the OSK* keys. */
369static bool g_fHaveOsk = false;
370/** The OSK0 and OSK1 values. */
371static uint8_t g_abOsk0And1[32+32];
372
373
374/**
375 * Waits for the specified state on the host SMC.
376 *
377 * @returns success indicator.
378 * @param bState The desired state.
379 * @param pszWhat What we're currently doing. For the log.
380 */
381static bool devR0SmcWaitHostState(uint8_t bState, const char *pszWhat)
382{
383 uint8_t bCurState = 0; /* (MSC is potentially uninitialized) */
384 for (uint32_t cMsSleep = 1; cMsSleep <= 64; cMsSleep <<= 1)
385 {
386 RTThreadSleep(cMsSleep);
387 bCurState = ASMInU16(SMC_PORT_CMD);
388 if ((bCurState & 0xf) == bState)
389 return true;
390 }
391
392 LogRel(("devR0Smc: %s: bCurState=%#x, wanted %#x.\n", pszWhat, bCurState, bState));
393#if 0
394 uint8_t bCurStatus2 = ASMInU8(SMC_PORT_STATUS_CODE);
395 uint8_t bCurStatus3 = ASMInU8(SMC_PORT_STATUS_CODE);
396 uint16_t wCurStatus3 = ASMInU16(SMC_PORT_STATUS_CODE);
397 uint32_t dwCurStatus3 = ASMInU32(SMC_PORT_STATUS_CODE);
398 LogRel(("SMC: status2=%#x status3=%#x w=%#x dw=%#x\n", bCurStatus2, bCurStatus3, wCurStatus3, dwCurStatus3));
399#endif
400 return false;
401}
402
403
404/**
405 * Reads a key by name from the host SMC.
406 *
407 * @returns success indicator.
408 * @param pszName The key name, must be exactly 4 chars long.
409 * @param pbBuf The output buffer.
410 * @param cbBuf The buffer size. Max 32 bytes.
411 */
412static bool devR0SmcQueryHostKey(const char *pszName, uint8_t *pbBuf, size_t cbBuf)
413{
414 Assert(strlen(pszName) == 4);
415 Assert(cbBuf <= 32);
416 Assert(cbBuf > 0);
417
418 /*
419 * Issue the READ command.
420 */
421 uint32_t cMsSleep = 1;
422 for (;;)
423 {
424 ASMOutU32(SMC_PORT_CMD, SMC_CMD_GET_KEY_VALUE);
425 RTThreadSleep(cMsSleep);
426 uint8_t bCurState = ASMInU8(SMC_PORT_CMD);
427 if ((bCurState & 0xf) == 0xc)
428 break;
429 cMsSleep <<= 1;
430 if (cMsSleep > 64)
431 {
432 LogRel(("devR0Smc: %s: bCurState=%#x, wanted %#x.\n", "cmd", bCurState, 0xc));
433 return false;
434 }
435 }
436
437 /*
438 * Send it the key.
439 */
440 for (unsigned off = 0; off < 4; off++)
441 {
442 ASMOutU8(SMC_PORT_DATA, pszName[off]);
443 if (!devR0SmcWaitHostState(4, "key"))
444 return false;
445 }
446
447 /*
448 * The desired amount of output.
449 */
450 ASMOutU8(SMC_PORT_DATA, (uint8_t)cbBuf);
451
452 /*
453 * Read the output.
454 */
455 for (size_t off = 0; off < cbBuf; off++)
456 {
457 if (!devR0SmcWaitHostState(5, off ? "data" : "len"))
458 return false;
459 pbBuf[off] = ASMInU8(SMC_PORT_DATA);
460 }
461
462 LogRel(("SMC: pbBuf=%.*s\n", cbBuf, pbBuf));
463 return true;
464}
465
466
467/**
468 * RTOnce callback that initializes g_fHaveOsk and g_abOsk0And1.
469 *
470 * @returns VINF_SUCCESS.
471 * @param pvUserIgnored Ignored.
472 */
473static DECLCALLBACK(int) devR0SmcInitOnce(void *pvUserIgnored)
474{
475 g_fHaveOsk = devR0SmcQueryHostKey("OSK0", &g_abOsk0And1[0], 32)
476 && devR0SmcQueryHostKey("OSK1", &g_abOsk0And1[32], 32);
477
478#if 0
479 /*
480 * Dump the device registers.
481 */
482 for (uint16_t uPort = 0x300; uPort < 0x320; uPort ++)
483 LogRel(("SMC: %#06x=%#010x w={%#06x, %#06x}, b={%#04x %#04x %#04x %#04x}\n", uPort,
484 ASMInU32(uPort), ASMInU16(uPort), ASMInU16(uPort + 2),
485 ASMInU8(uPort), ASMInU8(uPort + 1), ASMInU8(uPort +2), ASMInU8(uPort + 3) ));
486#endif
487
488 NOREF(pvUserIgnored);
489 return VINF_SUCCESS;
490}
491
492
493/**
494 * @interface_method_impl{PDMDEVREGR0,pfnRequest}
495 */
496static DECLCALLBACK(int) devR0SmcReqHandler(PPDMDEVINS pDevIns, uint32_t uReq, uint64_t uArg)
497{
498 PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
499 int rc = VERR_INVALID_FUNCTION;
500 RT_NOREF_PV(uArg);
501
502 if (uReq == SMC_CALLR0_READ_OSK)
503 {
504 rc = RTOnce(&g_SmcR0Once, devR0SmcInitOnce, NULL);
505 if ( RT_SUCCESS(rc)
506 && g_fHaveOsk)
507 {
508 AssertCompile(sizeof(g_abOsk0And1) + 1 == sizeof(pThis->szOsk0And1));
509 memcpy(pThis->szOsk0And1, g_abOsk0And1, sizeof(pThis->szOsk0And1) - 1);
510 pThis->szOsk0And1[sizeof(pThis->szOsk0And1) - 1] = '\0';
511 }
512 }
513 return rc;
514}
515
516#endif /* IN_RING0 && (AMD64 || X86) */
517
518#if defined(IN_RING3) && defined(RT_OS_DARWIN)
519
520/**
521 * Preferred method to retrieve the SMC key.
522 *
523 * @param pabKey where to store the key.
524 * @param cbKey size of the buffer.
525 */
526static int getSmcKeyOs(char *pabKey, uint32_t cbKey)
527{
528 /*
529 * Method as described in Amit Singh's article:
530 * http://osxbook.com/book/bonus/chapter7/tpmdrmmyth/
531 */
532 typedef struct
533 {
534 uint32_t key;
535 uint8_t pad0[22];
536 uint32_t datasize;
537 uint8_t pad1[10];
538 uint8_t cmd;
539 uint32_t pad2;
540 uint8_t data[32];
541 } AppleSMCBuffer;
542
543 AssertReturn(cbKey >= 65, VERR_INTERNAL_ERROR);
544
545 io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
546 IOServiceMatching("AppleSMC"));
547 if (!service)
548 return VERR_NOT_FOUND;
549
550 io_connect_t port = (io_connect_t)0;
551 kern_return_t kr = IOServiceOpen(service, mach_task_self(), 0, &port);
552 IOObjectRelease(service);
553
554 if (kr != kIOReturnSuccess)
555 return RTErrConvertFromDarwin(kr);
556
557 AppleSMCBuffer inputStruct = { 0, {0}, 32, {0}, 5, };
558 AppleSMCBuffer outputStruct;
559 size_t cbOutputStruct = sizeof(outputStruct);
560
561 for (int i = 0; i < 2; i++)
562 {
563 inputStruct.key = (uint32_t)(i == 0 ? 'OSK0' : 'OSK1');
564 kr = IOConnectCallStructMethod((mach_port_t)port,
565 (uint32_t)2,
566 (const void *)&inputStruct,
567 sizeof(inputStruct),
568 (void *)&outputStruct,
569 &cbOutputStruct);
570 if (kr != kIOReturnSuccess)
571 {
572 IOServiceClose(port);
573 return RTErrConvertFromDarwin(kr);
574 }
575
576 for (int j = 0; j < 32; j++)
577 pabKey[j + i*32] = outputStruct.data[j];
578 }
579
580 IOServiceClose(port);
581
582 pabKey[64] = 0;
583
584 return VINF_SUCCESS;
585}
586
587#endif /* IN_RING3 && RT_OS_DARWIN */
588
589
590/** @callback_method_impl{FNDEVSMCKEYGETTER, OSK0 and OSK1} */
591static DECLCALLBACK(uint8_t) scmKeyGetOSKs(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
592{
593 RT_NOREF1(bCmd);
594 Assert(SMC4CH_EQ(&pKeyDesc->Key, "OSK0") || SMC4CH_EQ(&pKeyDesc->Key, "OSK1"));
595 const char *pszSrc = pThis->szOsk0And1;
596 if (SMC4CH_EQ(&pKeyDesc->Key, "OSK1"))
597 pszSrc += 32;
598 memcpy(pCurKey->Value.ab, pszSrc, 32);
599 return SMC_STATUS_CD_SUCCESS;
600}
601
602
603/** @callback_method_impl{FNDEVSMCKEYGETTER, \#KEY} */
604static DECLCALLBACK(uint8_t) scmKeyGetKeyCount(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
605{
606 RT_NOREF3(pThis, bCmd, pKeyDesc);
607 Assert(pKeyDesc == &g_aSmcKeys[SMC_KEYIDX_FIRST_ENUM]);
608 uint32_t cKeys = RT_ELEMENTS(g_aSmcKeys) - SMC_KEYIDX_FIRST_ENUM;
609 pCurKey->Value.u32 = RT_H2BE_U32(cKeys);
610 return SMC_STATUS_CD_SUCCESS;
611}
612
613
614/** @callback_method_impl{FNDEVSMCKEYGETTER, REV - Source revision.} */
615static DECLCALLBACK(uint8_t) scmKeyGetRevision(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
616{
617 RT_NOREF3(pThis, bCmd, pKeyDesc);
618#ifdef VBOX_WITH_SMC_2_x
619 pCurKey->Value.ab[0] = 0x02;
620 pCurKey->Value.ab[1] = 0x03;
621 pCurKey->Value.ab[2] = 0x0f;
622 pCurKey->Value.ab[3] = 0x00;
623 pCurKey->Value.ab[4] = 0x00;
624 pCurKey->Value.ab[5] = 0x35;
625#else
626 pCurKey->Value.ab[0] = 0x01;
627 pCurKey->Value.ab[1] = 0x25;
628 pCurKey->Value.ab[2] = 0x0f;
629 pCurKey->Value.ab[3] = 0x00;
630 pCurKey->Value.ab[4] = 0x00;
631 pCurKey->Value.ab[5] = 0x04;
632#endif
633 return SMC_STATUS_CD_SUCCESS;
634}
635
636#ifdef VBOX_WITH_SMC_2_x
637
638/** @callback_method_impl{FNDEVSMCKEYGETTER, $Adr - SMC address.} */
639static DECLCALLBACK(uint8_t) scmKeyGetDollarAddress(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
640{
641 RT_NOREF3(pThis, bCmd, pKeyDesc);
642 pCurKey->Value.u32 = RT_H2BE_U32(SMC_PORT_FIRST);
643 return VINF_SUCCESS;
644}
645
646
647/** @callback_method_impl{FNDEVSMCKEYGETTER, $Num - Some kind of number.} */
648static DECLCALLBACK(uint8_t) scmKeyGetDollarNumber(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
649{
650 RT_NOREF2(bCmd, pKeyDesc);
651 pCurKey->Value.ab[0] = pThis->bDollaryNumber;
652 return VINF_SUCCESS;
653}
654
655/** @callback_method_impl{FNDEVSMCKEYPUTTER, $Num - Some kind of number.} */
656static DECLCALLBACK(uint8_t) scmKeyPutDollarNumber(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
657{
658 RT_NOREF2(bCmd, pKeyDesc);
659 Log(("scmKeyPutDollarNumber: %#x -> %#x\n", pThis->bDollaryNumber, pCurKey->Value.ab[0]));
660 pThis->bDollaryNumber = pCurKey->Value.ab[0];
661 return VINF_SUCCESS;
662}
663
664#endif /* VBOX_WITH_SMC_2_x */
665
666/** @callback_method_impl{FNDEVSMCKEYGETTER, MSSD - Machine Shutdown reason.} */
667static DECLCALLBACK(uint8_t) scmKeyGetShutdownReason(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
668{
669 RT_NOREF2(bCmd, pKeyDesc);
670 pCurKey->Value.ab[0] = pThis->bShutdownReason;
671 return SMC_STATUS_CD_SUCCESS;
672}
673
674
675/** @callback_method_impl{FNDEVSMCKEYPUTTER, MSSD - Machine Shutdown reason.} */
676static DECLCALLBACK(uint8_t) scmKeyPutShutdownReason(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
677{
678 RT_NOREF2(bCmd, pKeyDesc);
679 Log(("scmKeyPutShutdownReason: %#x -> %#x\n", pThis->bShutdownReason, pCurKey->Value.ab[0]));
680 pThis->bShutdownReason = pCurKey->Value.ab[0];
681 return SMC_STATUS_CD_SUCCESS;
682}
683
684
685/** @callback_method_impl{FNDEVSMCKEYGETTER, MSSD - Ninja timer action job.} */
686static DECLCALLBACK(uint8_t)
687scmKeyGetNinjaTimerAction(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
688{
689 RT_NOREF2(bCmd, pKeyDesc);
690 pCurKey->Value.ab[0] = pThis->bNinjaActionTimerJob;
691 return SMC_STATUS_CD_SUCCESS;
692}
693
694
695/** @callback_method_impl{FNDEVSMCKEYPUTTER, NATJ - Ninja timer action job.} */
696static DECLCALLBACK(uint8_t)
697scmKeyPutNinjaTimerAction(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
698{
699 RT_NOREF2(bCmd, pKeyDesc);
700 Log(("scmKeyPutNinjaTimerAction: %#x -> %#x\n", pThis->bNinjaActionTimerJob, pCurKey->Value.ab[0]));
701 pThis->bNinjaActionTimerJob = pCurKey->Value.ab[0];
702 return SMC_STATUS_CD_SUCCESS;
703}
704
705#ifdef VBOX_WITH_SMC_2_x
706
707/** @callback_method_impl{FNDEVSMCKEYGETTER, Generic one getter.} */
708static DECLCALLBACK(uint8_t) scmKeyGetOne(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
709{
710 RT_NOREF2(pThis, bCmd);
711 memset(&pCurKey->Value.ab[0], 0, pKeyDesc->cbValue);
712 pCurKey->Value.ab[pKeyDesc->cbValue - 1] = 1;
713 return SMC_STATUS_CD_SUCCESS;
714}
715
716#endif /* VBOX_WITH_SMC_2_x */
717
718/** @callback_method_impl{FNDEVSMCKEYGETTER, Generic zero getter.} */
719static DECLCALLBACK(uint8_t) scmKeyGetZero(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
720{
721 RT_NOREF2(pThis, bCmd);
722 memset(&pCurKey->Value.ab[0], 0, pKeyDesc->cbValue);
723 return SMC_STATUS_CD_SUCCESS;
724}
725
726
727/**
728 * Looks up a key and copies its value and attributes into the CurKey.
729 *
730 * @returns Key index on success, UINT32_MAX on failure.
731 * @param uKeyValue The key value (DEVSMC4CHID.u32).
732 */
733static uint32_t smcKeyLookup(uint32_t uKeyValue)
734{
735 uint32_t iKey = RT_ELEMENTS(g_aSmcKeys);
736 while (iKey-- > 0)
737 if (g_aSmcKeys[iKey].Key.u32 == uKeyValue)
738 return iKey;
739 return UINT32_MAX;
740}
741
742
743/**
744 * Looks up a key and copies its value and attributes into the CurKey.
745 *
746 * @returns Apple SMC Status Code.
747 * @param pThis The SMC instance data.
748 */
749static uint8_t smcKeyGetByName(PDEVSMC pThis)
750{
751 uint8_t bRc;
752#ifdef LOG_ENABLED
753 uint32_t const uKeyValueLog = RT_H2LE_U32(pThis->CurKey.Key.u32);
754#endif
755 uint32_t iKey = smcKeyLookup(pThis->CurKey.Key.u32);
756 if (iKey != UINT32_MAX)
757 {
758 if ( g_aSmcKeys[iKey].cbValue == pThis->CurKey.cbValue
759 || !g_aSmcKeys[iKey].cbValue)
760 {
761 pThis->CurKey.Type = g_aSmcKeys[iKey].Type;
762 pThis->CurKey.fAttr = g_aSmcKeys[iKey].fAttr;
763 RT_ZERO(pThis->CurKey.Value);
764 if (g_aSmcKeys[iKey].pfnGet)
765 {
766 bRc = g_aSmcKeys[iKey].pfnGet(pThis, &pThis->CurKey, pThis->bCmd, &g_aSmcKeys[iKey]);
767 if (bRc == SMC_STATUS_CD_SUCCESS)
768 {
769 LogFlow(("smcKeyGetByName: key=%4.4s value=%.*Rhxs\n",
770 &uKeyValueLog, pThis->CurKey.cbValue, &pThis->CurKey.Value));
771 return SMC_STATUS_CD_SUCCESS;
772 }
773
774 Log(("smcKeyGetByName: key=%4.4s getter failed! bRc=%#x\n", &uKeyValueLog, bRc));
775 }
776 else
777 {
778 Log(("smcKeyGetByName: key=%4.4s is not readable!\n", &uKeyValueLog));
779 bRc = SMC_STATUS_CD_KEY_NOT_READABLE;
780 }
781 }
782 else
783 {
784 Log(("smcKeyGetByName: Wrong value size; user=%#x smc=%#x key=%4.4s !\n",
785 pThis->CurKey.cbValue, g_aSmcKeys[iKey].cbValue, &uKeyValueLog));
786 bRc = SMC_STATUS_CD_KEY_SIZE_MISMATCH;
787 }
788 }
789 else
790 {
791 Log(("smcKeyGetByName: Key not found! key=%4.4s size=%#x\n", &uKeyValueLog, pThis->CurKey.cbValue));
792 bRc = SMC_STATUS_CD_KEY_NOT_FOUND;
793 }
794
795 RT_ZERO(pThis->CurKey);
796 return bRc;
797}
798
799
800/**
801 * Looks up a key by index and copies its name (and attributes) into the CurKey.
802 *
803 * @returns Apple SMC Status Code.
804 * @param pThis The SMC instance data.
805 */
806static uint8_t smcKeyGetByIndex(PDEVSMC pThis)
807{
808 uint8_t bRc;
809 uint32_t iKey = RT_BE2H_U32(pThis->CurKey.Key.u32);
810 if (iKey < RT_ELEMENTS(g_aSmcKeys) - SMC_KEYIDX_FIRST_ENUM)
811 {
812 pThis->CurKey.Key = g_aSmcKeys[iKey].Key;
813 pThis->CurKey.Type = g_aSmcKeys[iKey].Type;
814 pThis->CurKey.fAttr = g_aSmcKeys[iKey].fAttr;
815 pThis->CurKey.cbValue = g_aSmcKeys[iKey].cbValue;
816 RT_ZERO(pThis->CurKey.Value);
817 Log(("smcKeyGetByIndex: %#x -> %c%c%c%c\n", iKey,
818 pThis->CurKey.Key.ab[3], pThis->CurKey.Key.ab[2], pThis->CurKey.Key.ab[1], pThis->CurKey.Key.ab[0]));
819 bRc = SMC_STATUS_CD_SUCCESS;
820 }
821 else
822 {
823 Log(("smcKeyGetByIndex: Key out or range: %#x, max %#x\n", iKey, RT_ELEMENTS(g_aSmcKeys) - SMC_KEYIDX_FIRST_ENUM));
824 bRc = SMC_STATUS_CD_KEY_NOT_FOUND;
825 }
826 return bRc;
827}
828
829
830/**
831 * Looks up a key by index and copies its attributes into the CurKey.
832 *
833 * @returns Apple SMC Status Code.
834 * @param pThis The SMC instance data.
835 */
836static uint8_t smcKeyGetAttrByName(PDEVSMC pThis)
837{
838 uint8_t bRc;
839#ifdef LOG_ENABLED
840 uint32_t const uKeyValueLog = RT_H2LE_U32(pThis->CurKey.Key.u32);
841#endif
842 uint32_t iKey = smcKeyLookup(pThis->CurKey.Key.u32);
843 if (iKey != UINT32_MAX)
844 {
845 pThis->CurKey.Type = g_aSmcKeys[iKey].Type;
846 pThis->CurKey.fAttr = g_aSmcKeys[iKey].fAttr;
847 pThis->CurKey.cbValue = g_aSmcKeys[iKey].cbValue;
848 RT_ZERO(pThis->CurKey.Value);
849 if (g_aSmcKeys[iKey].cbValue)
850 bRc = SMC_STATUS_CD_SUCCESS;
851 else
852 bRc = g_aSmcKeys[iKey].pfnGet(pThis, &pThis->CurKey, pThis->bCmd, &g_aSmcKeys[iKey]);
853 if (bRc == SMC_STATUS_CD_SUCCESS)
854 {
855 LogFlow(("smcKeyGetAttrByName: key=%4.4s value=%.*Rhxs\n",
856 &uKeyValueLog, pThis->CurKey.cbValue, &pThis->CurKey.Value));
857 return SMC_STATUS_CD_SUCCESS;
858 }
859
860 Log(("smcKeyGetAttrByName: key=%4.4s getter failed! bRc=%#x\n", &uKeyValueLog, bRc));
861 }
862 else
863 {
864 Log(("smcKeyGetAttrByName: Key not found! key=%4.4s size=%#x\n", &uKeyValueLog, pThis->CurKey.cbValue));
865 bRc = SMC_STATUS_CD_KEY_NOT_FOUND;
866 }
867
868 RT_ZERO(pThis->CurKey);
869 return bRc;
870}
871
872
873static uint8_t smcKeyPutPrepare(PDEVSMC pThis)
874{
875 RT_NOREF1(pThis);
876 return 0;
877}
878
879static uint8_t smcKeyPutValue(PDEVSMC pThis)
880{
881 RT_NOREF1(pThis);
882 return 0;
883}
884
885
886/**
887 * Data register read.
888 *
889 * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
890 * @param uReg The register number.
891 * @param pbValue Where to return the value.
892 */
893static VBOXSTRICTRC smcRegData_r(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue)
894{
895 RT_NOREF1(uReg);
896 switch (pThis->bCmd)
897 {
898 case SMC_CMD_GET_KEY_VALUE:
899 if ( pThis->u.s.bState == 0x05
900 && pThis->offValue < pThis->CurKey.cbValue)
901 {
902 *pbValue = pThis->CurKey.Value.ab[pThis->offValue];
903 if (++pThis->offValue >= pThis->CurKey.cbValue)
904 pThis->u.s.bState = 0x00;
905 pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
906 }
907 else
908 {
909 Log(("smcRegData_r: Reading too much or at wrong time during SMC_CMD_GET_KEY_INFO! bState=%#x offValue=%#x\n",
910 pThis->u.s.bState, pThis->offValue));
911 pThis->u.s.bState = 0x00;
912 pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA; /** @todo check status code */
913 }
914 break;
915
916 case SMC_CMD_GET_KEY_INFO:
917 if ( pThis->u.s.bState == 0x05
918 && pThis->offValue < 6)
919 {
920 if (pThis->offValue == 0)
921 *pbValue = pThis->CurKey.cbValue;
922 else if (pThis->offValue < 1 + 4)
923 *pbValue = pThis->CurKey.Type.ab[pThis->offValue - 1];
924 else
925 *pbValue = pThis->CurKey.fAttr;
926 if (++pThis->offValue >= 6)
927 pThis->u.s.bState = 0x00;
928 pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
929 }
930 else
931 {
932 Log(("smcRegData_r: Reading too much or at wrong time during SMC_CMD_GET_KEY_INFO! bState=%#x offValue=%#x\n",
933 pThis->u.s.bState, pThis->offValue));
934 pThis->u.s.bState = 0x00;
935 pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA; /** @todo check status code */
936 }
937 break;
938
939 case SMC_CMD_GET_KEY_BY_INDEX:
940 if ( pThis->u.s.bState == 0x05
941 && pThis->offValue < sizeof(pThis->CurKey.Key))
942 {
943 *pbValue = pThis->CurKey.Key.ab[pThis->offValue];
944 if (++pThis->offValue >= sizeof(pThis->CurKey.Key))
945 pThis->u.s.bState = 0x00;
946 pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
947 }
948 else
949 {
950 Log(("smcRegData_r: Reading too much or at wrong time during GET_KEY_BY_INDEX! bState=%#x offValue=%#x\n",
951 pThis->u.s.bState, pThis->offValue));
952 pThis->u.s.bState = 0x00;
953 pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA; /** @todo check status code */
954 }
955 break;
956
957 case SMC_CMD_PUT_KEY:
958 Log(("smcRegData_r: Attempting to read data during PUT_KEY!\n"));
959 *pbValue = 0xff;
960 pThis->u.s.bState = 0;
961 pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
962 break;
963
964 default:
965 Log(("smcRegData_r: Unknown command attempts reading data\n"));
966 *pbValue = 0xff;
967 pThis->u.s.bState = 0;
968 pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
969 break;
970 }
971
972 return VINF_SUCCESS;
973}
974
975
976/**
977 * Data register write.
978 *
979 * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
980 * @param uReg The register number.
981 * @param bValue The value being written.
982 */
983static VBOXSTRICTRC smcRegData_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
984{
985 RT_NOREF1(uReg);
986 switch (pThis->bCmd)
987 {
988 /*
989 * Get or put key value.
990 *
991 * 5 bytes written, first 4 is the key the 5th is the value size. In
992 * the case of a put the value bytes are then written, while a get will
993 * read the value bytes.
994 */
995 case SMC_CMD_GET_KEY_VALUE:
996 case SMC_CMD_PUT_KEY:
997 if (pThis->offKey < 4)
998 {
999 /* Key byte. */
1000 pThis->CurKey.Key.ab[pThis->offKey++] = bValue;
1001 pThis->u.s.bState = 0x04;
1002 pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
1003 }
1004 else if (pThis->offKey == 4)
1005 {
1006 /* Data length. */
1007 pThis->u.s.bState = 0;
1008 if (bValue <= sizeof(pThis->CurKey.Value))
1009 {
1010 pThis->CurKey.cbValue = bValue;
1011 pThis->offKey = 5;
1012 Assert(pThis->offValue == 0);
1013
1014 if (pThis->bCmd == SMC_CMD_GET_KEY_VALUE)
1015 pThis->u.s.bStatusCode = smcKeyGetByName(pThis);
1016 else
1017 pThis->u.s.bStatusCode = smcKeyPutPrepare(pThis);
1018 if (pThis->u.s.bStatusCode == SMC_STATUS_CD_SUCCESS)
1019 pThis->u.s.bState = 0x05;
1020 }
1021 else
1022 {
1023 Log(("smcRegData_w: Guest attempts to get/put too many value bytes: %#x (max %#x)!\n",
1024 bValue, sizeof(pThis->CurKey.Value)));
1025 pThis->u.s.bStatusCode = SMC_STATUS_CD_KEY_SIZE_MISMATCH; /** @todo check this case! */
1026 }
1027 }
1028 else if ( pThis->bCmd == SMC_CMD_PUT_KEY
1029 && pThis->offValue < pThis->CurKey.cbValue)
1030 {
1031 /* More value bytes for put key action. */
1032 pThis->CurKey.Value.ab[pThis->offValue++] = bValue;
1033 if (pThis->offValue != pThis->CurKey.cbValue)
1034 pThis->u.s.bState = 0x05;
1035 else
1036 {
1037 pThis->u.s.bState = 0x00;
1038 pThis->u.s.bStatusCode = smcKeyPutValue(pThis);
1039 }
1040 }
1041 else
1042 {
1043 Log(("smcRegData_w: Writing too much data on %s command!\n", pThis->bCmd == SMC_CMD_PUT_KEY ? "put" : "get"));
1044 pThis->u.s.bState = 0x00;
1045 pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
1046 }
1047 break;
1048
1049 /*
1050 * Get key info and key by index seems to take action after the last
1051 * key char is written. They then both go into a data reading phase.
1052 */
1053 case SMC_CMD_GET_KEY_INFO:
1054 case SMC_CMD_GET_KEY_BY_INDEX:
1055 if (pThis->offKey < 4)
1056 {
1057 pThis->CurKey.Key.ab[pThis->offKey] = bValue;
1058 if (++pThis->offKey == 4)
1059 {
1060 if (pThis->bCmd == SMC_CMD_GET_KEY_BY_INDEX)
1061 pThis->u.s.bStatusCode = smcKeyGetByIndex(pThis);
1062 else
1063 pThis->u.s.bStatusCode = smcKeyGetAttrByName(pThis);
1064 pThis->u.s.bState = pThis->u.s.bStatusCode == SMC_STATUS_CD_SUCCESS ? 0x05 : 0x00;
1065 }
1066 else
1067 {
1068 pThis->u.s.bState = 0x04;
1069 pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
1070 }
1071 }
1072 else
1073 {
1074 Log(("smcRegData_w: Writing data beyond 5th byte on get %s command!\n",
1075 pThis->bCmd == SMC_CMD_GET_KEY_INFO ? "info" : "by index"));
1076 pThis->u.s.bState = 0x00;
1077 pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
1078 }
1079 break;
1080
1081 default:
1082 Log(("smcRegData_w: Unknown command %#x!\n", bValue));
1083 pThis->u.s.bState = 0x00; /** @todo Check statuses with real HW. */
1084 pThis->u.s.bStatusCode = SMC_STATUS_CD_BAD_COMMAND;
1085 break;
1086 }
1087 return VINF_SUCCESS;
1088}
1089
1090
1091/**
1092 * Command register write.
1093 *
1094 * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
1095 * @param uReg The register number.
1096 * @param bValue The value being written.
1097 */
1098static VBOXSTRICTRC smcRegCmd_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
1099{
1100 LogFlow(("smcRegCmd_w: New command: %#x (old=%#x)\n", bValue, pThis->bCmd)); NOREF(uReg);
1101
1102 pThis->bCmd = bValue;
1103
1104 /* Validate the command. */
1105 switch (bValue)
1106 {
1107 case SMC_CMD_GET_KEY_VALUE:
1108 case SMC_CMD_PUT_KEY:
1109 case SMC_CMD_GET_KEY_BY_INDEX:
1110 case SMC_CMD_GET_KEY_INFO:
1111 pThis->u.s.bState = 0x0c;
1112 pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
1113 break;
1114
1115 default:
1116 Log(("SMC: Unknown command %#x!\n", bValue));
1117 pThis->u.s.bState = 0x00; /** @todo Check state with real HW. */
1118 pThis->u.s.bStatusCode = SMC_STATUS_CD_BAD_COMMAND;
1119 break;
1120 }
1121
1122 /* Reset the value/key related state. */
1123 pThis->offKey = 0;
1124 pThis->offValue = 0;
1125 pThis->CurKey.Key.u32 = 0;
1126 pThis->CurKey.cbValue = 0;
1127
1128 return VINF_SUCCESS;
1129}
1130
1131
1132/**
1133 * Generic register write.
1134 *
1135 * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
1136 * @param uReg The register number.
1137 * @param bValue The value being written.
1138 */
1139static VBOXSTRICTRC smcRegGen_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
1140{
1141 Log(("smcRegGen_w: %#04x: %#x -> %#x (write)\n", uReg, pThis->u.abRegsRW[uReg], bValue));
1142 pThis->u.abRegsRW[uReg] = bValue;
1143 return VINF_SUCCESS;
1144}
1145
1146
1147/**
1148 * Read from register that isn't writable and reads as 0xFF.
1149 *
1150 * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
1151 * @param uReg The register number.
1152 * @param pbValue Where to return the value.
1153 */
1154static VBOXSTRICTRC smcRegGen_r(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue)
1155{
1156 Log(("smcRegGen_r: %#04x: %#x (read)\n", uReg, pThis->u.abRegsRW[uReg]));
1157 *pbValue = pThis->u.abRegsRW[uReg];
1158 return VINF_SUCCESS;
1159}
1160
1161
1162/**
1163 * Write to register that isn't writable and reads as 0xFF.
1164 *
1165 * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
1166 * @param uReg The register number.
1167 * @param bValue The value being written.
1168 */
1169static VBOXSTRICTRC smcRegFF_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
1170{
1171 RT_NOREF3(pThis, uReg, bValue);
1172 Log(("SMC: %#04x: Writing %#x to unknown register!\n", uReg, bValue));
1173 return VINF_SUCCESS;
1174}
1175
1176
1177/**
1178 * Read from register that isn't writable and reads as 0xFF.
1179 *
1180 * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
1181 * @param uReg The register number.
1182 * @param pbValue Where to return the value.
1183 */
1184static VBOXSTRICTRC smcRegFF_r(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue)
1185{
1186 RT_NOREF2(pThis, uReg);
1187 Log(("SMC: %#04x: Reading from unknown register!\n", uReg));
1188 *pbValue = 0xff;
1189 return VINF_SUCCESS;
1190}
1191
1192
1193
1194/**
1195 * SMC register handlers (indexed by relative I/O port).
1196 *
1197 * The device seems to be all byte registers and will split wider
1198 * accesses between registers like if it was MMIO. To better illustrate it
1199 * here is the output of the code in devR0SmcInitOnce on a MacPro3,1:
1200 * @verbatim
1201 * SMC: 0x0300=0xffffff63 w={0xff63, 0xffff}, b={0x63 0xff 0xff 0xff}
1202 * SMC: 0x0301=0x0cffffff w={0xffff, 0x0cff}, b={0xff 0xff 0xff 0x0c}
1203 * SMC: 0x0302=0xff0cffff w={0xffff, 0xff0c}, b={0xff 0xff 0x0c 0xff}
1204 * SMC: 0x0303=0xffff0cff w={0x0cff, 0xffff}, b={0xff 0x0c 0xff 0xff}
1205 * SMC: 0x0304=0xffffff0c w={0xff0c, 0xffff}, b={0x0c 0xff 0xff 0xff}
1206 * SMC: 0x0305=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1207 * SMC: 0x0306=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1208 * SMC: 0x0307=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1209 * SMC: 0x0308=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1210 * SMC: 0x0309=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1211 * SMC: 0x030a=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1212 * SMC: 0x030b=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1213 * SMC: 0x030c=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
1214 * SMC: 0x030d=0x00ffffff w={0xffff, 0x00ff}, b={0xff 0xff 0xff 0x00}
1215 * SMC: 0x030e=0x0000ffff w={0xffff, 0x0000}, b={0xff 0xff 0x00 0x00}
1216 * SMC: 0x030f=0x000000ff w={0x00ff, 0x0000}, b={0xff 0x00 0x00 0x00}
1217 * SMC: 0x0310=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1218 * SMC: 0x0311=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1219 * SMC: 0x0312=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1220 * SMC: 0x0313=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1221 * SMC: 0x0314=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1222 * SMC: 0x0315=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1223 * SMC: 0x0316=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1224 * SMC: 0x0317=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1225 * SMC: 0x0318=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
1226 * SMC: 0x0319=0xbe000000 w={0x0000, 0xbe00}, b={0x00 0x00 0x00 0xbe}
1227 * SMC: 0x031a=0xbabe0000 w={0x0000, 0xbabe}, b={0x00 0x00 0xbe 0xba}
1228 * SMC: 0x031b=0x00babe00 w={0xbe00, 0x00ba}, b={0x00 0xbe 0xba 0x00}
1229 * SMC: 0x031c=0xbe00babe w={0xbabe, 0xbe00}, b={0xbe 0xba 0x00 0xbe}
1230 * SMC: 0x031d=0xffbe00ba w={0x00ba, 0xffbe}, b={0xba 0x00 0xbe 0xff}
1231 * SMC: 0x031e=0xffffbe00 w={0xbe00, 0xffff}, b={0x00 0xbe 0xff 0xff}
1232 * SMC: 0x031f=0xffffffbe w={0xffbe, 0xffff}, b={0xbe 0xff 0xff 0xff}
1233 * @endverbatim
1234 *
1235 * The last dword is writable (0xbeXXbabe) where in the register at 0x1e is some
1236 * kind of status register for qualifying search failures and the like and will
1237 * be cleared under certain conditions. The whole dword can be written and read
1238 * back unchanged, according to my experiments. The 0x00 and 0x04 registers
1239 * does not read back what is written.
1240 *
1241 * My guess is that the 0xff values indicates ports that are not writable and
1242 * hardwired to 0xff, while the other values indicates ports that can be written
1243 * to and normally read back as written. I'm not going to push my luck too far
1244 * wrt to exact behavior until I see the guest using the registers.
1245 */
1246static const struct
1247{
1248 VBOXSTRICTRC (*pfnWrite)(PDEVSMC pThis, uint8_t uReg, uint8_t bValue);
1249 VBOXSTRICTRC (*pfnRead)(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue);
1250} g_aSmcRegs[SMC_REG_COUNT] =
1251{
1252 /* [0x00] = */ { smcRegData_w, smcRegData_r },
1253 /* [0x01] = */ { smcRegFF_w, smcRegFF_r },
1254 /* [0x02] = */ { smcRegFF_w, smcRegFF_r },
1255 /* [0x03] = */ { smcRegFF_w, smcRegFF_r },
1256 /* [0x04] = */ { smcRegCmd_w, smcRegGen_r },
1257 /* [0x05] = */ { smcRegFF_w, smcRegFF_r },
1258 /* [0x06] = */ { smcRegFF_w, smcRegFF_r },
1259 /* [0x07] = */ { smcRegFF_w, smcRegFF_r },
1260 /* [0x08] = */ { smcRegFF_w, smcRegFF_r },
1261 /* [0x09] = */ { smcRegFF_w, smcRegFF_r },
1262 /* [0x0a] = */ { smcRegFF_w, smcRegFF_r },
1263 /* [0x0b] = */ { smcRegFF_w, smcRegFF_r },
1264 /* [0x0c] = */ { smcRegFF_w, smcRegFF_r },
1265 /* [0x0d] = */ { smcRegFF_w, smcRegFF_r },
1266 /* [0x0e] = */ { smcRegFF_w, smcRegFF_r },
1267 /* [0x0f] = */ { smcRegFF_w, smcRegFF_r },
1268 /* [0x10] = */ { smcRegGen_w, smcRegGen_r },
1269 /* [0x11] = */ { smcRegGen_w, smcRegGen_r },
1270 /* [0x12] = */ { smcRegGen_w, smcRegGen_r },
1271 /* [0x13] = */ { smcRegGen_w, smcRegGen_r },
1272 /* [0x14] = */ { smcRegGen_w, smcRegGen_r },
1273 /* [0x15] = */ { smcRegGen_w, smcRegGen_r },
1274 /* [0x16] = */ { smcRegGen_w, smcRegGen_r },
1275 /* [0x17] = */ { smcRegGen_w, smcRegGen_r },
1276 /* [0x18] = */ { smcRegGen_w, smcRegGen_r },
1277 /* [0x19] = */ { smcRegGen_w, smcRegGen_r },
1278 /* [0x1a] = */ { smcRegGen_w, smcRegGen_r },
1279 /* [0x1b] = */ { smcRegGen_w, smcRegGen_r },
1280 /* [0x1c] = */ { smcRegGen_w, smcRegGen_r },
1281 /* [0x1d] = */ { smcRegGen_w, smcRegGen_r },
1282 /* [0x1e] = */ { smcRegGen_w, smcRegGen_r },
1283 /* [0x1f] = */ { smcRegGen_w, smcRegGen_r },
1284};
1285
1286
1287/**
1288 * @callback_method_impl{FNIOMIOPORTNEWOUT}
1289 */
1290static DECLCALLBACK(VBOXSTRICTRC) smcIoPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
1291{
1292 RT_NOREF1(pvUser);
1293#ifndef IN_RING3
1294 if (cb > 1)
1295 {
1296 Log3(("smcIoPortWrite: %#04x write access: %#x (LB %u) -> ring-3\n", offPort, u32, cb));
1297 return VINF_IOM_R3_IOPORT_WRITE;
1298 }
1299#endif
1300#ifdef LOG_ENABLED
1301 RTIOPORT const offPortLog = offPort;
1302 unsigned const cbLog = cb;
1303#endif
1304
1305 /*
1306 * The first register, usually only one is accessed.
1307 */
1308 PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
1309 AssertReturn(offPort < RT_ELEMENTS(g_aSmcRegs), VERR_INTERNAL_ERROR_3); /* impossible*/
1310 VBOXSTRICTRC rc = g_aSmcRegs[offPort].pfnWrite(pThis, offPort, u32);
1311
1312 /*
1313 * On the off chance that multiple registers are being read.
1314 */
1315 if (cb > 1)
1316 {
1317 while (cb > 1 && offPort < SMC_REG_COUNT - 1)
1318 {
1319 cb--;
1320 offPort++;
1321 u32 >>= 8;
1322 VBOXSTRICTRC rc2 = g_aSmcRegs[offPort].pfnWrite(pThis, offPort, u32);
1323 if (rc2 != VINF_SUCCESS)
1324 {
1325 if ( rc == VINF_SUCCESS
1326 || (RT_FAILURE(rc2) && RT_SUCCESS(rc))
1327 || (rc2 < rc && RT_SUCCESS(rc2) && RT_SUCCESS(rc)))
1328 rc = rc2;
1329 }
1330 }
1331 }
1332
1333 LogFlow(("smcIoPortWrite: %#04x write access: %#x (LB %u) rc=%Rrc\n", offPortLog, u32, cbLog, VBOXSTRICTRC_VAL(rc) ));
1334 return rc;
1335}
1336
1337
1338/**
1339 * @callback_method_impl{FNIOMIOPORTNEWIN}
1340 */
1341static DECLCALLBACK(VBOXSTRICTRC) smcIoPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
1342{
1343 RT_NOREF1(pvUser);
1344#ifndef IN_RING3
1345 if (cb > 1)
1346 return VINF_IOM_R3_IOPORT_READ;
1347#endif
1348
1349 PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
1350#ifdef LOG_ENABLED
1351 RTIOPORT const offPortLog = offPort;
1352 unsigned const cbLog = cb;
1353#endif
1354
1355 /*
1356 * The first register, usually only one is accessed.
1357 */
1358 AssertReturn(offPort < RT_ELEMENTS(g_aSmcRegs), VERR_INTERNAL_ERROR_3); /* impossible*/
1359 Log2(("smcIoPortRead: %#04x read access: LB %u\n", offPort, cb));
1360 uint8_t bValue = 0xff;
1361 VBOXSTRICTRC rc = g_aSmcRegs[offPort].pfnRead(pThis, offPort, &bValue);
1362 *pu32 = bValue;
1363
1364 /*
1365 * On the off chance that multiple registers are being read.
1366 */
1367 if (cb > 1)
1368 {
1369 do
1370 {
1371 cb--;
1372 offPort++;
1373 bValue = 0xff;
1374 if (offPort < SMC_REG_COUNT)
1375 {
1376 VBOXSTRICTRC rc2 = g_aSmcRegs[offPort].pfnRead(pThis, offPort, &bValue);
1377 if (rc2 != VINF_SUCCESS)
1378 {
1379 if ( rc == VINF_SUCCESS
1380 || (RT_FAILURE(rc2) && RT_SUCCESS(rc))
1381 || (rc2 < rc && RT_SUCCESS(rc2) && RT_SUCCESS(rc)))
1382 rc = rc2;
1383 }
1384 }
1385 *pu32 |= (uint32_t)bValue << ((4 - cb) * 8);
1386 } while (cb > 1);
1387 }
1388 LogFlow(("smcIoPortRead: %#04x read access: %#x (LB %u) rc=%Rrc\n", offPortLog, *pu32, cbLog, VBOXSTRICTRC_VAL(rc)));
1389 return rc;
1390}
1391
1392#ifdef IN_RING3
1393
1394/** @callback_method_impl{FNSSMDEVSAVEEXEC} */
1395static DECLCALLBACK(int) smcR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1396{
1397 PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
1398 RT_NOREF2(pSSM, pThis);
1399
1400 /** @todo */
1401
1402 return VINF_SUCCESS;
1403}
1404
1405
1406/** @callback_method_impl{FNSSMDEVLOADEXEC} */
1407static DECLCALLBACK(int) smcR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1408{
1409 PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
1410 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1411 RT_NOREF2(pSSM, pThis);
1412
1413 /* Fend off unsupported versions. */
1414 if ( uVersion != SMC_SAVED_STATE_VERSION
1415#if SMC_SAVED_STATE_VERSION != SMC_SAVED_STATE_VERSION_BAKA
1416 && uVersion != SMC_SAVED_STATE_VERSION_BAKA
1417#endif
1418 && uVersion != SMC_SAVED_STATE_VERSION_BAKA + 1)
1419 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1420
1421 /*
1422 * Do the actual restoring.
1423 */
1424 if (uVersion == SMC_SAVED_STATE_VERSION)
1425 {
1426 /** @todo */
1427 }
1428
1429 return VINF_SUCCESS;
1430}
1431
1432
1433/**
1434 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1435 */
1436static DECLCALLBACK(int) smcR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1437{
1438 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1439 PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
1440 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1441
1442 Assert(iInstance == 0); RT_NOREF1(iInstance);
1443
1444 /*
1445 * Init the data.
1446 */
1447 pThis->bDollaryNumber = 1;
1448 pThis->bShutdownReason = 3; /* STOP_CAUSE_POWERKEY_GOOD_CODE */
1449
1450 /*
1451 * Validate configuration.
1452 */
1453 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "DeviceKey|GetKeyFromRealSMC", "");
1454
1455 /*
1456 * Read configuration.
1457 */
1458
1459 /* The DeviceKey sets OSK0 and OSK1. */
1460 int rc = pHlp->pfnCFGMQueryStringDef(pCfg, "DeviceKey", pThis->szOsk0And1, sizeof(pThis->szOsk0And1), "");
1461 if (RT_FAILURE(rc))
1462 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1463 N_("Configuration error: Querying \"DeviceKey\" as a string failed"));
1464
1465 /* Query the key from the OS / real hardware if asked to do so. */
1466 bool fGetKeyFromRealSMC;
1467 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "GetKeyFromRealSMC", &fGetKeyFromRealSMC, false);
1468 if (RT_FAILURE(rc))
1469 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1470 N_("Configuration error: Querying \"GetKeyFromRealSMC\" as a boolean failed"));
1471 if (fGetKeyFromRealSMC)
1472 {
1473# ifdef RT_OS_DARWIN
1474 rc = getSmcKeyOs(pThis->szOsk0And1, sizeof(pThis->szOsk0And1));
1475 if (RT_FAILURE(rc))
1476 {
1477 LogRel(("SMC: Retrieving the SMC key from the OS failed (%Rrc), trying to read it from hardware\n", rc));
1478# endif
1479 rc = PDMDevHlpCallR0(pDevIns, SMC_CALLR0_READ_OSK, 0 /*uArg*/);
1480 if (RT_SUCCESS(rc))
1481 LogRel(("SMC: Successfully retrieved the SMC key from hardware\n"));
1482 else
1483 LogRel(("SMC: Retrieving the SMC key from hardware failed(%Rrc)\n", rc));
1484# ifdef RT_OS_DARWIN
1485 }
1486 else
1487 LogRel(("SMC: Successfully retrieved the SMC key from the OS\n"));
1488# endif
1489 if (RT_FAILURE(rc))
1490 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1491 N_("Failed to query SMC value from the host"));
1492 }
1493
1494 /*
1495 * Register I/O Ports
1496 */
1497 rc = PDMDevHlpIoPortCreateAndMap(pDevIns, SMC_PORT_FIRST, SMC_REG_COUNT, smcIoPortWrite, smcIoPortRead,
1498 "SMC data port", NULL, &pThis->hIoPorts);
1499 AssertRCReturn(rc, rc);
1500
1501 /** @todo Newer versions (2.03) have an MMIO mapping as well (ACPI). */
1502
1503
1504 /*
1505 * Saved state.
1506 */
1507 rc = PDMDevHlpSSMRegister(pDevIns, SMC_SAVED_STATE_VERSION, sizeof(*pThis), smcR3SaveExec, smcR3LoadExec);
1508 if (RT_FAILURE(rc))
1509 return rc;
1510
1511 return VINF_SUCCESS;
1512}
1513
1514#else /* !IN_RING3 */
1515
1516/**
1517 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
1518 */
1519static DECLCALLBACK(int) smcRZConstruct(PPDMDEVINS pDevIns)
1520{
1521 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1522 PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
1523
1524 int rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPorts, smcIoPortWrite, smcIoPortRead, NULL /*pvUser*/);
1525 AssertRCReturn(rc, rc);
1526 RT_NOREF(pThis);
1527
1528 return VINF_SUCCESS;
1529}
1530
1531#endif /* !IN_RING3 */
1532
1533
1534/**
1535 * The device registration structure.
1536 */
1537const PDMDEVREG g_DeviceSmc =
1538{
1539 /* .u32Version = */ PDM_DEVREG_VERSION,
1540 /* .uReserved0 = */ 0,
1541 /* .szName = */ "smc",
1542 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
1543 /* .fClass = */ PDM_DEVREG_CLASS_ARCH,
1544 /* .cMaxInstances = */ 1,
1545 /* .uSharedVersion = */ 42,
1546 /* .cbInstanceShared = */ sizeof(DEVSMC),
1547 /* .cbInstanceCC = */ 0,
1548 /* .cbInstanceRC = */ 0,
1549 /* .cMaxPciDevices = */ 0,
1550 /* .cMaxMsixVectors = */ 0,
1551 /* .pszDescription = */ "Apple System Management Controller",
1552#if defined(IN_RING3)
1553 /* .pszRCMod = */ "VBoxDDRC.rc",
1554 /* .pszR0Mod = */ "VBoxDDR0.r0",
1555 /* .pfnConstruct = */ smcR3Construct,
1556 /* .pfnDestruct = */ NULL,
1557 /* .pfnRelocate = */ NULL,
1558 /* .pfnMemSetup = */ NULL,
1559 /* .pfnPowerOn = */ NULL,
1560 /* .pfnReset = */ NULL,
1561 /* .pfnSuspend = */ NULL,
1562 /* .pfnResume = */ NULL,
1563 /* .pfnAttach = */ NULL,
1564 /* .pfnDetach = */ NULL,
1565 /* .pfnQueryInterface = */ NULL,
1566 /* .pfnInitComplete = */ NULL,
1567 /* .pfnPowerOff = */ NULL,
1568 /* .pfnSoftReset = */ NULL,
1569 /* .pfnReserved0 = */ NULL,
1570 /* .pfnReserved1 = */ NULL,
1571 /* .pfnReserved2 = */ NULL,
1572 /* .pfnReserved3 = */ NULL,
1573 /* .pfnReserved4 = */ NULL,
1574 /* .pfnReserved5 = */ NULL,
1575 /* .pfnReserved6 = */ NULL,
1576 /* .pfnReserved7 = */ NULL,
1577#elif defined(IN_RING0)
1578 /* .pfnEarlyConstruct = */ NULL,
1579 /* .pfnConstruct = */ smcRZConstruct,
1580 /* .pfnDestruct = */ NULL,
1581 /* .pfnFinalDestruct = */ NULL,
1582# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
1583 /* .pfnRequest = */ devR0SmcReqHandler,
1584# else
1585 /* .pfnRequest = */ NULL,
1586# endif
1587 /* .pfnReserved0 = */ NULL,
1588 /* .pfnReserved1 = */ NULL,
1589 /* .pfnReserved2 = */ NULL,
1590 /* .pfnReserved3 = */ NULL,
1591 /* .pfnReserved4 = */ NULL,
1592 /* .pfnReserved5 = */ NULL,
1593 /* .pfnReserved6 = */ NULL,
1594 /* .pfnReserved7 = */ NULL,
1595#elif defined(IN_RC)
1596 /* .pfnConstruct = */ smcRZConstruct,
1597 /* .pfnReserved0 = */ NULL,
1598 /* .pfnReserved1 = */ NULL,
1599 /* .pfnReserved2 = */ NULL,
1600 /* .pfnReserved3 = */ NULL,
1601 /* .pfnReserved4 = */ NULL,
1602 /* .pfnReserved5 = */ NULL,
1603 /* .pfnReserved6 = */ NULL,
1604 /* .pfnReserved7 = */ NULL,
1605#else
1606# error "Not in IN_RING3, IN_RING0 or IN_RC!"
1607#endif
1608 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
1609};
1610
1611#endif /* VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette