Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

drivers: socinfo, thermal, pmic -- the chip's identity, its 27 temperature sensors, and the PMICs' inventory with their clock

shimoku9851546df649
diff --git a/configs/boot-args.lst b/configs/boot-args.lst
index f9641e5..e47c701 100644
--- a/configs/boot-args.lst
+++ b/configs/boot-args.lst
@@ -191,6 +191,8 @@ modem_wait k
modem_wda_off k
net_names k
net_trace k
+pmic k
+pmic_list k
privs k
rd kl
rec_mb k
@@ -220,6 +222,8 @@ soc_mpss_mem k
soc_pas_ipa k
soc_pas_mpss k
soc_smem k
+soc_smem_size k
+socinfo k
sound k
sound_below k
sound_gain k
@@ -234,6 +238,12 @@ switcher_wallpaper u
syssleep k
systrace k
systrace_name k
+thermal k
+thermal_log k
+thermal_poll k
+thermal_stop k
+thermal_tm0 k
+thermal_tm1 k
torch kl
torch_max_s k
torch_secs k
diff --git a/docs/boot-args.md b/docs/boot-args.md
index 3c01b11..5b02b97 100644
--- a/docs/boot-args.md
+++ b/docs/boot-args.md
@@ -2,7 +2,7 @@
Made by `tools/boot-args.py --markdown`; CI fails if it is out of date with the source (`tools/boot-args.py --check`).
-Every argument that the kernel, the loader or user space of this port reads, and which file reads it. **304 arguments**: 255 read by the kernel, 14 by the loader, 44 by user space (one is read in more than one place).
+Every argument that the kernel, the loader or user space of this port reads, and which file reads it. **314 arguments**: 265 read by the kernel, 14 by the loader, 44 by user space (one is read in more than one place).
This is a catalogue of what exists and who reads it, not a manual. What an argument means is in the head of the file that reads it. The stable switches have a symbol in the configuration (`tools/hakios-config.py help SYMBOL`, `docs/configs.md`); the rest are the knobs of bring-up (a driver's steps, pauses, traces, test modes) and can change with the driver. Any of them can be given in a configuration with `CONFIG_BOOTARGS="name=value ..."`, which checks that each name is in this list.
@@ -178,7 +178,7 @@ Layers: **K** the kernel, **L** the loader, **U** user space (through `sysctl ke
| `soc_mpss_mem` | K | `hakios.mpss.c` | | |
| `soc_pas_ipa` | K | `hakios.mpss.c` | | |
| `soc_pas_mpss` | K | `hakios.mpss.c` | | |
-| `soc_smem` | K | `hakios.mpss.c` | | |
+| `soc_smem` | K | `hakios.mpss.c`, `hakios.socinfo.c` | | |
## Audio and battery (31)
@@ -313,6 +313,21 @@ Layers: **K** the kernel, **L** the loader, **U** user space (through `sysctl ke
| `torch_max_s` | K | `hakios.torch.c` | | |
| `torch_secs` | K | `hakios.torch.c` | | |
+## Chip, thermal and PMIC (10)
+
+| Argument | Layer | Read by | Configuration | Set by |
+|---|---|---|---|---|
+| `pmic` | K | `hakios.pmic.c` | | |
+| `pmic_list` | K | `hakios.pmic.c` | | |
+| `soc_smem_size` | K | `hakios.socinfo.c` | | |
+| `socinfo` | K | `hakios.socinfo.c` | | |
+| `thermal` | K | `hakios.thermal.c` | | |
+| `thermal_log` | K | `hakios.thermal.c` | | |
+| `thermal_poll` | K | `hakios.thermal.c` | | |
+| `thermal_stop` | K | `hakios.thermal.c` | | |
+| `thermal_tm0` | K | `hakios.thermal.c` | | |
+| `thermal_tm1` | K | `hakios.thermal.c` | | |
+
## Version (1)
| Argument | Layer | Read by | Configuration | Set by |
diff --git a/drivers/core/hakios.socinfo.c b/drivers/core/hakios.socinfo.c
new file mode 100644
index 0000000..be86495
--- /dev/null
+++ b/drivers/core/hakios.socinfo.c
@@ -0,0 +1,336 @@
+/*
+ * What chip this is, from the chip itself: sysctl kern.hakios.soc_*. Only with boot-arg socinfo=1.
+ *
+ * Android's /sys/devices/soc0/{soc_id,revision,serial_number,machine,family,...} come from one item of the
+ * SoC's shared memory ("SMEM"), which the boot firmware fills before anything else runs: the "socinfo"
+ * structure (SMEM item 137). Linux's drivers/soc/qcom/socinfo.c reads it; so does this. It says which
+ * chip and revision this is, which board (hardware platform and its subtype), which foundry made the die,
+ * and the chip's serial number. (The structure also lists the board's power-management chips; this does not
+ * read that part yet.) A kernel that is built for one SoC family can then check the phone it was put on really is one, instead of
+ * trusting that the device tree handed to it came from this phone (docs/port-pkg110.md: a phone that
+ * reports itself as another model is the case that made this worth writing).
+ *
+ * THIS DRIVER ONLY READS. The SMEM region is firmware's: 0x200000 bytes at soc_smem (the phone's device tree
+ * reserves it as smem@80900000, no-map). It is read with byte loads only, because the memory is mapped as
+ * device memory here and an unaligned or wide load of it would fault; every offset is checked against the
+ * region before it is read, and a header that is not what it should be ends the search with a line that says
+ * what was found instead.
+ *
+ * Where things are in SMEM (Linux drivers/soc/qcom/smem.c):
+ * - a table of "partitions" in the last 4 KB of the region ("$TOC"), one of which is the GLOBAL one (host
+ * 0xfffe on both sides): a header ("$PRT") and, from its start, a chain of items, each a 16-byte entry
+ * (0xa5a5 canary, item number, size, padding) and then its data;
+ * - or, on the older layout, a header at the start of the region with a fixed table of 512 items.
+ * Both are tried, the partition table first.
+ *
+ * The structure grows by appending; a field is read only when the item is long enough to hold it.
+ *
+ * Status (what has and has not been seen): written from Linux's smem.c and socinfo.c as remembered, not from a
+ * copy of them. RUN ON THE PHONE (POCO X5 Pro, 2026-10-27, five runs): the item is found in the global partition and
+ * says format 0xf, SoC 475 (the device tree's msm-id 0x1db), revision 1.0, board platform 11, foundry 1, 272 bytes, raw id
+ * 402. What the first runs got wrong, and the diagnostic that found it: a partition-table entry is 48 bytes, not 32;
+ * the older table at the start of the region is not there on this phone (its words are text), so the lookup in it
+ * finds nothing and that is harmless. The firmware build string came out empty, and the PMIC list at the end of the
+ * structure is not read. Not run in QEMU: QEMU has no SMEM, and the driver then says so and does nothing.
+ *
+ * Boot-args:
+ * socinfo=1 read it
+ * soc_smem=<pa> where SMEM is (also the modem driver's argument; 0x80900000 on the reference phone)
+ * soc_smem_size=<n> its size (0x200000)
+ */
+#include <kern/startup.h>
+#include <libkern/libkern.h>
+#include <mach/mach_types.h>
+#include <pexpert/pexpert.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+
+extern vm_offset_t ml_io_map(vm_offset_t phys_addr, vm_size_t size);
+extern void loader_log_putc(char c);
+
+SYSCTL_DECL(_kern_hakios);
+
+static int say(const char *format, ...) __printflike(1, 2);
+
+static int
+say(const char *format, ...)
+{
+ char line[300];
+ va_list list;
+
+ va_start(list, format);
+ vsnprintf(line, sizeof(line), format, list);
+ va_end(list);
+ for (const char *c = line; *c != 0; c++) {
+ loader_log_putc(*c);
+ }
+ return printf("%s", line);
+}
+
+#define SMEM_DEFAULT_PA 0x80900000UL
+#define SMEM_DEFAULT_SIZE 0x200000UL
+#define SMEM_HW_SW_BUILD_ID 137 /* the socinfo structure */
+
+#define PTABLE_MAGIC 0x434f5424u /* "$TOC" */
+#define PART_MAGIC 0x54525024u /* "$PRT" */
+#define PTABLE_ENTRY 48 /* offset, size, flags, host0, host1, cacheline, reserved[7] (Linux smem.c) */
+#define GLOBAL_HOST 0xfffeu
+#define PRIVATE_CANARY 0xa5a5u
+#define LEGACY_ITEMS 512
+#define LEGACY_TOC_OFFSET 208 /* 4 proc_comm (64) + version[32] (128) + 4 words (16) */
+#define LEGACY_SBL_VERSION 7 /* version[7]: 12 means the partitioned layout */
+
+static vm_offset_t smem;
+static vm_size_t smem_size;
+
+static bool
+inside(uint64_t offset, uint64_t length)
+{
+ return offset <= smem_size && length <= smem_size - offset;
+}
+
+static uint8_t
+b8(uint64_t offset)
+{
+ return inside(offset, 1) ? *(volatile uint8_t *)(smem + offset) : 0;
+}
+
+static uint32_t
+le16(uint64_t offset)
+{
+ return b8(offset) | (uint32_t)b8(offset + 1) << 8;
+}
+
+static uint32_t
+le32(uint64_t offset)
+{
+ return le16(offset) | le16(offset + 2) << 16;
+}
+
+/*
+ * Item `item` of the shared memory: its offset and length in the region, or false. `how` says
+ * which layout it was found in, for the log.
+ */
+static bool
+smem_item(uint32_t item, uint64_t *offset, uint64_t *length, const char **how)
+{
+ uint64_t table = smem_size - 0x1000;
+
+ if (smem_size >= 0x2000 && le32(table) == PTABLE_MAGIC) {
+ uint32_t entries = le32(table + 8);
+
+ for (uint32_t i = 0; i < entries && i < 32; i++) {
+ uint64_t entry = table + 32 + PTABLE_ENTRY * (uint64_t)i;
+ uint32_t part_offset = le32(entry), part_size = le32(entry + 4);
+
+ if (le16(entry + 12) != GLOBAL_HOST || le16(entry + 14) != GLOBAL_HOST || part_size == 0) {
+ continue;
+ }
+ if (!inside(part_offset, part_size) || le32(part_offset) != PART_MAGIC) {
+ say("socinfo: the global partition (offset %#x, size %#x) has no \"$PRT\" header (%#x)\n", part_offset,
+ part_size, inside(part_offset, 4) ? le32(part_offset) : 0);
+ continue;
+ }
+ uint64_t at = part_offset + 32, end = part_offset + le32(part_offset + 16); /* offset_free_uncached */
+
+ if (end > (uint64_t)part_offset + part_size) {
+ end = (uint64_t)part_offset + part_size;
+ }
+ while (at + 16 <= end) {
+ uint32_t size = le32(at + 4), padding_hdr = le16(at + 10), padding_data = le16(at + 8);
+ uint64_t data = at + 16 + padding_hdr;
+
+ if (le16(at) != PRIVATE_CANARY) {
+ say("socinfo: item chain broken at %#llx (canary %#x)\n", (unsigned long long)at, le16(at));
+ break;
+ }
+ if (le16(at + 2) == item && size >= padding_data && inside(data, size - padding_data)) {
+ *offset = data;
+ *length = size - padding_data;
+ *how = "the global partition";
+ return true;
+ }
+ at = data + size;
+ }
+ }
+ }
+ /*
+ * The older layout, which Linux's qcom_smem_get_global() uses for every item that is not in a partition (and the
+ * socinfo is one): a header at the start of the region with a fixed table of 512 items. Not gated on the
+ * firmware's version word: the partitioned layout keeps this table for the items it has not moved.
+ */
+ if (item < LEGACY_ITEMS) {
+ uint64_t entry = LEGACY_TOC_OFFSET + 16 * (uint64_t)item;
+
+ if (le32(entry) != 0 && inside(le32(entry + 4), le32(entry + 8))) {
+ *offset = le32(entry + 4);
+ *length = le32(entry + 8);
+ *how = "the legacy table";
+ return true;
+ }
+ }
+ return false;
+}
+
+/* For a layout that is not what was expected: which items the legacy table has allocated (item number, size). */
+static void
+list_items(void)
+{
+ char line[200];
+ unsigned shown = 0;
+
+ line[0] = 0;
+ for (uint32_t item = 0; item < LEGACY_ITEMS && shown < 24; item++) {
+ uint64_t entry = LEGACY_TOC_OFFSET + 16 * (uint64_t)item;
+ char one[24];
+
+ if (le32(entry) == 0) {
+ continue;
+ }
+ snprintf(one, sizeof(one), " %u:%u@%#x", item, le32(entry + 8), le32(entry + 4));
+ strlcat(line, one, sizeof(line));
+ shown++;
+ }
+ say("socinfo: the legacy table has allocated:%s%s; the partition table has %u entries\n", line[0] ? line : " nothing", shown == 24 ? " ..." : "",
+ le32(smem_size - 0x1000 + 8));
+ for (uint32_t i = 0; i < le32(smem_size - 0x1000 + 8) && i < 12; i++) {
+ uint64_t entry = smem_size - 0x1000 + 32 + PTABLE_ENTRY * (uint64_t)i;
+
+ say("socinfo: partition %u: offset %#x size %#x hosts %#x/%#x, header magic there %#x\n", i, le32(entry), le32(entry + 4),
+ le16(entry + 12), le16(entry + 14), inside(le32(entry), 4) ? le32(le32(entry)) : 0);
+ }
+ say("socinfo: the first words of the region: %08x %08x %08x %08x %08x %08x %08x %08x\n", le32(0), le32(4), le32(8), le32(12),
+ le32(16), le32(20), le32(24), le32(28));
+}
+
+/* ---- what the structure holds ---------------------------------------------------------------------------- */
+
+static struct {
+ bool valid;
+ uint32_t fmt, id, version, raw_id, raw_version, hw_platform, platform_version, hw_subtype, foundry, serial,
+ family, raw_family, raw_num;
+ char build[33];
+} soc;
+
+static const struct { uint32_t id; const char *name; } known[] = {
+ { 415, "SM8350" }, { 439, "SM8350" }, { 457, "SM8450" }, { 475, "SC7280 (SM7325, \"yupik\")" },
+ { 487, "SC7280" }, { 497, "QCM6490" }, { 498, "QCS6490" }, { 483, "SM6350" }, { 434, "SM6350" },
+ { 480, "SM7225" }, { 459, "SM7225" }, { 416, "SM8250" }, { 356, "SM8250" },
+};
+
+static const char *
+soc_name(uint32_t id)
+{
+ for (unsigned i = 0; i < sizeof(known) / sizeof(known[0]); i++) {
+ if (known[i].id == id) {
+ return known[i].name;
+ }
+ }
+ return "an SoC this driver has no name for";
+}
+
+static void
+parse(uint64_t at, uint64_t length)
+{
+ /*
+ * Offsets of the fields in struct socinfo (Linux socinfo.c). The structure grew by appending, so a field is
+ * read only when the item is long enough to hold it; one that is not there stays 0. (Linux does the same:
+ * it tests offsetof() against the item's size, not the format number.)
+ */
+ #define FIELD(offset) (length >= (offset) + 4 ? le32(at + (offset)) : 0)
+
+ soc.fmt = FIELD(0);
+ soc.id = FIELD(4);
+ soc.version = FIELD(8);
+ for (unsigned i = 0; i < 32 && length >= 12 + i + 1; i++) {
+ uint8_t c = b8(at + 12 + i);
+
+ if (c == 0) {
+ break;
+ }
+ soc.build[i] = (c >= 0x20 && c < 0x7f) ? (char)c : '?';
+ }
+ soc.raw_id = FIELD(44);
+ soc.raw_version = FIELD(48);
+ soc.hw_platform = FIELD(52);
+ soc.platform_version = FIELD(56);
+ soc.hw_subtype = FIELD(64);
+ soc.foundry = FIELD(92);
+ soc.serial = FIELD(96);
+ soc.family = FIELD(108);
+ soc.raw_family = FIELD(112);
+ soc.raw_num = FIELD(116);
+ #undef FIELD
+}
+
+static char soc_name_text[64] = "unknown";
+static char soc_serial_text[16] = "";
+static char soc_build_text[40] = "";
+static int soc_id_value = -1;
+static int soc_revision_value = -1;
+static int soc_platform_value = -1;
+static int soc_subtype_value = -1;
+static int soc_foundry_value = -1;
+
+SYSCTL_INT(_kern_hakios, OID_AUTO, soc_id, CTLFLAG_RD | CTLFLAG_LOCKED, &soc_id_value, 0,
+ "the SoC's number in the firmware's table (475: SC7280/SM7325); -1: not read");
+SYSCTL_STRING(_kern_hakios, OID_AUTO, soc_name, CTLFLAG_RD | CTLFLAG_LOCKED, soc_name_text, 0, "the SoC's name, as this kernel knows that number");
+SYSCTL_INT(_kern_hakios, OID_AUTO, soc_revision, CTLFLAG_RD | CTLFLAG_LOCKED, &soc_revision_value, 0,
+ "the chip revision, major << 16 | minor (0x10000 is 1.0)");
+SYSCTL_INT(_kern_hakios, OID_AUTO, soc_platform, CTLFLAG_RD | CTLFLAG_LOCKED, &soc_platform_value, 0, "the board's hardware platform number");
+SYSCTL_INT(_kern_hakios, OID_AUTO, soc_platform_subtype, CTLFLAG_RD | CTLFLAG_LOCKED, &soc_subtype_value, 0, "its subtype");
+SYSCTL_INT(_kern_hakios, OID_AUTO, soc_foundry, CTLFLAG_RD | CTLFLAG_LOCKED, &soc_foundry_value, 0, "the foundry that made the die");
+SYSCTL_STRING(_kern_hakios, OID_AUTO, soc_serial, CTLFLAG_RD | CTLFLAG_LOCKED, soc_serial_text, 0,
+ "the chip's serial number (hex). It is this unit's, so it is not written to the log");
+SYSCTL_STRING(_kern_hakios, OID_AUTO, soc_build, CTLFLAG_RD | CTLFLAG_LOCKED, soc_build_text, 0, "the firmware's build id");
+
+__startup_func
+static void
+socinfo_init(void)
+{
+ uint32_t enabled = 0, pa = (uint32_t)SMEM_DEFAULT_PA, size = (uint32_t)SMEM_DEFAULT_SIZE;
+ uint64_t offset, length;
+ const char *how;
+
+ if (!PE_parse_boot_argn("socinfo", &enabled, sizeof(enabled)) || !enabled) {
+ return;
+ }
+ (void)PE_parse_boot_argn("soc_smem", &pa, sizeof(pa));
+ (void)PE_parse_boot_argn("soc_smem_size", &size, sizeof(size));
+ if (size < 0x2000 || size > 0x1000000) {
+ say("socinfo: soc_smem_size=%#x is not a size SMEM can have; not read\n", size);
+ return;
+ }
+ smem_size = size;
+ smem = ml_io_map(pa, size);
+ if (smem == 0) {
+ say("socinfo: could not map %#x bytes at %#x\n", size, pa);
+ return;
+ }
+ if (!smem_item(SMEM_HW_SW_BUILD_ID, &offset, &length, &how)) {
+ say("socinfo: item %d is not in the shared memory at %#x (partition table magic %#x, legacy version word %#x)\n",
+ SMEM_HW_SW_BUILD_ID, pa, le32(smem_size - 0x1000), le32(4 * 4 * 4 + 4 * LEGACY_SBL_VERSION));
+ list_items();
+ return;
+ }
+ if (length < 56) {
+ say("socinfo: item %d is %llu bytes, too short for a socinfo structure\n", SMEM_HW_SW_BUILD_ID, (unsigned long long)length);
+ return;
+ }
+ parse(offset, length);
+ soc.valid = true;
+ soc_id_value = (int)soc.id;
+ soc_revision_value = (int)soc.version;
+ soc_platform_value = (int)soc.hw_platform;
+ soc_subtype_value = (int)soc.hw_subtype;
+ soc_foundry_value = (int)soc.foundry;
+ strlcpy(soc_name_text, soc_name(soc.id), sizeof(soc_name_text));
+ strlcpy(soc_build_text, soc.build, sizeof(soc_build_text));
+ snprintf(soc_serial_text, sizeof(soc_serial_text), "%08x", soc.serial);
+ say("socinfo: in %s: format %#x, SoC %u = %s, revision %u.%u, board platform %u (version %u, subtype %u), foundry %u,"
+ " %llu bytes, firmware build \"%s\"\n", how, soc.fmt, soc.id, soc_name_text, soc.version >> 16, soc.version & 0xffff,
+ soc.hw_platform, soc.platform_version, soc.hw_subtype, soc.foundry, (unsigned long long)length, soc.build);
+ say("socinfo: raw id %u, raw version %#x; serial number ...%04x (all of it in kern.hakios.soc_serial)\n", soc.raw_id,
+ soc.raw_version, soc.serial & 0xffff);
+}
+STARTUP(EARLY_BOOT, STARTUP_RANK_LAST, socinfo_init);
diff --git a/drivers/map b/drivers/map
index f9231b1..3c81135 100644
--- a/drivers/map
+++ b/drivers/map
@@ -59,3 +59,6 @@ net bsd/dev/hakios.net.c
net bsd/dev/hakios.virtio_net.c
core bsd/dev/hakios.version.c
+core bsd/dev/hakios.socinfo.c
+power bsd/dev/hakios.thermal.c
+power bsd/dev/hakios.pmic.c
diff --git a/drivers/power/hakios.pmic.c b/drivers/power/hakios.pmic.c
new file mode 100644
index 0000000..1849e1c
--- /dev/null
+++ b/drivers/power/hakios.pmic.c
@@ -0,0 +1,304 @@
+/*
+ * What is on the phone's power-management chips, and what time their clock says. Only with boot-arg pmic=1.
+ *
+ * The phone's PMICs (the PMK8350 and PM7325 beside the SoC, with a PM8350C and others on the board) hang on
+ * one serial bus, SPMI, and the SoC reaches them through the "PMIC arbiter" (dt: qcom,spmi@c440000). Each chip
+ * is a set of small blocks ("peripherals", 256 registers each) with a type and a subtype in registers 0x04
+ * and 0x05: a GPIO, a regulator, the power-on block, a temperature alarm, a flash LED driver, a charger, the
+ * real-time clock. Android's drivers find theirs from the device tree. This one asks the arbiter what it has
+ * and what each block says it is, and puts the answer in the log: an inventory, so that the next driver to
+ * be written for one of these blocks starts from what the phone really has instead of from what a table
+ * for another phone says (this phone's device tree has no node for its real-time clock, for instance).
+ *
+ * THIS DRIVER ONLY READS. It reads through the arbiter's "observer" channels exactly as drivers/input/hakios.buttons.c
+ * does, one bus transfer at a time under the same lock (hakios_spmi_lock()), and writes only the command word
+ * that asks the arbiter for a read. Nothing is written to a PMIC: they make the phone's supplies. Where the arbiter
+ * answers "denied" or "failed" for a block, the line says so and the driver goes on.
+ *
+ * Register layout and the version (5) of the arbiter: Linux drivers/spmi/spmi-pmic-arb.c, as drivers/input/hakios.buttons.c
+ * has it and has run it on this phone. A phone whose arbiter is another version gets one line and nothing else.
+ *
+ * The clock. The PMICs have a real-time clock that Android sets, and keeps while the phone is off (Linux:
+ * rtc-pm8xxx.c). When the inventory finds a block of type 0x07 (the clock) it reads four bytes at register 0x48 of
+ * the one with subtype 0x01 -- the seconds since 1970, little-endian, as Linux reads them -- twice, a little over a
+ * second apart, and says whether the second is later than the first (it ticks), how many days that is, and what the
+ * kernel's own time (the loader gives the kernel the host's time in epoch=) minus it comes to. The value is kept as
+ * kern.hakios.rtc_seconds. The kernel's own clock is NOT set from it. (The first run on the phone showed why: the
+ * counter reads 60110539 seconds, 695 days of counting, which as a date is 1971. It is a counter, not wall-clock time; see
+ * the comment at the report.)
+ *
+ * Status (what has and has not been seen): RUN ON THE PHONE (POCO X5 Pro, 2026-10-27, four runs): the arbiter is version 0x50020000
+ * (5.2) with 512 channels, 444 distinct peripherals; slaves 0 to 3 answer (0, 1 and 2 are the PMK8350, PM7325 and PM8350C, as the buttons and flashlight drivers have them), the
+ * peripherals of slaves 4, 8 and 9 answer "failed" (no chip there: 98 of them); the clock is found where Linux has it
+ * (slave 0 0x6100, type 0x07 subtype 0x01) and ticks once a second. The access is the buttons driver's, which is
+ * known to work on this phone; what is new is the walk over all channels and the clock's registers (0x48 for the read), taken from Linux's rtc-pm8xxx.c
+ * as remembered. The first run on the phone (2026-10-27) found the clock's blocks where Linux has them: slave 0 0x6100 is type 0x07
+ * subtype 0x01 (the write block) and 0x6200 type 0x07 subtype 0x03 (the alarm); the type is 0x07, not the 0x06 first assumed. Nothing here has a recorded run on the phone until a
+ * line below says so; it is not run in QEMU, which has no arbiter.
+ *
+ * Boot-args:
+ * pmic=1 do it (once, from a thread of its own)
+ * pmic_list=0 only the clock, not the inventory of every block
+ */
+#include <kern/clock.h>
+#include <kern/locks.h>
+#include <kern/startup.h>
+#include <kern/thread.h>
+#include <kern/thread_call.h>
+#include <libkern/libkern.h>
+#include <mach/mach_types.h>
+#include <pexpert/pexpert.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+
+extern vm_offset_t ml_io_map(vm_offset_t phys_addr, vm_size_t size);
+extern void loader_log_putc(char c);
+extern void hakios_spmi_lock(void);
+extern void hakios_spmi_unlock(void);
+
+SYSCTL_DECL(_kern_hakios);
+
+static int say(const char *format, ...) __printflike(1, 2);
+
+static int
+say(const char *format, ...)
+{
+ char line[400];
+ va_list list;
+
+ va_start(list, format);
+ vsnprintf(line, sizeof(line), format, list);
+ va_end(list);
+ for (const char *c = line; *c != 0; c++) {
+ loader_log_putc(*c);
+ }
+ return printf("%s", line);
+}
+
+static inline uint32_t
+rd(vm_offset_t address)
+{
+ return *(volatile uint32_t *)address;
+}
+
+static inline void
+wr(vm_offset_t address, uint32_t value)
+{
+ *(volatile uint32_t *)address = value;
+}
+
+/* the arbiter, as drivers/input/hakios.buttons.c maps it */
+#define ARB_CORE_PHYS 0x0c440000UL
+#define ARB_CORE_SIZE 0x1100UL
+#define ARB_CNFG_PHYS 0x0c40a000UL
+#define ARB_CNFG_SIZE 0x1000UL
+#define ARB_OBSERVER_PHYS 0x0e600000UL
+#define ARB_OBSERVER_SIZE 0x10000UL
+#define ARB_SYSTEM 0
+#define ARB_VERSION 0x000
+#define ARB_VERSION_5 0x50000000u
+#define ARB_VERSION_7 0x70000000u
+#define ARB_FEATURES 0x004
+#define ARB_CHANNEL(n) (0x900 + 4 * (n)) /* [19:8] slave and peripheral */
+#define ARB_MAX_CHANNELS 512
+#define OBSERVER_COMMAND 0x00
+#define OBSERVER_STATUS 0x08
+#define OBSERVER_DATA0 0x18
+#define OBSERVER_DATA1 0x1c
+#define STATUS_DONE 0x1
+#define STATUS_FAILURE 0x2
+#define STATUS_DENIED 0x4
+#define STATUS_DROPPED 0x8
+#define COMMAND_READ (1u << 27)
+#define READ_TIMEOUT_US 2000
+
+static vm_offset_t arb_core, arb_observer;
+
+/* Read `count` (1 to 8) registers of the peripheral at channel `channel`, from register `reg`; NULL, or what went wrong. */
+static const char *
+spmi_read(uint32_t channel, uint8_t reg, uint8_t *bytes, uint32_t count)
+{
+ vm_offset_t ch = arb_observer + 0x10000UL * ARB_SYSTEM + 0x80UL * channel;
+ uint64_t start = mach_absolute_time(), now, interval;
+ uint32_t status, data[2];
+
+ nanoseconds_to_absolutetime(READ_TIMEOUT_US * (uint64_t)NSEC_PER_USEC, &interval);
+ hakios_spmi_lock();
+ wr(ch + OBSERVER_COMMAND, COMMAND_READ | (uint32_t)reg << 4 | (count - 1));
+ do {
+ status = rd(ch + OBSERVER_STATUS);
+ now = mach_absolute_time();
+ } while (!(status & STATUS_DONE) && now - start < interval);
+ data[0] = rd(ch + OBSERVER_DATA0);
+ data[1] = count > 4 ? rd(ch + OBSERVER_DATA1) : 0;
+ hakios_spmi_unlock();
+
+ if (!(status & STATUS_DONE)) {
+ return "no answer";
+ } else if (status & STATUS_DENIED) {
+ return "denied";
+ } else if (status & STATUS_FAILURE) {
+ return "failed";
+ } else if (status & STATUS_DROPPED) {
+ return "dropped";
+ }
+ for (uint32_t i = 0; i < count; i++) {
+ bytes[i] = (uint8_t)(data[i / 4] >> (8 * (i % 4)));
+ }
+ return NULL;
+}
+
+/* ---- the inventory --------------------------------------------------------------------------------------- */
+
+#define REG_TYPE 0x04
+#define RTC_TYPE 0x07
+#define RTC_SUBTYPE_WRITE 0x01
+#define RTC_REG_READ 0x48
+
+static int64_t rtc_seconds_value = -1;
+static int pmic_peripherals_value;
+SYSCTL_QUAD(_kern_hakios, OID_AUTO, rtc_seconds, CTLFLAG_RD | CTLFLAG_LOCKED, &rtc_seconds_value,
+ "the PMIC real-time clock, seconds since 1970 as it read once at boot; -1: not read");
+SYSCTL_INT(_kern_hakios, OID_AUTO, pmic_peripherals, CTLFLAG_RD | CTLFLAG_LOCKED, &pmic_peripherals_value, 0,
+ "how many blocks of the power-management chips the arbiter showed us");
+
+static uint8_t seen_type[ARB_MAX_CHANNELS][2];
+
+static bool
+read_clock(uint32_t channel, uint32_t *seconds)
+{
+ uint8_t b[4];
+ const char *problem = spmi_read(channel, RTC_REG_READ, b, 4);
+
+ if (problem != NULL) {
+ say("pmic: the clock's seconds: %s\n", problem);
+ return false;
+ }
+ *seconds = b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
+ return true;
+}
+
+static void
+clock_report(uint32_t channel)
+{
+ uint32_t first, second;
+ clock_sec_t kernel_seconds;
+ clock_usec_t kernel_micro;
+
+ if (!read_clock(channel, &first)) {
+ return;
+ }
+ delay_for_interval(1100, NSEC_PER_MSEC);
+ if (!read_clock(channel, &second)) {
+ return;
+ }
+ clock_get_calendar_microtime(&kernel_seconds, &kernel_micro);
+ rtc_seconds_value = second;
+ /*
+ * What this counter is, as the first run on the phone showed: it ticks once a second, and it is NOT wall-clock time (it
+ * read 60110539, which as a date is in 1971). Qualcomm phones keep the clock as a free-running counter in the PMIC, and
+ * Android's time service keeps the offset to real time in a file of its own; the counter's use to this kernel is a
+ * count of seconds that goes on through a reboot and a flat battery's worth of power-off. If the kernel was given the
+ * host's time (epoch=), the difference printed is that offset, which can be compared with the file's.
+ */
+ say("pmic: the real-time clock reads %u, then %u a second and a tenth later (%s); that is %u days of counting, not a date;"
+ " the kernel's time minus it is %lld s\n", first, second, second > first ? "it ticks" : "IT DOES NOT TICK", second / 86400,
+ (long long)kernel_seconds - (long long)second);
+}
+
+static bool list_all = true;
+
+static void
+pmic_run(__unused thread_call_param_t p0, __unused thread_call_param_t p1)
+{
+ bool list = list_all;
+ uint32_t version, channels, found = 0, rtc_channel = 0;
+ bool rtc_found = false;
+ uint16_t ppids[ARB_MAX_CHANNELS];
+ uint16_t channel_of[ARB_MAX_CHANNELS];
+
+ arb_core = ml_io_map(ARB_CORE_PHYS, ARB_CORE_SIZE);
+ arb_observer = ml_io_map(ARB_OBSERVER_PHYS, ARB_OBSERVER_SIZE);
+ if (arb_core == 0 || arb_observer == 0) {
+ say("pmic: could not map the PMIC arbiter\n");
+ goto out;
+ }
+ version = rd(arb_core + ARB_VERSION);
+ channels = rd(arb_core + ARB_FEATURES) & 0x7ff;
+ say("pmic: PMIC arbiter version %#x, %u channels\n", version, channels);
+ if (version < ARB_VERSION_5 || version >= ARB_VERSION_7) {
+ say("pmic: this driver knows version 5 of the arbiter only: nothing read\n");
+ goto out;
+ }
+ if (channels == 0 || channels > ARB_MAX_CHANNELS) {
+ channels = ARB_MAX_CHANNELS;
+ }
+ /* one entry for each distinct slave/peripheral, with the first channel that reaches it */
+ for (uint32_t n = 0; n < channels; n++) {
+ uint32_t map = rd(arb_core + ARB_CHANNEL(n));
+ uint16_t ppid = (uint16_t)((map >> 8) & 0xfff);
+ bool again = false;
+
+ if (map == 0) {
+ continue;
+ }
+ for (uint32_t i = 0; i < found; i++) {
+ if (ppids[i] == ppid) {
+ again = true;
+ break;
+ }
+ }
+ if (!again) {
+ ppids[found] = ppid;
+ channel_of[found] = (uint16_t)n;
+ found++;
+ }
+ }
+ pmic_peripherals_value = (int)found;
+ say("pmic: %u distinct peripherals in the arbiter's table; reading the type of each\n", found);
+ for (uint32_t i = 0; i < found; i++) {
+ uint8_t is[2] = { 0xff, 0xff };
+ const char *problem = spmi_read(channel_of[i], REG_TYPE, is, 2);
+
+ seen_type[i][0] = is[0];
+ seen_type[i][1] = is[1];
+ if (list) {
+ if (problem != NULL) {
+ say("pmic: slave %u 0x%02x00 (channel %u): %s\n", ppids[i] >> 8, ppids[i] & 0xff, channel_of[i], problem);
+ } else {
+ say("pmic: slave %u 0x%02x00 (channel %u): type %#04x subtype %#04x\n", ppids[i] >> 8, ppids[i] & 0xff,
+ channel_of[i], is[0], is[1]);
+ }
+ }
+ if (problem == NULL && is[0] == RTC_TYPE && is[1] == RTC_SUBTYPE_WRITE && !rtc_found) {
+ rtc_found = true;
+ rtc_channel = channel_of[i];
+ say("pmic: the real-time clock is slave %u 0x%02x00 (type %#x, subtype %#x)\n", ppids[i] >> 8, ppids[i] & 0xff,
+ is[0], is[1]);
+ }
+ }
+ if (rtc_found) {
+ clock_report(rtc_channel);
+ } else {
+ say("pmic: none of the %u blocks says it is a real-time clock (type %#x, subtype %#x): none read\n", found, RTC_TYPE,
+ RTC_SUBTYPE_WRITE);
+ }
+out:
+ return;
+}
+
+__startup_func
+static void
+pmic_init(void)
+{
+ uint32_t enabled = 0, list = 1;
+
+ if (!PE_parse_boot_argn("pmic", &enabled, sizeof(enabled)) || !enabled) {
+ return;
+ }
+ (void)PE_parse_boot_argn("pmic_list", &list, sizeof(list));
+ list_all = list != 0;
+ /* once, from a thread call: the reads wait on the bus, and that is no place for the startup thread */
+ thread_call_enter(thread_call_allocate(pmic_run, NULL));
+}
+STARTUP(EARLY_BOOT, STARTUP_RANK_LAST, pmic_init);
diff --git a/drivers/power/hakios.thermal.c b/drivers/power/hakios.thermal.c
new file mode 100644
index 0000000..8d69400
--- /dev/null
+++ b/drivers/power/hakios.thermal.c
@@ -0,0 +1,283 @@
+/*
+ * The SoC's temperature sensors: sysctl kern.hakios.thermal_*, a line in the log, and a stop for a phone that is
+ * getting too hot to be left alone. Only with boot-arg thermal=1.
+ *
+ * What is here. The chip has two blocks of temperature sensors ("TSENS", dt: tsens@c222000 and tsens@c223000,
+ * compatible qcom,tsens26xx, which Linux's tsens-v2.c drives): 15 sensors in the first (the CPU cores, the CPU
+ * subsystem, the always-on block) and 12 in the second (the GPU, the neural processors, video, memory, the
+ * modem, the camera). The blocks measure by themselves, all the time, and keep the last reading of each sensor in
+ * a status word that anyone may read: bits [11:0] the temperature in tenths of a degree, signed, and bit 21 set
+ * when there is a reading. THIS DRIVER ONLY READS those words (one 32-bit load each); it writes nothing, it
+ * takes no interrupt and it sets no threshold. Calibration is the firmware's: the blocks are calibrated from the
+ * fuses by the secure side before the operating system runs (Linux's tsens does it too, from the QFPROM, for its
+ * own use of the raw codes; the status words used here are already degrees).
+ *
+ * What a temperature is worth. A reading is believed when it has its valid bit and lies between -40 and 160
+ * degrees; anything else is a sensor that is not connected, or not yet measured, and is left out of the maximum.
+ * The table of sensors (names, which block, which number, and the temperatures at which the vendor slows
+ * things down and gives up) is the phone's own device tree's thermal-zones, made by tools/dt-thermal.py: the
+ * numbers are the vendor's, not ours.
+ *
+ * What it does with them.
+ * - every thermal_poll milliseconds (default 2000) it reads all sensors;
+ * - sysctl kern.hakios.thermal_max_mc (the hottest, in millidegrees), thermal_hottest (its name),
+ * thermal_sensors ("cpu-0-0=41.2 ... " in degrees), thermal_reads;
+ * - a line in the log (and the loader's log) every thermal_log seconds (default 60) and at once when the
+ * hottest sensor crosses a multiple of 5 degrees upwards from 60;
+ * - the stop: when the hottest valid sensor reads at least thermal_stop degrees three times in a row (default
+ * 105; thermal_stop=0 turns the stop off), it says so and hands control back to the boot loader the way
+ * "xl stop" does (the GPU, Wi-Fi and audio DSP given back first). The vendor starts to slow the CPU at 108 to
+ * 112 degrees; 105 is below that on purpose, because nothing here slows anything down: the stop is the whole
+ * protection, and it is for a phone left running unattended. It is a stop of the run, not a repair: a hot phone
+ * is hot for minutes after.
+ *
+ * Status (what has and has not been seen): RUN ON THE PHONE (POCO X5 Pro, 2026-10-27, four runs): the status-word layout
+ * (from Linux's tsens-v2.c as remembered, the same as drivers/gpu/hakios.adreno.c reads for the GPU's two sensors) is right:
+ * 27 of 27 sensors have a reading, 33.3 to 39.5 degrees on a phone at rest, the CPU cores a degree or two above the rest, the
+ * values move between runs; kern.hakios.thermal_* are read from user space (userland/src/hello.c with hello_wait=). NOT yet
+ * seen: the stop (thermal_stop) firing, a phone under load, a reading above 40 degrees. The sensors are the device tree's; whether
+ * the phone's own tree is the reference one (devices/redwood/prebuilt/device-tree.dtb) was not compared sensor by sensor.
+ * Not run in QEMU, and thermal=1 must not be given there: it has no such blocks, and a load from an address it does not map faults.
+ *
+ * Boot-args:
+ * thermal=1 read the sensors
+ * thermal_poll=<ms> how often (200 to 60000; 2000)
+ * thermal_log=<s> a summary line this often (0: only on a rise; 60)
+ * thermal_stop=<C> stop the run at this temperature (0: never; 105)
+ * thermal_tm0=<pa>, thermal_tm1=<pa> the two blocks' readings, when this is not the reference phone
+ * (0xc263000 and 0xc265000)
+ */
+#include <stdint.h>
+#include <kern/clock.h>
+#include <kern/startup.h>
+#include <kern/thread_call.h>
+#include <libkern/libkern.h>
+#include <mach/mach_types.h>
+#include <pexpert/pexpert.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+
+extern vm_offset_t ml_io_map(vm_offset_t phys_addr, vm_size_t size);
+extern void loader_log_putc(char c);
+extern void loader_return(void);
+extern void hakios_var_save_on_return(void);
+extern void hakios_adreno_stop(void);
+extern void hakios_wifi_stop(void);
+extern void hakios_adsp_stop(void);
+
+SYSCTL_DECL(_kern_hakios);
+
+static int say(const char *format, ...) __printflike(1, 2);
+
+static int
+say(const char *format, ...)
+{
+ char line[700]; /* the line of all the sensors is the long one */
+ va_list list;
+
+ va_start(list, format);
+ vsnprintf(line, sizeof(line), format, list);
+ va_end(list);
+ for (const char *c = line; *c != 0; c++) {
+ loader_log_putc(*c);
+ }
+ return printf("%s", line);
+}
+
+#define TM_SIZE 0x200
+#define TM_STATUS(sensor) (0xa0 + 4 * (sensor)) /* one word a sensor */
+#define TM_VALID (1u << 21)
+#define TEMP_MIN_TENTHS (-400)
+#define TEMP_MAX_TENTHS 1600
+
+/* made by tools/dt-thermal.py from the phone's device tree: name, block, sensor, slow down at, gives up at (millidegrees) */
+static const struct sensor_info {
+ const char * name;
+ uint32_t tm;
+ uint32_t number;
+ int32_t slow_mc;
+ int32_t limit_mc;
+} sensors[] = {
+ { "aoss-0", 0xc263000, 0, 115000, 125000 },
+ { "cpu-0-0", 0xc263000, 1, 110000, 125000 },
+ { "cpu-0-1", 0xc263000, 2, 110000, 125000 },
+ { "cpu-0-2", 0xc263000, 3, 110000, 125000 },
+ { "cpu-0-3", 0xc263000, 4, 110000, 125000 },
+ { "cpuss-0", 0xc263000, 5, 115000, 125000 },
+ { "cpuss-1", 0xc263000, 6, 115000, 125000 },
+ { "cpu-1-0", 0xc263000, 7, 108000, 125000 },
+ { "cpu-1-1", 0xc263000, 8, 108000, 125000 },
+ { "cpu-1-2", 0xc263000, 9, 108000, 125000 },
+ { "cpu-1-3", 0xc263000, 10, 108000, 125000 },
+ { "cpu-1-4", 0xc263000, 11, 108000, 125000 },
+ { "cpu-1-5", 0xc263000, 12, 108000, 125000 },
+ { "cpu-1-6", 0xc263000, 13, 108000, 125000 },
+ { "cpu-1-7", 0xc263000, 14, 108000, 125000 },
+ { "aoss-1", 0xc265000, 0, 115000, 125000 },
+ { "gpuss-0", 0xc265000, 1, 95000, 125000 },
+ { "gpuss-1", 0xc265000, 2, 95000, 125000 },
+ { "nspss-0", 0xc265000, 3, 100000, 125000 },
+ { "nspss-1", 0xc265000, 4, 100000, 125000 },
+ { "video", 0xc265000, 5, 100000, 125000 },
+ { "ddr", 0xc265000, 6, 115000, 125000 },
+ { "mdmss-0", 0xc265000, 7, 95000, 125000 },
+ { "mdmss-1", 0xc265000, 8, 95000, 125000 },
+ { "mdmss-2", 0xc265000, 9, 95000, 125000 },
+ { "mdmss-3", 0xc265000, 10, 95000, 125000 },
+ { "camera-0", 0xc265000, 11, 100000, 125000 },
+};
+#define NSENSORS ((unsigned)(sizeof(sensors) / sizeof(sensors[0])))
+#define REFERENCE_TM0 0xc263000u
+#define REFERENCE_TM1 0xc265000u
+
+static vm_offset_t tm_va[2];
+static uint32_t tm_pa[2] = { REFERENCE_TM0, REFERENCE_TM1 };
+static int32_t tenths[NSENSORS]; /* the last good reading; INT32_MIN: none yet */
+static uint32_t poll_ms = 2000, log_seconds = 60, stop_celsius = 105;
+static uint64_t last_log, reads;
+static int strikes, last_step;
+static bool stopping;
+
+static int thermal_max_mc = -273000;
+static char thermal_hottest[16] = "";
+static char thermal_list[NSENSORS * 16 + 8];
+static int thermal_reads_value;
+
+SYSCTL_INT(_kern_hakios, OID_AUTO, thermal_max_mc, CTLFLAG_RD | CTLFLAG_LOCKED, &thermal_max_mc, 0,
+ "the hottest valid temperature sensor of the SoC, in millidegrees (-273000: none read)");
+SYSCTL_STRING(_kern_hakios, OID_AUTO, thermal_hottest, CTLFLAG_RD | CTLFLAG_LOCKED, thermal_hottest, 0, "the name of that sensor");
+SYSCTL_STRING(_kern_hakios, OID_AUTO, thermal_sensors, CTLFLAG_RD | CTLFLAG_LOCKED, thermal_list, 0,
+ "every sensor and its temperature in degrees: \"cpu-0-0=41.2 gpuss-0=38.0 ...\"");
+SYSCTL_INT(_kern_hakios, OID_AUTO, thermal_reads, CTLFLAG_RD | CTLFLAG_LOCKED, &thermal_reads_value, 0, "how many times the sensors were read");
+
+/* For other drivers (a GPU or modem that wants to back off): the hottest temperature, in millidegrees; -273000: not read. */
+int
+hakios_thermal_max_mc(void)
+{
+ return thermal_max_mc;
+}
+
+static thread_call_t call;
+
+static void
+text_of(char *out, size_t size, int32_t tenth)
+{
+ int whole = tenth / 10, frac = tenth % 10;
+
+ snprintf(out, size, "%s%d.%d", (tenth < 0 && whole == 0) ? "-" : "", whole, frac < 0 ? -frac : frac);
+}
+
+static void
+look(void)
+{
+ int32_t hottest = INT32_MIN;
+ unsigned hot = 0, good = 0;
+ char list[sizeof(thermal_list)], one[16];
+
+ list[0] = 0;
+ for (unsigned i = 0; i < NSENSORS; i++) {
+ uint32_t status = *(volatile uint32_t *)(tm_va[sensors[i].tm == REFERENCE_TM0 ? 0 : 1] + TM_STATUS(sensors[i].number));
+ int32_t value = (int32_t)(status << 20) >> 20;
+
+ if (!(status & TM_VALID) || value < TEMP_MIN_TENTHS || value > TEMP_MAX_TENTHS) {
+ continue;
+ }
+ tenths[i] = value;
+ good++;
+ if (value > hottest) {
+ hottest = value;
+ hot = i;
+ }
+ text_of(one, sizeof(one), value);
+ if (strlen(list) + strlen(sensors[i].name) + strlen(one) + 3 < sizeof(list)) {
+ strlcat(list, sensors[i].name, sizeof(list));
+ strlcat(list, "=", sizeof(list));
+ strlcat(list, one, sizeof(list));
+ strlcat(list, " ", sizeof(list));
+ }
+ }
+ reads++;
+ thermal_reads_value = (int)reads;
+ if (good == 0) {
+ return;
+ }
+ strlcpy(thermal_list, list, sizeof(thermal_list));
+ strlcpy(thermal_hottest, sensors[hot].name, sizeof(thermal_hottest));
+ thermal_max_mc = hottest * 100;
+
+ uint64_t now = mach_absolute_time(), seconds;
+ int step = hottest >= 600 ? hottest / 50 : 0; /* a step each 5 degrees from 60 */
+
+ absolutetime_to_nanoseconds(now - last_log, &seconds);
+ seconds /= NSEC_PER_SEC;
+ if (step > last_step || last_log == 0 || (log_seconds != 0 && seconds >= log_seconds)) {
+ text_of(one, sizeof(one), hottest);
+ say("thermal: %u of %u sensors read; hottest %s at %s C; %s\n", good, NSENSORS, sensors[hot].name, one, thermal_list);
+ last_log = now;
+ }
+ last_step = step;
+
+ if (stop_celsius != 0 && hottest >= (int32_t)stop_celsius * 10) {
+ strikes++;
+ } else {
+ strikes = 0;
+ }
+ if (strikes >= 3 && !stopping) {
+ stopping = true;
+ text_of(one, sizeof(one), hottest);
+ say("\n--- thermal: %s reads %s C, at or above thermal_stop=%u three times in a row: back to the loader ---\n",
+ sensors[hot].name, one, stop_celsius);
+ hakios_var_save_on_return();
+ hakios_adreno_stop();
+ hakios_wifi_stop();
+ hakios_adsp_stop();
+ loader_return();
+ }
+}
+
+static void
+tick(__unused thread_call_param_t p0, __unused thread_call_param_t p1)
+{
+ uint64_t deadline;
+
+ look();
+ clock_interval_to_deadline(poll_ms, NSEC_PER_MSEC, &deadline);
+ thread_call_enter_delayed(call, deadline);
+}
+
+__startup_func
+static void
+thermal_init(void)
+{
+ uint32_t enabled = 0, value;
+
+ if (!PE_parse_boot_argn("thermal", &enabled, sizeof(enabled)) || !enabled) {
+ return;
+ }
+ for (unsigned i = 0; i < NSENSORS; i++) {
+ tenths[i] = INT32_MIN;
+ }
+ (void)PE_parse_boot_argn("thermal_tm0", &tm_pa[0], sizeof(tm_pa[0]));
+ (void)PE_parse_boot_argn("thermal_tm1", &tm_pa[1], sizeof(tm_pa[1]));
+ if (PE_parse_boot_argn("thermal_poll", &value, sizeof(value)) && value >= 200 && value <= 60000) {
+ poll_ms = value;
+ }
+ if (PE_parse_boot_argn("thermal_log", &value, sizeof(value)) && value <= 86400) {
+ log_seconds = value;
+ }
+ if (PE_parse_boot_argn("thermal_stop", &value, sizeof(value)) && value <= 160) {
+ stop_celsius = value;
+ }
+ for (unsigned b = 0; b < 2; b++) {
+ tm_va[b] = ml_io_map(tm_pa[b], TM_SIZE);
+ if (tm_va[b] == 0) {
+ say("thermal: could not map the sensors' block at %#x; off\n", tm_pa[b]);
+ return;
+ }
+ }
+ say("thermal: %u sensors in two blocks (%#x, %#x), every %u ms; stop at %u C%s\n", NSENSORS, tm_pa[0], tm_pa[1], poll_ms,
+ stop_celsius, stop_celsius ? "" : " (off)");
+ call = thread_call_allocate(tick, NULL);
+ tick(NULL, NULL);
+}
+STARTUP(EARLY_BOOT, STARTUP_RANK_LAST, thermal_init);
diff --git a/tools/boot-args.py b/tools/boot-args.py
index 7382e3f..5a6afba 100755
--- a/tools/boot-args.py
+++ b/tools/boot-args.py
@@ -65,6 +65,7 @@ GROUPS = [
("Storage and APFS", r"hakios\.(ufs|ufshc|virtio_blk|sparse|apfs_check)\.|apfs_vfsops\."),
("Network", r"hakios\.(net|virtio_net)\."),
("Haptics and torch", r"hakios\.(haptic|torch)\."),
+ ("Chip, thermal and PMIC", r"hakios\.(socinfo|thermal|pmic)\."),
("Version", r"hakios\.version\."),
("CPU, memory and boot", r"hakios\.cpufreq\.|GenericARMPE|loader_return|\(patch\)"),
("Debugging", r"user_exception_trace"),
diff --git a/tools/dt-thermal.py b/tools/dt-thermal.py
new file mode 100644
index 0000000..756ae5d
--- /dev/null
+++ b/tools/dt-thermal.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python3
+"""The thermal zones of a Qualcomm phone's device tree, as a table: zone, temperature sensor, trip points.
+
+usage: tools/dt-thermal.py DEVICE-TREE.dtb [--c]
+
+drivers/power/hakios.thermal.c reads the SoC's temperature sensors (TSENS) and wants to name each one the
+way the phone's own tree does (cpu-0-0, gpuss-0, ...) and to know the temperatures the vendor chose to act at.
+This reads those out of the tree: for every node under /soc/thermal-zones that has a "thermal-sensors" =
+<&tsensN id>, the zone's name, which TSENS block (by the block's register address) and which of its sensors,
+and the zone's trip points (temperature in millidegrees, type). Only numbers and names are read out; no part
+of the tree is copied. With --c it prints the C table the driver includes.
+"""
+import struct
+import sys
+
+
+def load():
+ import importlib.util
+ import os
+ spec = importlib.util.spec_from_file_location("soc_args", os.path.join(os.path.dirname(os.path.abspath(__file__)), "soc-args.py"))
+ mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(mod)
+ return mod
+
+
+def cells(prop):
+ return list(struct.unpack('>%dI' % (len(prop) // 4), prop[:len(prop) // 4 * 4]))
+
+
+def main():
+ args = [a for a in sys.argv[1:] if not a.startswith("--")]
+ if len(args) != 1:
+ sys.exit(__doc__)
+ mod = load()
+ nodes = mod.nodes_of(open(args[0], "rb").read())
+ phandle_reg = {}
+ for path, props in nodes.items():
+ if "compatible" in props and b"qcom,tsens26xx" in props["compatible"] and "phandle" in props:
+ reg = cells(props["reg"]) # srot base/size, tm base/size
+ phandle_reg[cells(props["phandle"])[0]] = reg[2] if len(reg) >= 4 else reg[0]
+ zones = []
+ for path, props in sorted(nodes.items()):
+ parts = path.split("/")
+ if len(parts) < 3 or parts[-2] != "thermal-zones" or "thermal-sensors" not in props:
+ continue
+ ts = cells(props["thermal-sensors"])
+ if ts[0] not in phandle_reg:
+ continue # a sensor of something else (the PMICs', the modem's)
+ trips = []
+ for tpath, tprops in sorted(nodes.items()):
+ if tpath.startswith(path + "/trips/") and tpath.count("/") == path.count("/") + 2 and "temperature" in tprops:
+ kind = tprops.get("type", b"").rstrip(b"\0").decode()
+ temp = struct.unpack('>i', tprops["temperature"][:4])[0]
+ trips.append((temp, kind))
+ zones.append((parts[-1], phandle_reg[ts[0]], ts[1] if len(ts) > 1 else 0, sorted(trips)))
+ # one row a sensor: the vendor has a "-step" zone (where it starts to slow things down) and a "-usr" zone (where
+ # it gives up) for most; the name is the zone's without that suffix
+ sensors = {}
+ for name, tm, sensor, trips in zones:
+ for suffix in ("-step", "-usr"):
+ if name.endswith(suffix):
+ name = name[:-len(suffix)]
+ row = sensors.setdefault((tm, sensor), {"name": name, "temps": []})
+ row["temps"] += [t for t, _k in trips]
+ if "--c" in sys.argv:
+ print("/* made by tools/dt-thermal.py from the phone's device tree: sensor, the TSENS block it is in (the address of its")
+ print(" * readings), its number there, the temperature the vendor starts to slow down at, and the one it gives up at,")
+ print(" * in millidegrees */")
+ for (tm, sensor), row in sorted(sensors.items(), key=lambda kv: (kv[0][0], kv[0][1])):
+ print(' { "%s", %#x, %u, %d, %d },' % (row["name"], tm, sensor, min(row["temps"]), max(row["temps"])))
+ else:
+ for (tm, sensor), row in sorted(sensors.items(), key=lambda kv: (kv[0][0], kv[0][1])):
+ print("%-14s tm=%#x sensor=%-2d slow down at %d, gives up at %d" % (row["name"], tm, sensor, min(row["temps"]), max(row["temps"])))
+
+
+main()
diff --git a/userland/src/hello.c b/userland/src/hello.c
index 256a276..ad7c8c8 100644
--- a/userland/src/hello.c
+++ b/userland/src/hello.c
@@ -39,6 +39,7 @@ syscall6(long num, long a, long b, long c, long d, long e, long f)
#define SYS_open 5
#define SYS_getpid 20
#define SYS_getuid 24
+#define SYS_select 93
#define SYS_sysctlbyname 274
#define O_RDWR 2
@@ -71,6 +72,63 @@ put_number(int fd, long v)
put(fd, buf + i);
}
+/* "name=value" for a string sysctl, or nothing at all if the kernel has no such name (the driver is off) */
+static void
+show_string(int fd, const char *name)
+{
+ char value[1100];
+ u64 size = sizeof(value);
+
+ if (syscall6(SYS_sysctlbyname, (long)name, length(name), (long)value, (long)&size, 0, 0) == 0) {
+ put(fd, name);
+ put(fd, "=");
+ put(fd, value);
+ put(fd, "\n");
+ }
+}
+
+/* ... for an int sysctl (four bytes) */
+static void
+show_int(int fd, const char *name)
+{
+ int value = 0;
+ u64 size = sizeof(value);
+
+ if (syscall6(SYS_sysctlbyname, (long)name, length(name), (long)&value, (long)&size, 0, 0) == 0) {
+ put(fd, name);
+ put(fd, "=");
+ if (value < 0) {
+ put(fd, "-");
+ value = -value;
+ }
+ put_number(fd, value);
+ put(fd, "\n");
+ }
+}
+
+/* hello_wait=N in the kernel's command line (kern.bootargs): N, or 0 */
+static long
+wait_seconds(void)
+{
+ char args[700];
+ u64 size = sizeof(args);
+ long n = 0;
+
+ if (syscall6(SYS_sysctlbyname, (long)"kern.bootargs", 13, (long)args, (long)&size, 0, 0) != 0) {
+ return 0;
+ }
+ for (char *c = args; *c; c++) {
+ if (c[0] == 'h' && c[1] == 'e' && c[2] == 'l' && c[3] == 'l' && c[4] == 'o' && c[5] == '_' && c[6] == 'w' &&
+ c[7] == 'a' && c[8] == 'i' && c[9] == 't' && c[10] == '=') {
+ for (c += 11; *c >= '0' && *c <= '9'; c++) {
+ n = n * 10 + (*c - '0');
+ }
+ return n;
+ }
+ }
+ return 0;
+}
+
void
start(void)
{
@@ -99,6 +157,26 @@ start(void)
put(fd, "\n");
}
+ /* What the drivers that are on say about the chip, through the same door (a driver that is off has no such name) */
+ show_string(fd, "kern.hakios.soc_name");
+ show_int(fd, "kern.hakios.soc_id");
+ show_int(fd, "kern.hakios.pmic_peripherals");
+ show_string(fd, "kern.hakios.thermal_sensors");
+
+ /*
+ * hello_wait=N: stay N seconds, and say once in five how warm the SoC is. Process 1 is not supposed to exit (doing so
+ * panics the kernel, which ends the test run), so this is also how a run is made to last.
+ */
+ for (long second = 0, wait = wait_seconds(); second < wait; second++) {
+ long tv[2] = { 1, 0 };
+
+ syscall6(SYS_select, 0, 0, 0, 0, (long)tv, 0);
+ if (second % 5 == 4) {
+ show_int(fd, "kern.hakios.thermal_max_mc");
+ show_string(fd, "kern.hakios.thermal_hottest");
+ }
+ }
+
/* Process 1 is not supposed to exit; doing so panics the kernel, which ends the test run. */
syscall3(SYS_exit, 0, 0, 0);
for (;;) {
diff --git a/xnu/hakios.patch b/xnu/hakios.patch
index dfe6dc2..37c44c3 100644
--- a/xnu/hakios.patch
+++ b/xnu/hakios.patch
@@ -62,10 +62,10 @@ index a4d25040f38f..aad72993d139 100644
kern_debug.o_INCFLAGS += -I$(SRCROOT)/osfmk
diff --git a/bsd/conf/files b/bsd/conf/files
-index 44274016a4cf..711845d36f49 100644
+index 44274016a4cf..8412318d3703 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
-@@ -109,6 +109,42 @@ bsd/dev/munge.c optional config_requires_u32_munging
+@@ -109,6 +109,45 @@ bsd/dev/munge.c optional config_requires_u32_munging
bsd/dev/unix_startup.c standard
bsd/dev/monotonic.c optional monotonic
@@ -105,10 +105,13 @@ index 44274016a4cf..711845d36f49 100644
+bsd/dev/hakios.wlfw.c standard
+bsd/dev/hakios.lx.c standard
+bsd/dev/hakios.sta.c standard
++bsd/dev/hakios.pmic.c standard
++bsd/dev/hakios.thermal.c standard
++bsd/dev/hakios.socinfo.c standard
bsd/dev/dev_perfmon.c optional config_perfmon
bsd/libkern/crc32.c standard
-@@ -155,6 +191,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +194,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
bsd/miscfs/mockfs/mockfs_fsnode.c optional mockfs
bsd/miscfs/mockfs/mockfs_vfsops.c optional mockfs
bsd/miscfs/mockfs/mockfs_vnops.c optional mockfs
@@ -123,7 +126,7 @@ index 44274016a4cf..711845d36f49 100644
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +405,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +408,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -131,7 +134,7 @@ index 44274016a4cf..711845d36f49 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +428,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +431,7 @@ bsd/kern/kern_debug.c standard
bsd/kern/kern_symfile.c standard
bsd/kern/kern_descrip.c standard
bsd/kern/kern_guarded.c standard