From nobody Sun Jul 26 13:40:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=pq.io ARC-Seal: i=1; a=rsa-sha256; t=1778174542; cv=none; d=zohomail.com; s=zohoarc; b=j9tQfVKvdHiU6SCy9msh4g6TfvrSfLhMHs0f4W1jFnKVKTyum4xt+4MOYDi+eiukQHcDUfoNuP2A2n/MKseAyUZX8UhZauRSpBeRAhyya/BMo785X94p8N588zYqx5R4XKMZ3lrDoRQ8fAt6K+qA+kV9geUKtf4oLGIn5P+y75M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1778174542; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QoT6ZV0zp2yCk/oaO3p5EzNmYDCXteG6Q8NREGInOLI=; b=C8f5FKb5J+6djsSbiX4MkrqNn34DvjLRukq4dXbLM4jP3QL5EE/VT7Quq8oyJA1HaVWgBmz86VTEP2NXbfqZlYaP7/A5Ta2qwPkgxyyXK2F9r/yzeHVjPAir0GEcVXvIpWPsKYThkXKWNWlZLZ0Lv4QE7qHh20q516RjXx26scQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1778174542898899.1235232198277; Thu, 7 May 2026 10:22:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wL2PP-0000su-LP; Thu, 07 May 2026 13:21:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wL0Wq-0004AN-5X for qemu-devel@nongnu.org; Thu, 07 May 2026 11:20:44 -0400 Received: from mail-244107.protonmail.ch ([109.224.244.107]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wL0Wm-00046S-NX for qemu-devel@nongnu.org; Thu, 07 May 2026 11:20:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pq.io; s=protonmail3; t=1778167236; x=1778426436; bh=QoT6ZV0zp2yCk/oaO3p5EzNmYDCXteG6Q8NREGInOLI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=NteZ6GXvVcD2fAX01t/eyJ3rvh6JcDGTrHsfz+g9Gj+ys0Xfb+MtoE3Asw77xsIDG hnS3m97IHIw3HJxMVlIytbKQmMdaZW0g/LuSVDIumrvsAdsWiqo9spw8yaFxPP2qkI WJ1GAkudqktVC2JIA5HytSBCNdIuH86I0idyBV/qnq5dg3IvX4BEHEFzHZiWckU4YH ckv8tFO5eHNFaQ6oyPuPfJC35qZg2ZSUI7yNSuNzPTkHgmddszQQbKZH9siqgq+VOa /QJv8yqyB4aoia1RAqEPW3EkDu57n/mnnHQxY8byLzFay2oO4vHJBY0sc7zsw2pkYN x1yCsQO3izChg== X-Pm-Submission-Id: 4gBGCj2DxZz2Scpx From: Matthew Jackson To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, peter.maydell@linaro.org Subject: [PATCH v2 1/2] hw/misc/applesmc: fix GET_KEY_BY_INDEX to return real keys, accept WRITE/TYPE commands Date: Thu, 7 May 2026 08:20:29 -0700 Message-ID: <20260507152030.48753-2-matthew@pq.io> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260507040153.14565-1-matthew@pq.io> References: <20260507040153.14565-1-matthew@pq.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=109.224.244.107; envelope-from=matthew@pq.io; helo=mail-244107.protonmail.ch X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 07 May 2026 13:21:05 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @pq.io) X-ZM-MESSAGEID: 1778174544183158500 Content-Type: text/plain; charset="utf-8" The applesmc device implements just enough of the Apple SMC PMIO protocol to satisfy the OSK boot check on older macOS versions. On modern macOS guests (x86 10.14+, all 15.x), the AppleSMC kext enumerates the SMC key space at boot via APPLESMC_GET_KEY_BY_INDEX_CMD (0x12). The current device only acknowledges APPLESMC_READ_CMD (0x10) at the command port; every other command falls through to the default arm of the switch and sets ST_1E_BAD_CMD. The macOS driver interprets the resulting 0x82 reply as "spurious data" and enters a retry loop that floods the kernel log with kSMCSpuriousData (0x81) / kSMCKeyNotFound errors at roughly 1800 events per second, pegging kernel_task at ~70% CPU and WindowServer at ~509% CPU. This reproduces reliably on any recent macOS 15 guest booted with -device isa-applesmc,osk=3D. This patch: * Accepts APPLESMC_WRITE_CMD, APPLESMC_GET_KEY_BY_INDEX_CMD, and APPLESMC_GET_KEY_TYPE_CMD at the command port (in addition to the existing READ_CMD path). * Implements GET_KEY_BY_INDEX by walking s->data_def and returning the 4-byte ASCII key name at the requested index; returns APPLESMC_ST_1E_BAD_INDEX (0xb8) once the index is past the end of the list so the guest stops iterating. * Implements GET_KEY_TYPE by looking up the key in s->data_def and returning a 6-byte response (type[4] + size[1] + attr[1]) matching VirtualSMC's kern_pmio.cpp behaviour. * Implements WRITE by accepting the key name, length, and payload and logging at LOG_UNIMP. macOS writes SMC keys during normal power management; silent acceptance avoids BAD_CMD on every write. * Replaces the unknown-key NOEXIST (0x84) reply on READ with a zeroed payload of the requested length, logged at LOG_UNIMP. Early-boot probes hit hundreds of undocumented keys per second; NOEXIST triggers retry storms while a zeroed payload satisfies the probe semantics without asserting a particular value. * Routes the BAD_CMD path through qemu_log_mask (LOG_GUEST_ERROR) instead of the smc_debug printf. * Fixes a typo in the MSSD key initialiser ("\0x3" -> "\x03"). The original literal was three bytes ('\\0', 'x', '3') truncated to one ('\\0') by the size argument, so MSSD has been silently returning 0 since introduction; the corrected value matches what a real iMac20,1 SMC reports. Reported-by: macOS guests booted with -device isa-applesmc since 10.14. Signed-off-by: Matthew Jackson --- hw/misc/applesmc.c | 177 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 170 insertions(+), 7 deletions(-) diff --git a/hw/misc/applesmc.c b/hw/misc/applesmc.c index fd96f5f..2b5ef3c 100644 --- a/hw/misc/applesmc.c +++ b/hw/misc/applesmc.c @@ -35,6 +35,7 @@ #include "hw/core/qdev-properties.h" #include "ui/console.h" #include "qemu/error-report.h" +#include "qemu/log.h" #include "qemu/module.h" #include "qemu/timer.h" #include "qom/object.h" @@ -126,7 +127,14 @@ static void applesmc_io_cmd_write(void *opaque, hwaddr= addr, uint64_t val, smc_debug("CMD received: 0x%02x\n", (uint8_t)val); switch (val) { case APPLESMC_READ_CMD: - /* did last command run through OK? */ + case APPLESMC_WRITE_CMD: + case APPLESMC_GET_KEY_BY_INDEX_CMD: + case APPLESMC_GET_KEY_TYPE_CMD: + /* + * Accept all standard SMC commands. Pre-existing code only handled + * READ_CMD; macOS boots hang if WRITE/TYPE/GET_KEY_BY_INDEX comma= nds + * return BAD_CMD during early AppleSMC driver init. + */ if (status =3D=3D APPLESMC_ST_CMD_DONE || status =3D=3D APPLESMC_S= T_NEW_CMD) { s->cmd =3D val; s->status =3D APPLESMC_ST_NEW_CMD | APPLESMC_ST_ACK; @@ -137,7 +145,8 @@ static void applesmc_io_cmd_write(void *opaque, hwaddr = addr, uint64_t val, } break; default: - smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val); + qemu_log_mask(LOG_GUEST_ERROR, + "applesmc: unexpected CMD 0x%02x\n", (uint8_t)val); s->status =3D APPLESMC_ST_NEW_CMD; s->status_1e =3D APPLESMC_ST_1E_BAD_CMD; } @@ -179,17 +188,170 @@ static void applesmc_io_data_write(void *opaque, hwa= ddr addr, uint64_t val, s->data_len =3D d->len; s->data_pos =3D 0; s->status =3D APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY; - s->status_1e =3D APPLESMC_ST_CMD_DONE; /* clear on valid = key */ + s->status_1e =3D APPLESMC_ST_CMD_DONE; } else { - smc_debug("READ_CMD: key '%c%c%c%c' not found!\n", - s->key[0], s->key[1], s->key[2], s->key[3]); + /* + * Return zeros for unknown keys instead of NOEXIST. Early + * macOS boot probes many undocumented keys; responding + * NOEXIST triggers retry storms. A zeroed payload satisfi= es + * the probe without asserting a particular value. + */ + qemu_log_mask(LOG_UNIMP, + "applesmc: READ unknown key '%c%c%c%c' len= =3D%d\n", + s->key[0], s->key[1], s->key[2], s->key[3], + (uint8_t)val); + memset(s->data, 0, APPLESMC_MAX_DATA_LENGTH); + s->data_len =3D (uint8_t)val; + s->data_pos =3D 0; + s->status =3D APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY; + s->status_1e =3D APPLESMC_ST_CMD_DONE; + } + } + s->read_pos++; + break; + case APPLESMC_WRITE_CMD: + /* + * Accept writes silently. macOS writes SMC keys during power + * management, fan control, etc. Log at LOG_UNIMP for visibility + * without treating the write as an error. + */ + if ((s->status & 0x0f) =3D=3D APPLESMC_ST_CMD_DONE) { + break; + } + if (s->read_pos < 4) { + s->key[s->read_pos] =3D val; + s->status =3D APPLESMC_ST_ACK; + } else if (s->read_pos =3D=3D 4) { + s->data_len =3D (uint8_t)val; + s->data_pos =3D 0; + s->status =3D APPLESMC_ST_ACK; + } else { + if (s->data_pos < s->data_len) { + s->data[s->data_pos] =3D (uint8_t)val; + s->data_pos++; + if (s->data_pos =3D=3D s->data_len) { + qemu_log_mask(LOG_UNIMP, + "applesmc: WRITE key '%c%c%c%c' len=3D%d= \n", + s->key[0], s->key[1], s->key[2], s->key[= 3], + s->data_len); + s->status =3D APPLESMC_ST_CMD_DONE; + s->status_1e =3D APPLESMC_ST_CMD_DONE; + } else { + s->status =3D APPLESMC_ST_ACK; + } + } + } + s->read_pos++; + break; + case APPLESMC_GET_KEY_TYPE_CMD: + /* + * Return key type info. Protocol (matches VirtualSMC): + * - Receive 4 bytes of key name. + * - After the 4th byte, immediately set DATA_READY with response. + * - Response is 6 bytes: type[4] + size[1] + attr[1]. + * Unlike READ_CMD there is no length byte between key name and + * response. + */ + if ((s->status & 0x0f) =3D=3D APPLESMC_ST_CMD_DONE) { + break; + } + if (s->read_pos < 3) { + s->key[s->read_pos] =3D val; + s->status =3D APPLESMC_ST_ACK; + } else if (s->read_pos =3D=3D 3) { + /* 4th and final key byte. Unlike READ_CMD which has a 5th byte + * for data length, GET_KEY_TYPE responds immediately after the + * 4-byte key name (matching VirtualSMC kern_pmio.cpp behavior= ). */ + s->key[3] =3D val; + d =3D applesmc_find_key(s); + if (d !=3D NULL) { + switch (d->len) { + case 1: + s->data[0] =3D 'u'; s->data[1] =3D 'i'; + s->data[2] =3D '8'; s->data[3] =3D ' '; + break; + case 2: + s->data[0] =3D 'u'; s->data[1] =3D 'i'; + s->data[2] =3D '1'; s->data[3] =3D '6'; + break; + case 4: + s->data[0] =3D 'u'; s->data[1] =3D 'i'; + s->data[2] =3D '3'; s->data[3] =3D '2'; + break; + default: + s->data[0] =3D 'c'; s->data[1] =3D 'h'; + s->data[2] =3D '8'; s->data[3] =3D '*'; + break; + } + s->data[4] =3D d->len; + s->data[5] =3D 0xD0; + } else { + qemu_log_mask(LOG_UNIMP, + "applesmc: GET_KEY_TYPE unknown '%c%c%c%c'\n= ", + s->key[0], s->key[1], s->key[2], s->key[3]); + s->data[0] =3D 'u'; s->data[1] =3D 'i'; + s->data[2] =3D '8'; s->data[3] =3D ' '; + s->data[4] =3D 1; + s->data[5] =3D 0xD0; + } + s->data_len =3D 6; + s->data_pos =3D 0; + s->status =3D APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY; + s->status_1e =3D APPLESMC_ST_CMD_DONE; + } + s->read_pos++; + break; + case APPLESMC_GET_KEY_BY_INDEX_CMD: + /* + * Return key name by index. macOS sends a 4-byte big-endian index + * and expects the 4-byte ASCII key name at that position. The + * previous implementation returned 4 zero bytes, which macOS + * treated as kSMCSpuriousData (0x81) and retried indefinitely, + * flooding the kernel log at ~1800 errors/sec. Walk the keys list + * to return the actual key name, or APPLESMC_ST_1E_BAD_INDEX + * (0xb8) once the index is past the end of the list so the guest + * stops iterating. + */ + if ((s->status & 0x0f) =3D=3D APPLESMC_ST_CMD_DONE) { + break; + } + if (s->read_pos < 3) { + s->key[s->read_pos] =3D val; + s->status =3D APPLESMC_ST_ACK; + } else if (s->read_pos =3D=3D 3) { + s->key[3] =3D val; + uint32_t idx =3D ((uint8_t)s->key[0] << 24) + | ((uint8_t)s->key[1] << 16) + | ((uint8_t)s->key[2] << 8) + | (uint8_t)s->key[3]; + struct AppleSMCData *def; + uint32_t i =3D 0; + bool found =3D false; + QLIST_FOREACH(def, &s->data_def, node) { + if (i =3D=3D idx) { + memcpy(s->data, def->key, 4); + s->data_len =3D 4; + s->data_pos =3D 0; + found =3D true; + break; + } + i++; + } + if (!found) { + s->data_len =3D 0; + s->status_1e =3D APPLESMC_ST_1E_BAD_INDEX; s->status =3D APPLESMC_ST_CMD_DONE; - s->status_1e =3D APPLESMC_ST_1E_NOEXIST; + s->read_pos++; + break; } + s->status =3D APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY; + s->status_1e =3D APPLESMC_ST_CMD_DONE; } s->read_pos++; break; default: + qemu_log_mask(LOG_GUEST_ERROR, + "applesmc: unhandled data for cmd 0x%02x\n", s->cmd); s->status =3D APPLESMC_ST_CMD_DONE; s->status_1e =3D APPLESMC_ST_1E_STILL_BAD_CMD; } @@ -330,12 +492,13 @@ static void applesmc_isa_realize(DeviceState *dev, Er= ror **errp) } =20 QLIST_INIT(&s->data_def); + applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03"); applesmc_add_key(s, "OSK0", 32, s->osk); applesmc_add_key(s, "OSK1", 32, s->osk + 32); applesmc_add_key(s, "NATJ", 1, "\0"); applesmc_add_key(s, "MSSP", 1, "\0"); - applesmc_add_key(s, "MSSD", 1, "\0x3"); + applesmc_add_key(s, "MSSD", 1, "\x03"); } =20 static void applesmc_unrealize(DeviceState *dev) --=20 2.50.1 (Apple Git-155)