VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevSMC.cpp@ 43879

Last change on this file since 43879 was 43879, checked in by vboxsync, 12 years ago

Extended RTOnce with termination cleanups. (Changes existing structures and functions.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.0 KB
Line 
1/* $Id: DevSMC.cpp 43879 2012-11-15 14:49:23Z vboxsync $ */
2/** @file
3 * DevSMC - SMC device emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * --------------------------------------------------------------------
17 *
18 * This code is based on:
19 *
20 * Apple SMC controller
21 *
22 * Copyright (c) 2007 Alexander Graf
23 *
24 * This library is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2 of the License, or (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with this library; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 *
38 * *****************************************************************
39 *
40 * In all Intel-based Apple hardware there is an SMC chip to control the
41 * backlight, fans and several other generic device parameters. It also
42 * contains the magic keys used to dongle Mac OS X to the device.
43 *
44 * This driver was mostly created by looking at the Linux AppleSMC driver
45 * implementation and does not support IRQ.
46 *
47 */
48
49/*******************************************************************************
50* Header Files *
51*******************************************************************************/
52#define LOG_GROUP LOG_GROUP_DEV_SMC
53#include <VBox/vmm/pdmdev.h>
54#include <VBox/log.h>
55#include <VBox/vmm/stam.h>
56#include <iprt/assert.h>
57#include <iprt/string.h>
58#ifdef IN_RING0
59# include <iprt/asm-amd64-x86.h>
60# include <iprt/once.h>
61# include <iprt/thread.h>
62#endif
63
64#include "VBoxDD2.h"
65
66
67/*******************************************************************************
68* Defined Constants And Macros *
69*******************************************************************************/
70/* data port used by Apple SMC */
71#define APPLESMC_DATA_PORT 0x300
72/* command/status port used by Apple SMC */
73#define APPLESMC_CMD_PORT 0x304
74#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
75#define APPLESMC_MAX_DATA_LENGTH 32
76
77#define APPLESMC_READ_CMD 0x10
78#define APPLESMC_WRITE_CMD 0x11
79#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
80#define APPLESMC_GET_KEY_TYPE_CMD 0x13
81
82/** The version of the saved state. */
83#define SMC_SAVED_STATE_VERSION 1
84
85/** The ring-0 operation number that attempts to get OSK0 and OSK1 from the real
86 * SMC. */
87#define SMC_CALLR0_READ_OSK 1
88
89/*******************************************************************************
90* Structures and Typedefs *
91*******************************************************************************/
92typedef struct AppleSMCData
93{
94 uint8_t len;
95 const char *key;
96 const char *data;
97} AppleSMCData;
98
99
100typedef struct
101{
102 PPDMDEVINSR3 pDevIns;
103
104 uint8_t cmd;
105 uint8_t status;
106 uint8_t key[4];
107 uint8_t read_pos;
108 uint8_t data_len;
109 uint8_t data_pos;
110 uint8_t data[255];
111
112 /** The OSK0 value. This is currently only used in the constructor. */
113 uint8_t abOsk0[32];
114 /** The OSK1 value. This is currently only used in the constructor */
115 uint8_t abOsk1[32];
116} SMCState;
117
118/*******************************************************************************
119* Global Variables *
120*******************************************************************************/
121#ifdef IN_RING3
122static char osk[64];
123
124/* See http://www.mactel-linux.org/wiki/AppleSMC */
125static struct AppleSMCData data[] =
126{
127 {6, "REV ", "\0x01\0x13\0x0f\0x00\0x00\0x03"},
128 {32,"OSK0", osk },
129 {32,"OSK1", osk+32 },
130 {1, "NATJ", "\0" },
131 {1, "MSSP", "\0" },
132 {1, "MSSD", "\0x3" },
133 {1, "NTOK", "\0"},
134 {0, NULL, NULL }
135};
136#endif /* IN_RING3 */
137#ifdef IN_RING0
138/** Do once for the SMC ring-0 static data (g_abOsk0, g_abOsk1, g_fHaveOsk). */
139static RTONCE g_SmcR0Once = RTONCE_INITIALIZER;
140/** Indicates whether we've successfully queried the OSK* keys. */
141static bool g_fHaveOsk = false;
142/** The OSK0 value. */
143static uint8_t g_abOsk0[32];
144/** The OSK1 value. */
145static uint8_t g_abOsk1[32];
146#endif /* IN_RING0 */
147
148#ifndef VBOX_DEVICE_STRUCT_TESTCASE
149
150#ifdef IN_RING0
151
152/**
153 * Waits for the specified status on the host SMC.
154 *
155 * @returns success indicator.
156 * @param bStatus The desired status.
157 * @param pszWhat What we're currently doing. For the log.
158 */
159static bool devR0SmcWaitHostStatus(uint8_t bStatus, const char *pszWhat)
160{
161 uint8_t bCurStatus;
162 for (uint32_t cMsSleep = 1; cMsSleep <= 64; cMsSleep <<= 1)
163 {
164 RTThreadSleep(cMsSleep);
165 bCurStatus = ASMInU8(APPLESMC_CMD_PORT);
166 if ((bCurStatus & 0xf) == bStatus)
167 return true;
168 }
169
170 LogRel(("devR0Smc: %s: bCurStatus=%#x, wanted %#x.\n", pszWhat, bCurStatus, bStatus));
171 return false;
172}
173
174/**
175 * Reads a key by name from the host SMC.
176 *
177 * @returns success indicator.
178 * @param pszName The key name, must be exactly 4 chars long.
179 * @param pbBuf The output buffer.
180 * @param cbBuf The buffer size. Max 32 bytes.
181 */
182static bool devR0SmcQueryHostKey(const char *pszName, uint8_t *pbBuf, size_t cbBuf)
183{
184 Assert(strlen(pszName) == 4);
185 Assert(cbBuf <= 32);
186 Assert(cbBuf > 0);
187
188 /*
189 * Issue the READ command.
190 */
191 uint32_t cMsSleep = 1;
192 for (;;)
193 {
194 ASMOutU8(APPLESMC_CMD_PORT, APPLESMC_READ_CMD);
195 RTThreadSleep(cMsSleep);
196 uint8_t bCurStatus = ASMInU8(APPLESMC_CMD_PORT);
197 if ((bCurStatus & 0xf) == 0xc)
198 break;
199 cMsSleep <<= 1;
200 if (cMsSleep > 64)
201 {
202 LogRel(("devR0Smc: %s: bCurStatus=%#x, wanted %#x.\n", "cmd", bCurStatus, 0xc));
203 return false;
204 }
205 }
206
207 /*
208 * Send it the key.
209 */
210 for (unsigned off = 0; off < 4; off++)
211 {
212 ASMOutU8(APPLESMC_DATA_PORT, pszName[off]);
213 if (!devR0SmcWaitHostStatus(4, "key"))
214 return false;
215 }
216
217 /*
218 * The desired amount of output.
219 */
220 ASMOutU8(APPLESMC_DATA_PORT, (uint8_t)cbBuf);
221
222 /*
223 * Read the output.
224 */
225 for (size_t off = 0; off < cbBuf; off++)
226 {
227 if (!devR0SmcWaitHostStatus(5, off ? "data" : "len"))
228 return false;
229 pbBuf[off] = ASMInU8(APPLESMC_DATA_PORT);
230 }
231
232 return true;
233}
234
235/**
236 * RTOnce callback that initializes g_fHaveOsk, g_abOsk0 and g_abOsk1.
237 *
238 * @returns VINF_SUCCESS.
239 * @param pvUserIgnored Ignored.
240 */
241static DECLCALLBACK(int) devR0SmcInitOnce(void *pvUserIgnored)
242{
243 g_fHaveOsk = devR0SmcQueryHostKey("OSK0", &g_abOsk0[0], sizeof(g_abOsk0))
244 && devR0SmcQueryHostKey("OSK1", &g_abOsk1[0], sizeof(g_abOsk1));
245
246 NOREF(pvUserIgnored);
247 return VINF_SUCCESS;
248}
249
250/**
251 * @interface_method_impl{FNPDMDEVREQHANDLERR0}
252 */
253PDMBOTHCBDECL(int) devR0SmcReqHandler(PPDMDEVINS pDevIns, uint32_t uOperation, uint64_t u64Arg)
254{
255 SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
256 int rc = VERR_INVALID_FUNCTION;
257
258 if (uOperation == SMC_CALLR0_READ_OSK)
259 {
260 rc = RTOnce(&g_SmcR0Once, devR0SmcInitOnce, NULL);
261 if ( RT_SUCCESS(rc)
262 && g_fHaveOsk)
263 {
264 AssertCompile(sizeof(g_abOsk0) == sizeof(pThis->abOsk0));
265 AssertCompile(sizeof(g_abOsk1) == sizeof(pThis->abOsk1));
266 memcpy(pThis->abOsk0, g_abOsk0, sizeof(pThis->abOsk0));
267 memcpy(pThis->abOsk1, g_abOsk1, sizeof(pThis->abOsk1));
268 }
269 }
270 return rc;
271}
272
273#endif /* IN_RING0 */
274#ifdef IN_RING3
275
276/**
277 * Saves a state of the SMC device.
278 *
279 * @returns VBox status code.
280 * @param pDevIns The device instance.
281 * @param pSSMHandle The handle to save the state to.
282 */
283static DECLCALLBACK(int) smcSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
284{
285 SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
286
287 /** @todo: implement serialization */
288 return VINF_SUCCESS;
289}
290
291
292/**
293 * Loads a SMC device state.
294 *
295 * @returns VBox status code.
296 * @param pDevIns The device instance.
297 * @param pSSMHandle The handle to the saved state.
298 * @param uVersion The data unit version number.
299 * @param uPass The data pass.
300 */
301static DECLCALLBACK(int) smcLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t uVersion, uint32_t uPass)
302{
303 SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
304
305 if (uVersion != SMC_SAVED_STATE_VERSION)
306 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
307 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
308
309 /** @todo: implement serialization */
310 return VINF_SUCCESS;
311}
312
313/**
314 * Reset notification.
315 *
316 * @returns VBox status.
317 * @param pDevIns The device instance data.
318 */
319static DECLCALLBACK(void) smcReset(PPDMDEVINS pDevIns)
320{
321 SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
322 LogFlow(("smcReset: \n"));
323}
324
325
326/**
327 * Info handler, device version.
328 *
329 * @param pDevIns Device instance which registered the info.
330 * @param pHlp Callback functions for doing output.
331 * @param pszArgs Argument string. Optional and specific to the handler.
332 */
333static DECLCALLBACK(void) smcInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
334{
335 SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
336}
337
338
339static void applesmc_fill_data(SMCState *s)
340{
341 struct AppleSMCData *d;
342 for (d=data; d->len; d++)
343 {
344 uint32_t key_data = *((uint32_t*)d->key);
345 uint32_t key_current = *((uint32_t*)s->key);
346 if (key_data == key_current)
347 {
348 Log(("APPLESMC: Key matched (%s Len=%d Data=%s)\n", d->key, d->len, d->data));
349 memcpy(s->data, d->data, d->len);
350 return;
351 }
352 }
353}
354
355/**
356 * Port I/O Handler for IN operations.
357 *
358 * @returns VBox status code.
359 *
360 * @param pDevIns The device instance.
361 * @param pvUser User argument - ignored.
362 * @param uPort Port number used for the IN operation.
363 * @param pu32 Where to store the result.
364 * @param cb Number of bytes read.
365 */
366PDMBOTHCBDECL(int) smcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
367{
368 SMCState * s = PDMINS_2_DATA(pDevIns, SMCState *);
369 uint8_t retval = 0;
370
371 NOREF(pvUser);
372 Log(("SMC port read: %x (%d)\n", Port, cb));
373
374 /** @todo: status code? */
375 if (cb != 1)
376 return VERR_IOM_IOPORT_UNUSED;
377
378 switch (Port)
379 {
380 case APPLESMC_CMD_PORT:
381 {
382 retval = s->status;
383 break;
384 }
385 case APPLESMC_DATA_PORT:
386 {
387 switch (s->cmd) {
388 case APPLESMC_READ_CMD:
389 if(s->data_pos < s->data_len)
390 {
391 retval = s->data[s->data_pos];
392 Log(("APPLESMC: READ_DATA[%d] = %#hhx\n", s->data_pos, retval));
393 s->data_pos++;
394 if(s->data_pos == s->data_len)
395 {
396 s->status = 0x00;
397 Log(("APPLESMC: EOF\n"));
398 }
399 else
400 s->status = 0x05;
401 }
402 }
403 break;
404 }
405 }
406
407 *pu32 = retval;
408
409 return VINF_SUCCESS;
410}
411
412
413/**
414 * Port I/O Handler for OUT operations.
415 *
416 * @returns VBox status code.
417 *
418 * @param pDevIns The device instance.
419 * @param pvUser User argument - ignored.
420 * @param uPort Port number used for the IN operation.
421 * @param u32 The value to output.
422 * @param cb The value size in bytes.
423 */
424PDMBOTHCBDECL(int) smcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
425{
426 SMCState* s = PDMINS_2_DATA(pDevIns, SMCState *);
427
428 NOREF(pvUser);
429
430 Log(("SMC port write: %x (%d) %x\n", Port, cb, u32));
431 /** @todo: status code? */
432 if (cb != 1)
433 return VINF_SUCCESS;
434
435 switch (Port)
436 {
437 case APPLESMC_CMD_PORT:
438 {
439 switch (u32)
440 {
441 case APPLESMC_READ_CMD:
442 s->status = 0x0c;
443 break;
444 }
445 s->cmd = u32;
446 s->read_pos = 0;
447 s->data_pos = 0;
448 break;
449 }
450 case APPLESMC_DATA_PORT:
451 {
452 switch(s->cmd)
453 {
454 case APPLESMC_READ_CMD:
455 if (s->read_pos < 4)
456 {
457 s->key[s->read_pos] = u32;
458 s->status = 0x04;
459 }
460 else
461 if (s->read_pos == 4)
462 {
463 s->data_len = u32;
464 s->status = 0x05;
465 s->data_pos = 0;
466 Log(("APPLESMC: Key = %c%c%c%c Len = %d\n", s->key[0], s->key[1], s->key[2], s->key[3], u32));
467 applesmc_fill_data(s);
468 }
469 s->read_pos++;
470 break;
471 }
472 }
473 }
474 return VINF_SUCCESS;
475}
476
477
478/**
479 * @interface_method_impl{PDMDEVREG,pfnConstruct}
480 */
481static DECLCALLBACK(int) smcConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
482{
483 SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
484 Assert(iInstance == 0);
485
486 /*
487 * Store state.
488 */
489 pThis->pDevIns = pDevIns;
490
491 /*
492 * Validate and read the configuration.
493 */
494 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "DeviceKey|GetKeyFromRealSMC", "");
495
496 /*
497 * Read the DeviceKey config value.
498 */
499 char *pszDeviceKey;
500 int rc = CFGMR3QueryStringAllocDef(pCfg, "DeviceKey", &pszDeviceKey, "");
501 if (RT_FAILURE(rc))
502 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
503 N_("Configuration error: Querying \"DeviceKey\" as a string failed"));
504
505 size_t cchDeviceKey = strlen(pszDeviceKey);
506 if (cchDeviceKey > 0)
507 memcpy(&pThis->abOsk0[0], pszDeviceKey, RT_MIN(cchDeviceKey, sizeof(pThis->abOsk0)));
508 if (cchDeviceKey > sizeof(pThis->abOsk0))
509 memcpy(&pThis->abOsk1[0], &pszDeviceKey[sizeof(pThis->abOsk0)],
510 RT_MIN(cchDeviceKey - sizeof(pThis->abOsk0), sizeof(pThis->abOsk1)));
511
512 MMR3HeapFree(pszDeviceKey);
513
514 /*
515 * Query the key from the real hardware if asked to do so.
516 */
517 bool fGetKeyFromRealSMC;
518 rc = CFGMR3QueryBoolDef(pCfg, "GetKeyFromRealSMC", &fGetKeyFromRealSMC, false);
519 if (RT_FAILURE(rc))
520 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
521 N_("Configuration error: Querying \"GetKeyFromRealSMC\" as a boolean failed"));
522 if (fGetKeyFromRealSMC)
523 {
524 rc = PDMDevHlpCallR0(pDevIns, SMC_CALLR0_READ_OSK, 0 /*u64Arg*/);
525 if (RT_FAILURE(rc))
526 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
527 N_("Failed to query SMC value from the host"));
528 }
529
530 /*
531 * For practical/historical reasons, the OSK[0|1] data is stored in a
532 * global buffer in ring-3.
533 */
534 AssertCompile(sizeof(osk) == sizeof(pThis->abOsk0) + sizeof(pThis->abOsk1));
535 AssertCompile(sizeof(char) == sizeof(uint8_t));
536 memcpy(osk, pThis->abOsk0, sizeof(pThis->abOsk0));
537 memcpy(&osk[sizeof(pThis->abOsk0)], pThis->abOsk1, sizeof(pThis->abOsk1));
538
539 /*
540 * Register the IO ports.
541 */
542 rc = PDMDevHlpIOPortRegister(pDevIns, APPLESMC_DATA_PORT, 1, NULL,
543 smcIOPortWrite, smcIOPortRead,
544 NULL, NULL, "SMC Data");
545 if (RT_FAILURE(rc))
546 return rc;
547 rc = PDMDevHlpIOPortRegister(pDevIns, APPLESMC_CMD_PORT, 1, NULL,
548 smcIOPortWrite, smcIOPortRead,
549 NULL, NULL, "SMC Commands");
550 if (RT_FAILURE(rc))
551 return rc;
552
553 /* Register saved state management */
554 rc = PDMDevHlpSSMRegister(pDevIns, SMC_SAVED_STATE_VERSION, sizeof(*pThis), smcSaveExec, smcLoadExec);
555 if (RT_FAILURE(rc))
556 return rc;
557
558 /*
559 * Initialize the device state.
560 */
561 smcReset(pDevIns);
562
563 /**
564 * @todo: Register statistics.
565 */
566 PDMDevHlpDBGFInfoRegister(pDevIns, "smc", "Display SMC status. (no arguments)", smcInfo);
567
568 return VINF_SUCCESS;
569}
570
571/**
572 * The device registration structure.
573 */
574const PDMDEVREG g_DeviceSMC =
575{
576 /* u32Version */
577 PDM_DEVREG_VERSION,
578 /* szName */
579 "smc",
580 /* szRCMod */
581 "VBoxDD2GC.gc",
582 /* szR0Mod */
583 "VBoxDD2R0.r0",
584 /* pszDescription */
585 "System Management Controller (SMC) Device",
586 /* fFlags */
587 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36| PDM_DEVREG_FLAGS_R0,
588 /* fClass */
589 PDM_DEVREG_CLASS_MISC,
590 /* cMaxInstances */
591 1,
592 /* cbInstance */
593 sizeof(SMCState),
594 /* pfnConstruct */
595 smcConstruct,
596 /* pfnDestruct */
597 NULL,
598 /* pfnRelocate */
599 NULL,
600 /* pfnIOCtl */
601 NULL,
602 /* pfnPowerOn */
603 NULL,
604 /* pfnReset */
605 smcReset,
606 /* pfnSuspend */
607 NULL,
608 /* pfnResume */
609 NULL,
610 /* pfnAttach */
611 NULL,
612 /* pfnDetach */
613 NULL,
614 /* pfnQueryInterface. */
615 NULL,
616 /* pfnInitComplete */
617 NULL,
618 /* pfnPowerOff */
619 NULL,
620 /* pfnSoftReset */
621 NULL,
622 /* u32VersionEnd */
623 PDM_DEVREG_VERSION
624};
625
626#endif /* IN_RING3 */
627
628#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