From nobody Mon May 1 21:28:51 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q9GYJ1360z492VS; Mon, 1 May 2023 21:28:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q9GYH547wz3k3X; Mon, 1 May 2023 21:28:51 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682976531; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7tW3QgpM1XRy1VVgPvmOdCvPZ/snqfLvv8MHZg5196s=; b=SOSAADGL28035vwC4UfEqZgGJDgr2QPuqGiAqlFyqg+XUozBzoHWrrUscvG320btGPC7UB dz6rMHxFxHIea20FjR81WA+7ZrMdbJ7uVNR94oyG1VmC86MgRRYJtjZ35RzLT26XPIzngZ 9IPETFrXPMPVyQGSE7HwPiBdQxQyJjFrJuUWBYCHOJD+Zb9vLkFNKMAJQla4WgPPQv1y8j 74q44BowL/5rpspk1Vj+NiHx2IitFWDcO68ME3CLXsKXX3Mo8qozghzXNl9m70Vwz6gapd QhyaLBpyc/fg0Scfw0bG0v5fgzY9IM1hc2yN6GZT7VC4aMgDyxhxf4vWUjU+xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682976531; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7tW3QgpM1XRy1VVgPvmOdCvPZ/snqfLvv8MHZg5196s=; b=HtS+B/VdawvaMAL8/qtTR8ODYKTbLH5u2aCvquc9dR3Y9XHAEq62KseUUdpxIL+eGcpo90 JLrKeB7cqMvVa3j+BimKaKID9iVhAvha8EHk1n55iaua0zpPUNFLPsK7jbVIR0/vBey5au cDWcAfBp0q1P4ZtJTfeYOv2dfi8er2z74vQ/CD+G1bzFW3IbinY2fpLr0V7wStND4UWq0n Fqrlo8diK/bnkIzVCoQrkqIUYc+yuhpNYkifYjKvJJ2EaOx9rvCNY/YQbm9xMtMy8xVIxU QC7xed18DuJFoO/uogHYQVoGlGnj2W2UiHzoTkTcVfNqK3yMdw7+LywuJEJs7A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1682976531; a=rsa-sha256; cv=none; b=lUyoIku5yabuIamRRtvWDBr0IwFHnMyIqY+dvMPxB3AhZE1oMUjaIeRuhj8gYRBRL0YAEV WPKcpIMNmNY+GeCrNcFijOaMrbRvlbt5mlZO5Rw4F1qukvpuovXvYaozROLaz5WN59KuGZ Q5aKbQ1BAgrwFdkGiCo7tXh+3Qo+yweAVVpwG/4zdf+35wwVBJKE5LJNt4+VjFKZM4CLIr w21vANvBfTCyR+T5u8i+B6mhmOt5Zc/6U0MEYHAT0a0ZI5NXugiAhoWFvB7UkF8HelKZQG 12jqMudIMAZhyhOzeDLEmeZVDsafvkl/9yCVRJNEIQkqt7eLRjv0Her/Al3lUA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q9GYH42zDzM0M; Mon, 1 May 2023 21:28:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 341LSpex085939; Mon, 1 May 2023 21:28:51 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 341LSpEB085938; Mon, 1 May 2023 21:28:51 GMT (envelope-from git) Date: Mon, 1 May 2023 21:28:51 GMT Message-Id: <202305012128.341LSpEB085938@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: c5e433b99ed3 - main - stand: Avoid unaligned access in smbios code List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c5e433b99ed3ddef0eb4fa937f38c34d4a3c4ae0 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=c5e433b99ed3ddef0eb4fa937f38c34d4a3c4ae0 commit c5e433b99ed3ddef0eb4fa937f38c34d4a3c4ae0 Author: Warner Losh AuthorDate: 2023-05-01 21:12:34 +0000 Commit: Warner Losh CommitDate: 2023-05-01 21:12:34 +0000 stand: Avoid unaligned access in smbios code This code was written on x86 where unaligned accesses were easy. LinuxBoot running on aarch64 uses mmap of /dev/mem to read the smbios table. Linux's mapping of this memory doesn't allow the normal unaligned fixup, so we get a bus error instead. We can't use the more natural le16dec and friends because they optimize into a single, unaligned memory load. We don't see this issue on aarch64 UEFI because memory is mapped such that unaligned accesses are fixed up. Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D39793 --- stand/libsa/smbios.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/stand/libsa/smbios.c b/stand/libsa/smbios.c index 657e1635e6e1..a88d3ac4ab69 100644 --- a/stand/libsa/smbios.c +++ b/stand/libsa/smbios.c @@ -96,10 +96,47 @@ __FBSDID("$FreeBSD$"); #define SMBIOS3_SIG "_SM3_" #define SMBIOS_DMI_SIG "_DMI_" -#define SMBIOS_GET8(base, off) (*(uint8_t *)((base) + (off))) -#define SMBIOS_GET16(base, off) (*(uint16_t *)((base) + (off))) -#define SMBIOS_GET32(base, off) (*(uint32_t *)((base) + (off))) -#define SMBIOS_GET64(base, off) (*(uint64_t *)((base) + (off))) +/* + * 5.1 General + *... + * NOTE The Entry Point Structure and all SMBIOS structures assume a + * little-endian ordering convention... + * ... + * + * We use memcpy to avoid unaligned access to memory. To normal memory, this is + * fine, but the memory we are using might be mmap'd /dev/mem which under Linux + * on aarch64 doesn't allow unaligned access. leXdec and friends can't be used + * because those can optimize to an unaligned load (which often is fine, but not + * for mmap'd /dev/mem which has special memory attributes). + */ +static inline uint8_t SMBIOS_GET8(const caddr_t base, int off) { return (base[off]); } + +static inline uint16_t +SMBIOS_GET16(const caddr_t base, int off) +{ + uint16_t v; + + memcpy(&v, base + off, sizeof(v)); + return (le16toh(v)); +} + +static inline uint32_t +SMBIOS_GET32(const caddr_t base, int off) +{ + uint32_t v; + + memcpy(&v, base + off, sizeof(v)); + return (le32toh(v)); +} + +static inline uint64_t +SMBIOS_GET64(const caddr_t base, int off) +{ + uint64_t v; + + memcpy(&v, base + off, sizeof(v)); + return (le64toh(v)); +} #define SMBIOS_GETLEN(base) SMBIOS_GET8(base, 0x01) #define SMBIOS_GETSTR(base) ((base) + SMBIOS_GETLEN(base)) @@ -195,7 +232,7 @@ smbios_setenv(const char *name, caddr_t addr, const int offset) #define UUID_TYPE uint32_t #define UUID_STEP sizeof(UUID_TYPE) #define UUID_ALL_BITS (UUID_SIZE / UUID_STEP) -#define UUID_GET(base, off) (*(UUID_TYPE *)((base) + (off))) +#define UUID_GET(base, off) SMBIOS_GET32(base, off) static void smbios_setuuid(const char *name, const caddr_t addr, const int ver __unused)