diff --git a/drivers/soc/apple/rtkit-crashlog.c b/drivers/soc/apple/rtkit-crashlog.c index 5defa12862b15..2ec4bd9569e46 100644 --- a/drivers/soc/apple/rtkit-crashlog.c +++ b/drivers/soc/apple/rtkit-crashlog.c @@ -3,6 +3,8 @@ * Apple RTKit IPC library * Copyright (C) The Asahi Linux Contributors */ +#include + #include "rtkit-internal.h" #define FOURCC(a, b, c, d) \ @@ -13,6 +15,51 @@ #define APPLE_RTKIT_CRASHLOG_MBOX FOURCC('C', 'm', 'b', 'x') #define APPLE_RTKIT_CRASHLOG_TIME FOURCC('C', 't', 'i', 'm') #define APPLE_RTKIT_CRASHLOG_REGS FOURCC('C', 'r', 'g', '8') +#define APPLE_RTKIT_CRASHLOG_REGS_CL4 FOURCC('C', 'r', 'l', '4') + +/* + * Newer firmware writes a container instead of a single crashlog: a + * 'CLGH' header and a table of records, each with an ID and an offset from + * the container. The record whose ID is the container's current ID holds + * the log of this crash, and may itself be a container (on T6050's DCP: + * per-coprocessor, then per-task). The log at the end is an ordinary + * 'CLHE' crashlog with the same sections. + */ +struct apple_rtkit_crashlog_container { + u32 fourcc; + u32 version; + u32 size; + u32 current_id; + u32 count; +} __packed; +static_assert(sizeof(struct apple_rtkit_crashlog_container) == 0x14); + +struct apple_rtkit_crashlog_record { + u64 id; + u64 _unk0; + u32 offset; + u32 _unk1; +} __packed; +static_assert(sizeof(struct apple_rtkit_crashlog_record) == 0x18); + +#define APPLE_RTKIT_CRASHLOG_CONTAINER_VERSION 2 +#define APPLE_RTKIT_CRASHLOG_CONTAINER_MAX_DEPTH 8 +/* Set in a log's flags while the co-processor is still writing it. */ +#define APPLE_RTKIT_CRASHLOG_INCOMPLETE BIT(8) + +/* Register frame of the 'Crl4' section, offsets from the section start. */ +#define APPLE_RTKIT_CRASHLOG_CL4_SECTION_SIZE 0x378 +#define APPLE_RTKIT_CRASHLOG_CL4_FLAGS 0x10 +#define APPLE_RTKIT_CRASHLOG_CL4_SIZE 0x14 +#define APPLE_RTKIT_CRASHLOG_CL4_NOMINAL_SIZE 0xd8 +#define APPLE_RTKIT_CRASHLOG_CL4_REGS 0x18 +#define APPLE_RTKIT_CRASHLOG_CL4_LR 0x108 +#define APPLE_RTKIT_CRASHLOG_CL4_PC 0x110 +#define APPLE_RTKIT_CRASHLOG_CL4_SP 0x118 +#define APPLE_RTKIT_CRASHLOG_CL4_PSR 0x120 +#define APPLE_RTKIT_CRASHLOG_CL4_ESR 0x358 +#define APPLE_RTKIT_CRASHLOG_CL4_FAR 0x360 +#define APPLE_RTKIT_CRASHLOG_CL4_UNDECODABLE BIT(16) /* For COMPILE_TEST on non-ARM64 architectures */ #ifndef PSR_MODE_EL0t @@ -71,8 +118,8 @@ static void apple_rtkit_crashlog_dump_str(struct apple_rtkit *rtk, u8 *bfr, *newline = tmp; ptr = newline + 1; } else { - dev_warn(rtk->dev, "RTKit: Message (id=%x): %s", idx, - ptr); + dev_warn(rtk->dev, "RTKit: Message (id=%x): %.*s", idx, + (int)(end - ptr), ptr); break; } } @@ -81,7 +128,8 @@ static void apple_rtkit_crashlog_dump_str(struct apple_rtkit *rtk, u8 *bfr, static void apple_rtkit_crashlog_dump_version(struct apple_rtkit *rtk, u8 *bfr, size_t size) { - dev_warn(rtk->dev, "RTKit: Version: %s", bfr + 16); + /* The string need not be terminated inside the section. */ + dev_warn(rtk->dev, "RTKit: Version: %.*s", (int)(size - 16), bfr + 16); } static void apple_rtkit_crashlog_dump_time(struct apple_rtkit *rtk, u8 *bfr, @@ -100,6 +148,10 @@ static void apple_rtkit_crashlog_dump_mailbox(struct apple_rtkit *rtk, u8 *bfr, size_t n_messages; struct apple_rtkit_crashlog_mbox_entry entry; + if (size < 28) { + dev_warn(rtk->dev, "RTKit: Mailbox section too small: 0x%zx", size); + return; + } memcpy(&type, bfr + 16, 4); memcpy(&index, bfr + 24, 4); n_messages = (size - 28) / sizeof(entry); @@ -173,13 +225,150 @@ static void apple_rtkit_crashlog_dump_regs(struct apple_rtkit *rtk, u8 *bfr, dev_warn(rtk->dev, "\n"); } +static void apple_rtkit_crashlog_dump_regs_cl4(struct apple_rtkit *rtk, + u8 *section, size_t size) +{ + u32 flags, regs_size; + int i; + + if (size < APPLE_RTKIT_CRASHLOG_CL4_SECTION_SIZE) { + dev_warn(rtk->dev, "RTKit: Crl4 section too small: 0x%zx", size); + return; + } + flags = get_unaligned_le32(section + APPLE_RTKIT_CRASHLOG_CL4_FLAGS); + regs_size = get_unaligned_le32(section + APPLE_RTKIT_CRASHLOG_CL4_SIZE); + if (regs_size != APPLE_RTKIT_CRASHLOG_CL4_NOMINAL_SIZE || + (flags & APPLE_RTKIT_CRASHLOG_CL4_UNDECODABLE)) { + dev_warn(rtk->dev, "RTKit: Crl4 frame not decodable (flags %#x, size %#x)", + flags, regs_size); + return; + } + + dev_warn(rtk->dev, "RTKit: Exception dump (Crl4):"); + dev_warn(rtk->dev, " PSR = 0x%llx", + get_unaligned_le64(section + APPLE_RTKIT_CRASHLOG_CL4_PSR)); + dev_warn(rtk->dev, " PC = 0x%llx", + get_unaligned_le64(section + APPLE_RTKIT_CRASHLOG_CL4_PC)); + dev_warn(rtk->dev, " LR = 0x%llx", + get_unaligned_le64(section + APPLE_RTKIT_CRASHLOG_CL4_LR)); + dev_warn(rtk->dev, " ESR = 0x%llx", + get_unaligned_le64(section + APPLE_RTKIT_CRASHLOG_CL4_ESR)); + dev_warn(rtk->dev, " FAR = 0x%llx", + get_unaligned_le64(section + APPLE_RTKIT_CRASHLOG_CL4_FAR)); + dev_warn(rtk->dev, " SP = 0x%llx", + get_unaligned_le64(section + APPLE_RTKIT_CRASHLOG_CL4_SP)); + for (i = 0; i < 28; i += 4) { + u8 *r = section + APPLE_RTKIT_CRASHLOG_CL4_REGS + i * 8; + + dev_warn(rtk->dev, " x%02d-x%02d = %016llx %016llx %016llx %016llx", + i, i + 3, get_unaligned_le64(r), get_unaligned_le64(r + 8), + get_unaligned_le64(r + 16), get_unaligned_le64(r + 24)); + } +} + +/** + * apple_rtkit_crashlog_locate() - find this crash's log in a crashlog buffer + * @bfr: a copy of the buffer + * @size: its size + * @log_offset: set to the offset of the 'CLHE' log + * @log_id: set to the ID of the record that held it (0 for a bare log) + * @depth: set to the number of containers followed + * + * A bare 'CLHE' log at offset 0 is returned as it is. In a 'CLGH' container, + * the record with the current ID is followed, through nested containers, + * to its log. Offsets only move forward, so a corrupt table cannot loop. + * + * Return: 0 if a complete log was found, -EAGAIN if the selected log is + * still being written, -EINVAL otherwise. + */ +int apple_rtkit_crashlog_locate(const u8 *bfr, size_t size, size_t *log_offset, + u32 *log_id, unsigned int *depth) +{ + struct apple_rtkit_crashlog_container container; + struct apple_rtkit_crashlog_header header; + struct apple_rtkit_crashlog_record record; + size_t offset = 0, avail, next; + unsigned int level; + u32 id = 0, i; + + for (level = 0; level <= APPLE_RTKIT_CRASHLOG_CONTAINER_MAX_DEPTH; level++) { + avail = size - offset; + if (avail < sizeof(header)) + return -EINVAL; + memcpy(&header, bfr + offset, sizeof(header)); + + if (header.fourcc == APPLE_RTKIT_CRASHLOG_HEADER_FOURCC) { + if (header.size < sizeof(header) || header.size > avail) + return -EINVAL; + if (level && (header.flags & APPLE_RTKIT_CRASHLOG_INCOMPLETE)) + return -EAGAIN; + *log_offset = offset; + *log_id = id; + *depth = level; + return 0; + } + if (header.fourcc != APPLE_RTKIT_CRASHLOG_CONTAINER_FOURCC) + return -EINVAL; + + memcpy(&container, bfr + offset, sizeof(container)); + if (((container.version >> 8) & 0xff) != + APPLE_RTKIT_CRASHLOG_CONTAINER_VERSION || + container.size > avail || !container.count || + container.count > (avail - sizeof(container)) / sizeof(record)) + return -EINVAL; + + next = 0; + for (i = 0; i < container.count; i++) { + memcpy(&record, bfr + offset + sizeof(container) + + i * sizeof(record), sizeof(record)); + if (record.id == container.current_id) { + next = record.offset; + break; + } + } + if (next < sizeof(container) || next >= avail) + return -EINVAL; + + id = container.current_id; + offset += next; + } + + return -EINVAL; +} + void apple_rtkit_crashlog_dump(struct apple_rtkit *rtk, u8 *bfr, size_t size) { size_t offset; u32 section_fourcc, section_size; struct apple_rtkit_crashlog_header header; + if (size < sizeof(header)) { + dev_warn(rtk->dev, "RTKit: Crashlog buffer too small: 0x%zx", + size); + return; + } memcpy(&header, bfr, sizeof(header)); + if (header.fourcc == APPLE_RTKIT_CRASHLOG_CONTAINER_FOURCC) { + unsigned int depth; + u32 id; + int ret; + + ret = apple_rtkit_crashlog_locate(bfr, size, &offset, &id, &depth); + if (ret) { + dev_warn(rtk->dev, + "RTKit: Crashlog container holds no complete log (%d)", + ret); + return; + } + dev_warn(rtk->dev, + "RTKit: Crashlog container: log '%c%c%c%c' at 0x%zx (depth %u)", + (id >> 24) & 0xff, (id >> 16) & 0xff, (id >> 8) & 0xff, + id & 0xff, offset, depth); + bfr += offset; + size -= offset; + memcpy(&header, bfr, sizeof(header)); + } + if (header.fourcc != APPLE_RTKIT_CRASHLOG_HEADER_FOURCC) { dev_warn(rtk->dev, "RTKit: Expected crashlog header but got %x", header.fourcc); @@ -196,13 +385,35 @@ void apple_rtkit_crashlog_dump(struct apple_rtkit *rtk, u8 *bfr, size_t size) offset = sizeof(header); while (offset < size) { + if (size - offset < 16) { + dev_warn(rtk->dev, + "RTKit: Crashlog section header at 0x%zx is truncated", + offset); + return; + } memcpy(§ion_fourcc, bfr + offset, 4); memcpy(§ion_size, bfr + offset + 12, 4); - switch (section_fourcc) { - case APPLE_RTKIT_CRASHLOG_HEADER_FOURCC: + if (section_fourcc == APPLE_RTKIT_CRASHLOG_HEADER_FOURCC) { dev_dbg(rtk->dev, "RTKit: End of crashlog reached"); return; + } + /* A zero size would never advance. */ + if (section_size < 16 || section_size > size - offset) { + dev_warn(rtk->dev, + "RTKit: Crashlog section %x at 0x%zx has a bad size 0x%x", + section_fourcc, offset, section_size); + return; + } + /* + * The decoders read up to section_size bytes from 16 bytes into + * the section, so a section is only decoded with at least the + * footer's 16-byte header after it. + */ + if (section_size > size - offset - 16) + break; + + switch (section_fourcc) { case APPLE_RTKIT_CRASHLOG_STR: apple_rtkit_crashlog_dump_str(rtk, bfr + offset + 16, section_size); @@ -223,6 +434,10 @@ void apple_rtkit_crashlog_dump(struct apple_rtkit *rtk, u8 *bfr, size_t size) apple_rtkit_crashlog_dump_regs(rtk, bfr + offset + 16, section_size); break; + case APPLE_RTKIT_CRASHLOG_REGS_CL4: + apple_rtkit_crashlog_dump_regs_cl4(rtk, bfr + offset, + section_size); + break; default: dev_warn(rtk->dev, "RTKit: Unknown crashlog section: %x", diff --git a/drivers/soc/apple/rtkit-internal.h b/drivers/soc/apple/rtkit-internal.h index 0c5acb7fc7090..a4976fd1b456c 100644 --- a/drivers/soc/apple/rtkit-internal.h +++ b/drivers/soc/apple/rtkit-internal.h @@ -23,6 +23,8 @@ #define APPLE_RTKIT_APP_ENDPOINT_START 0x20 #define APPLE_RTKIT_MAX_ENDPOINTS 0x100 #define APPLE_RTKIT_CRASHLOG_HEADER_FOURCC 0x434c4845 +/* A container of crashlogs (one per task or core): 'CLGH'. */ +#define APPLE_RTKIT_CRASHLOG_CONTAINER_FOURCC 0x434c4748 struct apple_rtkit_crashlog_header { u32 fourcc; @@ -82,9 +84,16 @@ struct apple_rtkit { bool crashlog_inherited; bool oslog_inherited; + /* A crash reported before the crashlog buffer was mapped: the log is + * read once the co-processor has written it. */ + struct delayed_work crashlog_late_work; + unsigned int crashlog_late_polls; + struct workqueue_struct *wq; }; void apple_rtkit_crashlog_dump(struct apple_rtkit *rtk, u8 *bfr, size_t size); +int apple_rtkit_crashlog_locate(const u8 *bfr, size_t size, size_t *log_offset, + u32 *log_id, unsigned int *depth); #endif diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index d352083aaa26c..1d56f00c851ec 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -256,6 +256,9 @@ static void apple_rtkit_management_rx_epmap(struct apple_rtkit *rtk, u64 msg) if (!(msg & APPLE_RTKIT_MGMT_EPMAP_LAST)) return; + if (!test_bit(APPLE_RTKIT_EP_CRASHLOG, rtk->endpoints)) + dev_info(rtk->dev, "RTKit: crashlog endpoint not in the endpoint map\n"); + for_each_set_bit(ep, rtk->endpoints, APPLE_RTKIT_APP_ENDPOINT_START) { switch (ep) { /* the management endpoint is started by default */ @@ -695,11 +698,82 @@ static void apple_rtkit_memcpy(struct apple_rtkit *rtk, void *dst, len, DMA_FROM_DEVICE); } +#define APPLE_RTKIT_CRASHLOG_LATE_POLL_MS 250 +#define APPLE_RTKIT_CRASHLOG_LATE_POLLS 20 + +/* + * Whether the crashlog buffer holds a log: an ordinary 'CLHE' log at its + * start, or a 'CLGH' container whose current record leads to a complete one. + */ +static bool apple_rtkit_crashlog_header_valid(struct apple_rtkit *rtk) +{ + struct apple_rtkit_crashlog_header header; + size_t size = rtk->crashlog_buffer.size, offset; + unsigned int depth; + u32 id; + + if (size < sizeof(header)) + return false; + apple_rtkit_memcpy(rtk, &header, &rtk->crashlog_buffer, 0, + sizeof(header)); + if (header.fourcc == APPLE_RTKIT_CRASHLOG_CONTAINER_FOURCC) { + u8 *bfr __free(kfree) = kmalloc(size, GFP_KERNEL); + + if (!bfr) + return false; + apple_rtkit_memcpy(rtk, bfr, &rtk->crashlog_buffer, 0, size); + return !apple_rtkit_crashlog_locate(bfr, size, &offset, &id, + &depth); + } + return header.fourcc == APPLE_RTKIT_CRASHLOG_HEADER_FOURCC && + header.size >= sizeof(header) && + header.size <= size; +} + +static void apple_rtkit_crashlog_late(struct work_struct *work) +{ + struct apple_rtkit *rtk = container_of(to_delayed_work(work), + struct apple_rtkit, + crashlog_late_work); + u8 *bfr __free(kfree) = NULL; + + if (!apple_rtkit_crashlog_header_valid(rtk)) { + if (++rtk->crashlog_late_polls < APPLE_RTKIT_CRASHLOG_LATE_POLLS) + queue_delayed_work(rtk->wq, &rtk->crashlog_late_work, + msecs_to_jiffies(APPLE_RTKIT_CRASHLOG_LATE_POLL_MS)); + else + dev_err(rtk->dev, + "RTKit: crash reported, but no crashlog was written in %u ms\n", + APPLE_RTKIT_CRASHLOG_LATE_POLLS * + APPLE_RTKIT_CRASHLOG_LATE_POLL_MS); + return; + } + + /* The crash was reported and the client told; only dump the log. */ + dev_err(rtk->dev, "RTKit: co-processor crashed; crashlog written after %u ms\n", + (rtk->crashlog_late_polls + 1) * APPLE_RTKIT_CRASHLOG_LATE_POLL_MS); + bfr = kzalloc(rtk->crashlog_buffer.size, GFP_KERNEL); + if (!bfr) + return; + apple_rtkit_memcpy(rtk, bfr, &rtk->crashlog_buffer, 0, + rtk->crashlog_buffer.size); + apple_rtkit_crashlog_dump(rtk, bfr, rtk->crashlog_buffer.size); +} + static void apple_rtkit_crashlog_rx(struct apple_rtkit *rtk, u64 msg) { u8 type = FIELD_GET(APPLE_RTKIT_SYSLOG_TYPE, msg); u8 *bfr __free(kfree) = NULL; - struct apple_rtkit_crashlog_header header; + /* + * An undiscovered crashlog endpoint was never started, so the + * co-processor was never asked for a buffer and, as in an inherited + * session, a message there can only be a crash report. Firmware that + * leaves this endpoint out of its map still reports crashes on it, + * with a buffer of its own. + */ + bool reports_crash = rtk->crashlog_inherited || + !test_bit(APPLE_RTKIT_EP_CRASHLOG, rtk->endpoints); + bool await_log = false; if (type != APPLE_RTKIT_CRASHLOG_CRASH) { dev_warn(rtk->dev, "RTKit: Unknown crashlog message: %llx\n", @@ -710,26 +784,23 @@ static void apple_rtkit_crashlog_rx(struct apple_rtkit *rtk, u64 msg) if (!rtk->crashlog_buffer.size) { if (apple_rtkit_common_rx_get_buffer(rtk, &rtk->crashlog_buffer, APPLE_RTKIT_EP_CRASHLOG, msg)) - goto unmapped; + goto no_buffer; /* * In a session that went through the handshake, the first - * message on this endpoint is the buffer request. Firmware may - * place that buffer in memory which still holds the log of an - * earlier crash, so only look for one in an inherited session. + * message on a started crashlog endpoint is the buffer request. + * Firmware may place that buffer in memory which still holds + * the log of an earlier crash, so only look for one when the + * message can only be a crash report. */ - if (!rtk->crashlog_inherited) + if (!reports_crash) return; } - if (rtk->crashlog_inherited) { - if (rtk->crashlog_buffer.size < sizeof(header)) - goto unmapped; - - apple_rtkit_memcpy(rtk, &header, &rtk->crashlog_buffer, 0, - sizeof(header)); - if (header.fourcc != APPLE_RTKIT_CRASHLOG_HEADER_FOURCC || - header.size < sizeof(header) || - header.size > rtk->crashlog_buffer.size) - goto unmapped; + if (reports_crash) { + if (rtk->crashlog_buffer.size < + sizeof(struct apple_rtkit_crashlog_header)) + goto no_buffer; + if (!apple_rtkit_crashlog_header_valid(rtk)) + goto no_log; } dev_err(rtk->dev, "RTKit: co-processor has crashed\n"); @@ -754,14 +825,34 @@ static void apple_rtkit_crashlog_rx(struct apple_rtkit *rtk, u64 msg) rtk->ops->crashed(rtk->cookie, bfr, bfr ? rtk->crashlog_buffer.size : 0); return; -unmapped: - /* An inherited session already negotiated its crash buffer. */ - if (!rtk->crashlog_inherited) +no_buffer: + /* Otherwise this was a buffer request that could not be served. */ + if (!reports_crash) return; - dev_err(rtk->dev, "RTKit: adopted co-processor crashed without a readable log\n"); + dev_err(rtk->dev, "RTKit: co-processor crashed without a usable crashlog buffer\n"); + goto crashed; + +no_log: + /* The buffer is mapped, but holds no valid log (yet). */ + dev_err(rtk->dev, "RTKit: co-processor crashed without a readable log\n"); + /* + * A co-processor whose crashlog endpoint was never discovered or + * started (the external DCP on T6050) requests the buffer only when + * it crashes, and writes the log after receiving it. Read the log once + * it is there, rather than dropping it. + */ + await_log = !test_bit(APPLE_RTKIT_EP_CRASHLOG, rtk->endpoints); + +crashed: rtk->crashed = true; if (rtk->ops->crashed) rtk->ops->crashed(rtk->cookie, NULL, 0); + + if (await_log) { + rtk->crashlog_late_polls = 0; + queue_delayed_work(rtk->wq, &rtk->crashlog_late_work, + msecs_to_jiffies(APPLE_RTKIT_CRASHLOG_LATE_POLL_MS)); + } } static void apple_rtkit_ioreport_rx(struct apple_rtkit *rtk, u64 msg) @@ -1279,6 +1370,9 @@ static void apple_rtkit_detach_rx(struct apple_rtkit *rtk) static void apple_rtkit_stop_rx(struct apple_rtkit *rtk) { apple_rtkit_detach_rx(rtk); + /* A crashlog message still being handled may queue the late read. */ + flush_workqueue(rtk->wq); + cancel_delayed_work_sync(&rtk->crashlog_late_work); destroy_workqueue(rtk->wq); } @@ -1382,6 +1476,7 @@ static struct apple_rtkit *__apple_rtkit_init(struct device *dev, void *cookie, init_completion(&rtk->coreanalytics_completion); init_completion(&rtk->iop_pwr_ack_completion); init_completion(&rtk->ap_pwr_ack_completion); + INIT_DELAYED_WORK(&rtk->crashlog_late_work, apple_rtkit_crashlog_late); bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS); set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints); @@ -1512,6 +1607,7 @@ int apple_rtkit_reinit(struct apple_rtkit *rtk) /* make sure we don't handle any messages while reinitializing */ apple_rtkit_detach_rx(rtk); flush_workqueue(rtk->wq); + cancel_delayed_work_sync(&rtk->crashlog_late_work); apple_rtkit_free_buffer(rtk, &rtk->coreanalytics_buffer); apple_rtkit_free_buffer(rtk, &rtk->ioreport_buffer); diff --git a/tools/asahi/rtkit-crashlog-tests.py b/tools/asahi/rtkit-crashlog-tests.py new file mode 100755 index 0000000000000..382c8d39cd1a3 --- /dev/null +++ b/tools/asahi/rtkit-crashlog-tests.py @@ -0,0 +1,512 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Feed RTKit's crashlog parser a container log and malformed logs. + +drivers/soc/apple/rtkit-crashlog.c is compiled whole and unchanged, with ASan +and UBSan, against a stand-in for rtkit-internal.h that carries the real +header's crashlog definitions. Every log is allocated at its exact size, so a +read past it is a sanitizer error, and a walk that stops advancing runs into +the line limit. + +The container log has the layout of a T6050 DCP crash (a 'CLGH' container, +a nested one, and at 0x2000 the 'CLHE' log of task RTAL with eleven sections +and a footer), with neutral contents. The malformed logs cover a zero and an +oversized section size, a truncated section header, a short mailbox section, +a short and a truncated 'Crl4' section, unterminated strings, short buffers +and broken containers. --log FILE feeds a raw crashlog buffer read from a +device instead, and prints what the parser makes of it. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[2] +SOURCE = 'drivers/soc/apple/rtkit-crashlog.c' +HEADER = 'drivers/soc/apple/rtkit-internal.h' + +# Each mutation removes one bound; a test case must catch it. +MUTATIONS = { + 'buffer-header': ('if (size < sizeof(header)) {', 'if (0) {'), + 'section-header': ('if (size - offset < 16) {', 'if (0) {'), + 'section-size': ('if (section_size < 16 || section_size > size - offset) {', 'if (0) {'), + 'footer-room': ('if (section_size > size - offset - 16)\n\t\t\tbreak;', ''), + 'mailbox-minimum': ('if (size < 28) {', 'if (0) {'), + 'version-string': ('"RTKit: Version: %.*s", (int)(size - 16), bfr + 16', '"RTKit: Version: %s", bfr + 16'), + 'message-string': ('%.*s", idx,\n\t\t\t\t (int)(end - ptr), ptr', '%s", idx,\n\t\t\t\t ptr'), +} + +STUB = r''' +#include +#include +#include +#include +#include +#include +#include +#include +/* As linux/build_bug.h has it: the message is optional. */ +#undef static_assert +#define __static_assert(expr, msg, ...) _Static_assert(expr, msg) +#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) +typedef uint8_t u8; +typedef uint32_t u32; +typedef unsigned long long u64; +#define __packed __attribute__((packed)) +#define BIT(n) (1UL << (n)) +#define EINVAL 22 +#define EAGAIN 11 +struct device { int id; }; +struct apple_rtkit { struct device *dev; }; +void dev_printk(const char *level, struct device *dev, const char *fmt, ...) + __attribute__((format(printf, 3, 4))); +#define dev_warn(dev, ...) dev_printk("warn", dev, __VA_ARGS__) +#define dev_dbg(dev, ...) dev_printk("dbg", dev, __VA_ARGS__) +@DEFINITIONS@ +int apple_rtkit_crashlog_locate(const u8 *bfr, size_t size, size_t *log_offset, + u32 *log_id, unsigned int *depth); +void apple_rtkit_crashlog_dump(struct apple_rtkit *rtk, u8 *bfr, size_t size); +''' + +UNALIGNED = r''' +#include +#include +static inline uint32_t get_unaligned_le32(const void *p) { uint32_t v; memcpy(&v, p, 4); return v; } +static inline unsigned long long get_unaligned_le64(const void *p) +{ unsigned long long v; memcpy(&v, p, 8); return v; } +''' + +MAIN = r''' +#include "rtkit-internal.h" + +#define FOURCC(s) (((u32)(s)[0] << 24) | ((u32)(s)[1] << 16) | ((u32)(s)[2] << 8) | (u32)(s)[3]) +#define MAX_LINES 1000 + +static char lines[MAX_LINES][512]; +static unsigned int n_lines; +static struct device device; +static struct apple_rtkit rtk = { .dev = &device }; + +void dev_printk(const char *level, struct device *dev, const char *fmt, ...) +{ + char *line = lines[n_lines]; + va_list ap; + size_t n; + + assert(dev == &device); + /* A walk that does not advance prints without end. */ + assert(n_lines < MAX_LINES - 1); + n_lines++; + va_start(ap, fmt); + vsnprintf(line, sizeof(lines[0]), fmt, ap); + va_end(ap); + n = strlen(line); + while (n && line[n - 1] == '\n') + line[--n] = 0; + printf(" %s: %s\n", level, line); +} + +static void put32(u8 *b, size_t off, u32 v) { memcpy(b + off, &v, 4); } +static void put64(u8 *b, size_t off, u64 v) { memcpy(b + off, &v, 8); } + +/* A section header: its type, a fill word, flags and its size, header included. */ +static size_t section(u8 *b, size_t off, const char *type, u32 flags, u32 size) +{ + put32(b, off, FOURCC(type)); + put32(b, off + 4, 0xefefefef); + put32(b, off + 8, flags); + put32(b, off + 12, size); + return off + size; +} + +/* A 'CLHE' header (or footer) of a log of `size` bytes at `off`. */ +static void clhe(u8 *b, size_t off, u32 size, u32 flags) +{ + put32(b, off, FOURCC("CLHE")); + put32(b, off + 4, 0x303); + put32(b, off + 8, size); + put32(b, off + 12, flags); +} + +/* A 'CLGH' container at `off` with `count` (id, offset) records. */ +static void clgh(u8 *b, size_t off, u32 size, const char *current, unsigned int count, + const char *const *ids, const u32 *offsets) +{ + put32(b, off, FOURCC("CLGH")); + put32(b, off + 4, 0x202); + put32(b, off + 8, size); + put32(b, off + 12, FOURCC(current)); + put32(b, off + 16, count); + for (unsigned int i = 0; i < count; i++) { + put64(b, off + 0x14 + i * 0x18, FOURCC(ids[i])); + put32(b, off + 0x14 + i * 0x18 + 16, offsets[i]); + } +} + +/* Runs the dump on an exact-size copy of `log` and returns the number of lines. */ +static unsigned int dump(const char *name, const u8 *log, size_t size) +{ + u8 *copy = malloc(size ? size : 1); + + assert(copy); + memcpy(copy, log, size); + n_lines = 0; + printf("%s (%zu bytes)\n", name, size); + apple_rtkit_crashlog_dump(&rtk, copy, size); + free(copy); + return n_lines; +} + +static bool printed(const char *text) +{ + for (unsigned int i = 0; i < n_lines; i++) + if (strstr(lines[i], text)) + return true; + return false; +} + +static bool ended(void) +{ + return n_lines && !strcmp(lines[n_lines - 1], "RTKit: End of crashlog reached"); +} + +#define DCP_SIZE 0x17000 +#define DCP_LOG 0x2000 +#define DCP_LOG_SIZE 0x26e0 +#define MAILBOX_ENTRIES 128 + +/* The layout of a T6050 DCP crashlog: the RTAL task's log, two containers deep. */ +static void dcp_log(u8 *b) +{ + static const char *const top[] = { "CL4U", "CL4K" }, *const tasks[] = { "root", "RTAL" }; + static const u32 top_offsets[] = { 0x44, 0x12000 }, task_offsets[] = { 0x44, 0x1fbc }; + static const u32 pattern[8] = { 0x202, 0x242, 0x302, 0x342, 0x302, 0x342, 0x202, 0x242 }; + size_t off = DCP_LOG + 0x20, body; + + memset(b, 0, DCP_SIZE); + clgh(b, 0, DCP_SIZE, "CL4U", 2, top, top_offsets); + clgh(b, 0x44, 0x11fbc, "RTAL", 2, tasks, task_offsets); + clhe(b, DCP_LOG, DCP_LOG_SIZE, 0x820); + put32(b, DCP_LOG + 0x1c, FOURCC("RTAL")); + + body = off + 16; + off = section(b, off, "Cstr", 0x101, 0x60); + put32(b, body, 1); + strcpy((char *)b + body + 4, "crash message [0x87aa9a0]"); + off = section(b, off, "Cver", 0x300, 0xa0); + body = off + 16; + off = section(b, off, "Ctim", 0x100, 0x20); + put64(b, body, 87481704354ULL); + off = section(b, off, "Ccst", 0x400, 0x70); + off = section(b, off, "Crtk", 0x300, 0x17c0); + body = off + 16; + off = section(b, off, "Cmbx", 0x300, 0xc30); + for (unsigned int i = 0; i < MAILBOX_ENTRIES; i++) { + size_t e = body + 28 + i * 0x18; + + put64(b, e, 0x37); + put64(b, e + 8, pattern[i % 8]); + put32(b, e + 16, 0xda8656a9 + i * 0x1000); + } + off = section(b, off, "Cpct", 0x200, 0x50); + off = section(b, off, "Csta", 0x100, 0x30); + off = section(b, off, "Cced", 0x100, 0x50); + off = section(b, off, "Chex", 0x100, 0x20); + off = section(b, off, "Csta", 0x100, 0x30); + assert(off == DCP_LOG + DCP_LOG_SIZE - 0x20); + clhe(b, off, DCP_LOG_SIZE, 0x820); +} + +static void test_dcp_log(void) +{ + static const char *const expect[] = { + "RTKit: Crashlog container: log 'RTAL' at 0x2000 (depth 2)", + "RTKit: Message (id=1): crash message [0x87aa9a0]", + "RTKit: Version: ", + "RTKit: Crash time: 87481704354", + "RTKit: Unknown crashlog section: 43637374", + "RTKit: Unknown crashlog section: 4372746b", + "RTKit: Mailbox history (type = 0, index = 0)", + "RTKit: Unknown crashlog section: 43706374", + "RTKit: Unknown crashlog section: 43737461", + "RTKit: Unknown crashlog section: 43636564", + "RTKit: Unknown crashlog section: 43686578", + "RTKit: Unknown crashlog section: 43737461", + "RTKit: End of crashlog reached", + }; + static u8 b[DCP_SIZE]; + unsigned int n, i, j = 0, entries = 0; + size_t log_offset; + unsigned int depth; + u32 id; + + dcp_log(b); + assert(!apple_rtkit_crashlog_locate(b, DCP_SIZE, &log_offset, &id, &depth)); + assert(log_offset == DCP_LOG && id == FOURCC("RTAL") && depth == 2); + n = dump("T6050 DCP container log", b, DCP_SIZE); + for (i = 0; i < n; i++) { + if (!strncmp(lines[i], "RTKit: #", 9)) { + entries++; + continue; + } + assert(j < sizeof(expect) / sizeof(expect[0])); + assert(!strcmp(lines[i], expect[j])); + j++; + } + assert(j == sizeof(expect) / sizeof(expect[0])); + assert(entries == MAILBOX_ENTRIES); + assert(printed("RTKit: #000@da8656a9: 0000000000000037 0000000000000202")); + assert(printed("RTKit: #127@da8e46a9: 0000000000000037 0000000000000242")); + + /* Still being written: not a complete log. */ + put32(b, DCP_LOG + 12, 0x820 | 0x100); + assert(apple_rtkit_crashlog_locate(b, DCP_SIZE, &log_offset, &id, &depth) == -EAGAIN); + dump("log still being written", b, DCP_SIZE); + assert(printed("holds no complete log (-11)")); + put32(b, DCP_LOG + 12, 0x820); + + /* A record past the buffer, and a container that names itself. */ + put32(b, 0x44 + 0x14 + 0x18 + 16, DCP_SIZE); + assert(apple_rtkit_crashlog_locate(b, DCP_SIZE, &log_offset, &id, &depth) == -EINVAL); + dump("record past the buffer", b, DCP_SIZE); + assert(printed("holds no complete log (-22)")); + put32(b, 0x44 + 0x14 + 0x18 + 16, 0); + assert(apple_rtkit_crashlog_locate(b, DCP_SIZE, &log_offset, &id, &depth) == -EINVAL); + /* The log's section walk is bounded by the log, not the buffer. */ + dcp_log(b); + put32(b, DCP_LOG + 8, 0x20 + 0x60); + dump("log cut after its first section", b, DCP_SIZE); + assert(printed("End of crashlog reached but no footer present") && !printed("Message")); +} + +/* A bare log: a header, then sections built by the caller, then optionally a footer. */ +static u8 logbuf[0x1000]; + +static size_t bare(void) +{ + memset(logbuf, 0, sizeof(logbuf)); + return 0x20; +} + +static unsigned int finish(const char *name, size_t off, bool footer) +{ + size_t size = off + (footer ? 0x20 : 0); + + if (footer) + clhe(logbuf, off, size, 0); + clhe(logbuf, 0, size, 0); + return dump(name, logbuf, size); +} + +static void test_malformed(void) +{ + size_t off, body; + unsigned int i; + + off = section(logbuf, bare(), "Ctim", 0x100, 0x20); + section(logbuf, off, "Csta", 0x100, 0); + finish("zero section size", off + 0x30, true); + assert(printed("section 43737461 at 0x40 has a bad size 0x0") && n_lines == 2); + + off = section(logbuf, bare(), "Ctim", 0x100, 0x20); + section(logbuf, off, "Cmbx", 0x300, 0x10000); + finish("oversized section", off + 0x40, true); + assert(printed("section 436d6278 at 0x40 has a bad size 0x10000")); + + off = bare(); + put32(logbuf, off, FOURCC("Ctim")); + finish("truncated section header", off + 8, false); + assert(printed("section header at 0x20 is truncated") && n_lines == 1); + + off = section(logbuf, bare(), "Ctim", 0x100, 0x20); + finish("section, then the footer", off, true); + assert(printed("Crash time: 0") && ended() && n_lines == 2); + + /* Without a footer the last section may not be decoded: its decoder reads 16 bytes on. */ + body = bare(); + off = section(logbuf, body, "Cstr", 0x101, 0x40); + put32(logbuf, body + 16, 1); + memset(logbuf + body + 20, 'm', 0x40 - 20); + finish("section reaching the end of a log without a footer", off, false); + assert(printed("End of crashlog reached but no footer present") && n_lines == 1); + + for (u32 size = 16; size < 28; size++) { + off = section(logbuf, bare(), "Cmbx", 0x300, size); + finish("short mailbox section", off, true); + assert(printed("Mailbox section too small") && ended()); + } + off = section(logbuf, bare(), "Cmbx", 0x300, 28 + 2 * 0x18); + finish("two-entry mailbox section", off, true); + assert(printed("RTKit: #001@") && !printed("#002") && ended()); + + body = bare(); + off = section(logbuf, body, "Crl4", 0x100, 0x378); + put32(logbuf, body + 0x14, 0xd8); + put64(logbuf, body + 0x110, 0xfffffe0007001234ULL); + put64(logbuf, body + 0x360, 0xdead0000ULL); + for (i = 0; i < 28; i++) + put64(logbuf, body + 0x18 + i * 8, 0x1000 + i); + finish("whole Crl4 section", off, true); + assert(printed("Exception dump (Crl4):") && printed("PC = 0xfffffe0007001234") && + printed("FAR = 0xdead0000") && + printed("x24-x27 = 0000000000001018 0000000000001019 000000000000101a 000000000000101b") && + ended()); + + off = section(logbuf, bare(), "Crl4", 0x100, 0x378); + finish("truncated Crl4 section", 0x20 + 0x100, true); + assert(printed("section 43726c34 at 0x20 has a bad size 0x378")); + + off = section(logbuf, bare(), "Crl4", 0x100, 0x40); + finish("short Crl4 section", off, true); + assert(printed("Crl4 section too small: 0x40") && ended()); + + body = bare(); + off = section(logbuf, body, "Crl4", 0x100, 0x378); + put32(logbuf, body + 0x10, 1 << 16); + put32(logbuf, body + 0x14, 0xd8); + finish("undecodable Crl4 frame", off, true); + assert(printed("Crl4 frame not decodable") && ended()); + + /* Strings without a terminator, against the end of the log. */ + body = bare(); + off = section(logbuf, body, "Cver", 0x300, 0x40); + memset(logbuf + body + 32, 'v', 0x40 - 32); + put32(logbuf, off, FOURCC("CLHE")); + memset(logbuf + off + 4, 0x55, 12); + clhe(logbuf, 0, off + 16, 0); + dump("unterminated version", logbuf, off + 16); + assert(!strcmp(lines[0], "RTKit: Version: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvEHLCUUUUUUUUUUUU")); + + body = bare(); + off = section(logbuf, body, "Cstr", 0x101, 0x40); + put32(logbuf, body + 16, 1); + memset(logbuf + body + 20, 'm', 0x40 - 20); + put32(logbuf, off, FOURCC("CLHE")); + memset(logbuf + off + 4, 0x55, 12); + clhe(logbuf, 0, off + 16, 0); + dump("unterminated message", logbuf, off + 16); + assert(!strcmp(lines[0], "RTKit: Message (id=1): mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmEHLCUUUUUUUUUUUU")); + + body = bare(); + off = section(logbuf, body, "Cstr", 0x101, 0x40); + put32(logbuf, body + 16, 2); + strcpy((char *)logbuf + body + 20, "first\nsecond\n"); + finish("message lines", off, true); + assert(printed("Message (id=2): first") && printed("Message (id=2): second") && ended()); + + /* Buffers too short for a header, and logs larger than their buffer. */ + for (size_t size = 0; size < 0x20; size += 4) { + memset(logbuf, 0, sizeof(logbuf)); + clhe(logbuf, 0, 0x20, 0); + dump("short buffer", logbuf, size); + assert(printed("Crashlog buffer too small")); + } + clhe(logbuf, 0, 0x40, 0); + dump("log larger than its buffer", logbuf, 0x20); + assert(printed("Crashlog size (40) is too large")); + memset(logbuf, 0, sizeof(logbuf)); + put32(logbuf, 0, FOURCC("CLGH")); + dump("container header only", logbuf, 0x20); + assert(printed("holds no complete log (-22)")); +} + +static int test_file(const char *path) +{ + FILE *f = fopen(path, "rb"); + size_t size; + u8 *b; + + assert(f); + fseek(f, 0, SEEK_END); + size = ftell(f); + rewind(f); + b = malloc(size); + assert(b && fread(b, 1, size, f) == size); + fclose(f); + dump(path, b, size); + free(b); + return 0; +} + +int main(int argc, char **argv) +{ + assert(FOURCC("CLHE") == APPLE_RTKIT_CRASHLOG_HEADER_FOURCC); + assert(FOURCC("CLGH") == APPLE_RTKIT_CRASHLOG_CONTAINER_FOURCC); + if (argc > 1) + return test_file(argv[1]); + test_dcp_log(); + test_malformed(); + printf("PASS rtkit crashlog: container log decoded; malformed logs refused without a loop or an over-read\n"); + return 0; +} +''' + + +def definitions(header): + """The crashlog fourccs and header struct of rtkit-internal.h.""" + found = re.findall(r'^#define APPLE_RTKIT_CRASHLOG_\w+_FOURCC .*$', header, re.M) + found += re.findall(r'^struct apple_rtkit_crashlog_header \{.*?^\};\n(?:\nstatic_assert\(.*?\);)?', + header, re.M | re.S) + assert len(found) == 3, found + return '\n'.join(found) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--ref', help='Git revision of the parser to exercise') + parser.add_argument('--out', type=Path) + parser.add_argument('--mutation', choices=sorted(MUTATIONS)) + parser.add_argument('--log', type=Path, help='a raw crashlog buffer to dump') + args = parser.parse_args() + + def read(path): + if args.ref: + return subprocess.check_output(['git', 'show', f'{args.ref}:{path}'], cwd=ROOT, text=True) + return (ROOT / path).read_text() + source, header = read(SOURCE), read(HEADER) + if args.mutation: + old, new = MUTATIONS[args.mutation] + # A mutation that no longer matches the parser would test nothing. + assert source.count(old) == 1, 'stale mutation: ' + args.mutation + source = source.replace(old, new) + + def run(out): + out.mkdir(parents=True, exist_ok=True) + (out / 'linux').mkdir(exist_ok=True) + (out / 'rtkit-crashlog.c').write_text(source) + (out / 'rtkit-internal.h').write_text(STUB.replace('@DEFINITIONS@', definitions(header))) + (out / 'linux/unaligned.h').write_text(UNALIGNED) + (out / 'main.c').write_text(MAIN) + exe = out / 'crashlog-tests' + compiler = subprocess.run(['clang', '-std=gnu11', '-O1', '-g', '-Wall', '-Werror', '-Wno-unused-function', + '-fsanitize=address,undefined', '-fno-sanitize-recover=all', + '-I', str(out), str(out / 'rtkit-crashlog.c'), str(out / 'main.c'), + '-o', str(exe)], capture_output=True, text=True) + (out / 'compile.log').write_text(compiler.stdout + compiler.stderr) + if compiler.returncode: + print(compiler.stdout + compiler.stderr) + compiler.check_returncode() + env = dict(os.environ, ASAN_OPTIONS='abort_on_error=1:detect_leaks=0', UBSAN_OPTIONS='print_stacktrace=1') + result = subprocess.run([str(exe)] + ([str(args.log.resolve())] if args.log else []), + capture_output=True, text=True, env=env) + receipt = {'source_sha256': hashlib.sha256(source.encode()).hexdigest(), 'ref': args.ref, + 'mutation': args.mutation, 'log': str(args.log) if args.log else None, + 'exit': result.returncode} + (out / 'results.json').write_text(json.dumps(dict(receipt, stdout=result.stdout, + stderr=result.stderr), indent=2) + '\n') + print(result.stdout + result.stderr) + print(json.dumps(receipt)) + return result.returncode != 0 + + if args.out: + return run(args.out) + with tempfile.TemporaryDirectory(prefix='rtkit-crashlog-') as out: + return run(Path(out)) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tools/asahi/rtkit-oslog-tests.py b/tools/asahi/rtkit-oslog-tests.py index 160c163758d54..df50c90350b81 100644 --- a/tools/asahi/rtkit-oslog-tests.py +++ b/tools/asahi/rtkit-oslog-tests.py @@ -41,6 +41,7 @@ def function(source, name): #define spin_unlock_irqrestore(l,f) ((void)(l), (void)(f)) struct apple_rtkit_shmem { size_t size; }; struct mailbox { int rx_lock; void (*rx)(void); }; +struct delayed_work { int pending; }; struct apple_rtkit { void *dev, *wq; struct mailbox *mbox; @@ -53,6 +54,7 @@ def function(source, name): char *syslog_msg_buffer; size_t syslog_n_entries, syslog_msg_size; int epmap_completion, iop_pwr_ack_completion, ap_pwr_ack_completion, coreanalytics_completion; + struct delayed_work crashlog_late_work; }; static unsigned sent, warnings, errors, requests, detachments, flushes, freed, checks; static int send_error, setup_error, start_error; @@ -70,6 +72,7 @@ def function(source, name): static void apple_rtkit_rx(void) {} static void apple_rtkit_detach_rx(struct apple_rtkit *r) { ++detachments; r->shutting_down=true; } static void flush_workqueue(void *w) { (void)w; ++flushes; } +#define cancel_delayed_work_sync(w) ((void)(w), false) static void apple_rtkit_free_buffer(struct apple_rtkit *r, struct apple_rtkit_shmem *b) { (void)r; ++freed; b->size=0; } #define kfree(p) ((void)(p)) diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 5c5ca93c68594..b8ea5fdd70b11 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -145,6 +145,9 @@ def mutations(flag, names, base, expect=KILL): + mutations('--mutation', ('ack-clobber', 'arm-ack', 'offset-seconds', 'offset-sign', 'event-bit', 'wrong-base'), out()), 'qspi-nvram-tests.py': [(out('--output'), PASS)], + 'rtkit-crashlog-tests.py': [(out(), PASS)] + + mutations('--mutation', ('buffer-header', 'section-header', 'section-size', 'footer-room', + 'mailbox-minimum', 'version-string', 'message-string'), out()), 'rtkit-oslog-tests.py': [(out(), PASS)] + mutations('--mutate', ('admission', 'channel', 'counter', 'reset', 'buffer'), out()), 't6030-clock-tests.py': [(out(), PASS)],