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: battguard -- end a run nobody watches before the battery is flat (battery_stop=<percent>)

shimokuf57342f316f6
diff --git a/configs/boot-args.lst b/configs/boot-args.lst
index e47c701..7407d6d 100644
--- a/configs/boot-args.lst
+++ b/configs/boot-args.lst
@@ -54,6 +54,8 @@ applog_name k
autounlock u
battery kl
battery_shared_pa k
+battery_stop k
+battery_stop_poll k
battmgr k
bootcpu kl
buttons k
diff --git a/configs/common_defconfig b/configs/common_defconfig
index e811039..5eb98ef 100644
--- a/configs/common_defconfig
+++ b/configs/common_defconfig
@@ -2,7 +2,7 @@
# HakiOS common defconfig: every setting there is, with what it does.
# Made by tools/hakios-config.py listing common; the values are the ones that file sets, and tests/CI check that it names every symbol.
#
-# 311 symbols in 17 groups; 9 are set.
+# 313 symbols in 17 groups; 9 are set.
#
#
@@ -455,7 +455,7 @@ CONFIG_GPU=y
# CONFIG_VBLUR is not set
#
-# Audio and battery (30)
+# Audio and battery (32)
#
# the audio DSP; the battery level in the UI and charging state come through it [needs fw-audio/ from the phone]
@@ -467,6 +467,12 @@ CONFIG_SOUND=4
# the microphone (4 is what the README's phone line uses) [needs fw-audio/ from the phone] [requires CONFIG_ADSP]
# CONFIG_MIC is not set
+# hand control back to the loader when the battery is at or below this percent three looks in a row (0: never); acts ...
+# CONFIG_BATTERY_STOP is not set
+
+# how often the battery guard looks, in seconds (5 to 600; 20) [requires CONFIG_BATTERY_STOP]
+# CONFIG_BATTERY_STOP_POLL is not set
+
# read by hakios.adsp.c (kernel)
# CONFIG_ADSP_AGAIN is not set
diff --git a/docs/boot-args.md b/docs/boot-args.md
index 0cb7d72..d6cc152 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. **314 arguments**: 265 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. **316 arguments**: 267 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.
@@ -180,7 +180,7 @@ Layers: **K** the kernel, **L** the loader, **U** user space (through `sysctl ke
| `soc_pas_mpss` | K | `hakios.mpss.c` | `CONFIG_SOC_PAS_MPSS` | |
| `soc_smem` | K | `hakios.mpss.c`, `hakios.socinfo.c` | `CONFIG_SOC_SMEM` | |
-## Audio and battery (31)
+## Audio and battery (33)
| Argument | Layer | Read by | Configuration | Set by |
|---|---|---|---|---|
@@ -196,6 +196,8 @@ Layers: **K** the kernel, **L** the loader, **U** user space (through `sysctl ke
| `adsp_wait` | K | `hakios.adsp.c` | `CONFIG_ADSP_WAIT` | |
| `battery` | KL | `hakios.adsp.c`, `hakios.battery.c`, `loader.c` | `CONFIG_BATTERY` | |
| `battery_shared_pa` | K | `hakios.battery.c` | | the loader (the address of its charger page) |
+| `battery_stop` | K | `hakios.battguard.c` | `CONFIG_BATTERY_STOP` | |
+| `battery_stop_poll` | K | `hakios.battguard.c` | `CONFIG_BATTERY_STOP_POLL` | |
| `battmgr` | K | `hakios.adsp.c` | `CONFIG_BATTMGR` | |
| `mic` | K | `hakios.mic.c` | `CONFIG_MIC` | |
| `mic_adc` | K | `hakios.mic.c` | `CONFIG_MIC_ADC` | |
diff --git a/drivers/map b/drivers/map
index 3c81135..7ef9d03 100644
--- a/drivers/map
+++ b/drivers/map
@@ -62,3 +62,4 @@ core bsd/dev/hakios.version.c
core bsd/dev/hakios.socinfo.c
power bsd/dev/hakios.thermal.c
power bsd/dev/hakios.pmic.c
+power bsd/dev/hakios.battguard.c
diff --git a/drivers/power/hakios.battguard.c b/drivers/power/hakios.battguard.c
new file mode 100644
index 0000000..bc8a438
--- /dev/null
+++ b/drivers/power/hakios.battguard.c
@@ -0,0 +1,119 @@
+/*
+ * A run nobody watches must not empty the battery: with boot-arg battery_stop=<percent>, hand control back to the boot loader
+ * when the charge is at or below that for three looks in a row, the way drivers/power/hakios.thermal.c does for heat.
+ *
+ * Why. The phone is not charged while HakiOS runs (the kernel has no charging path yet: docs/install-safe.md), and a session
+ * left going overnight ends with a flat battery, which is a phone that has gone off USB until somebody plugs it in. This is the
+ * protection that costs nothing: at 15 percent the run ends, the loader has the log, and (xl with XL_REBOOT_AFTER=1) the phone
+ * goes back to Android, which charges it.
+ *
+ * What it believes. It asks the battery driver's own numbers (sysctl kern.hakios.battery_soc, ..._present, ..._charger_present;
+ * they come from the loader's charger page or, with adsp=1, from the audio DSP's battery service) and acts only on a reading that is
+ * between 0 and 100 with a battery present and NO charger attached. An unknown charge (-1), no battery, or a charger on the cable
+ * is never a reason to stop. It does not read any hardware itself.
+ *
+ * Status (what has and has not been seen): COMPILED ONLY. It was written while the phone was off USB, so it has not run on one;
+ * whether the numbers it reads are filled in when a kernel runs without adsp=1 is not known (the battery driver's header says
+ * where they come from; the first run that sets battery_stop will print its "battguard:" line and say). Not run in QEMU.
+ *
+ * Boot-args:
+ * battery_stop=<percent> stop at this charge or below (0: never, the default; 1 to 99)
+ * battery_stop_poll=<s> how often to look (5 to 600; 20)
+ */
+#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 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);
+extern int kernel_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
+
+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);
+}
+
+static thread_call_t call;
+static uint32_t stop_percent, poll_seconds = 20, looks, strikes, last_said_minute = ~0u;
+static bool stopping;
+
+static bool
+read_int(const char *name, int *value)
+{
+ size_t size = sizeof(*value);
+
+ return kernel_sysctlbyname(name, value, &size, NULL, 0) == 0 && size == sizeof(*value);
+}
+
+static void
+tick(__unused thread_call_param_t p0, __unused thread_call_param_t p1)
+{
+ int soc = -1, present = 0, charger = 0;
+ uint64_t deadline;
+ bool known = read_int("kern.hakios.battery_soc", &soc) && read_int("kern.hakios.battery_present", &present) &&
+ read_int("kern.hakios.battery_charger_present", &charger) && soc >= 0 && soc <= 100 && present && !charger;
+ uint32_t minute = (uint32_t)(++looks * poll_seconds / 300); /* a line every five minutes, and the first */
+
+ if (minute != last_said_minute || looks == 1) {
+ last_said_minute = minute;
+ say("battguard: charge %d%% (battery %s, charger %s); stop at or below %u%%%s\n", soc, present ? "present" : "not reported",
+ charger ? "attached: the guard waits" : "not attached", stop_percent, known ? "" : " -- not a reading it acts on");
+ }
+ strikes = (known && (uint32_t)soc <= stop_percent) ? strikes + 1 : 0;
+ if (strikes >= 3 && !stopping) {
+ stopping = true;
+ say("\n--- battguard: the charge is %d%%, at or below battery_stop=%u three looks in a row: back to the loader ---\n", soc,
+ stop_percent);
+ hakios_var_save_on_return();
+ hakios_adreno_stop();
+ hakios_wifi_stop();
+ hakios_adsp_stop();
+ loader_return();
+ }
+ clock_interval_to_deadline(poll_seconds, NSEC_PER_SEC, &deadline);
+ thread_call_enter_delayed(call, deadline);
+}
+
+__startup_func
+static void
+battguard_init(void)
+{
+ uint32_t value;
+
+ if (!PE_parse_boot_argn("battery_stop", &stop_percent, sizeof(stop_percent)) || stop_percent == 0 || stop_percent > 99) {
+ return;
+ }
+ if (PE_parse_boot_argn("battery_stop_poll", &value, sizeof(value)) && value >= 5 && value <= 600) {
+ poll_seconds = value;
+ }
+ say("battguard: on, looking every %u s; the run ends at %u%% or less (and only with a battery, a known charge and no charger)\n",
+ poll_seconds, stop_percent);
+ uint64_t deadline;
+
+ call = thread_call_allocate(tick, NULL);
+
+ clock_interval_to_deadline(poll_seconds, NSEC_PER_SEC, &deadline);
+ thread_call_enter_delayed(call, deadline);
+}
+STARTUP(EARLY_BOOT, STARTUP_RANK_LAST, battguard_init);
diff --git a/tools/boot-args.py b/tools/boot-args.py
index 69ca561..3b7906e 100755
--- a/tools/boot-args.py
+++ b/tools/boot-args.py
@@ -62,7 +62,7 @@ GROUPS = [
("Security stand-ins", r"kern_minimac|miniamfi"),
("Wi-Fi", r"hakios\.(wifi|wlfw|lx|sta|apple80211)\.|^lx_[a-z0-9]+\.c$"), # wifi/lx/src/lx_*.c: the Linux layer
("Cellular", r"hakios\.(mpss|ipa|cell|nas|qmi)\."),
- ("Audio and battery", r"hakios\.(adsp|audio|sound|speaker|mic|virtio_sound|battery)\."),
+ ("Audio and battery", r"hakios\.(adsp|audio|sound|speaker|mic|virtio_sound|battery|battguard)\."),
("Cameras", r"hakios\.camera\."),
("Touch and buttons", r"hakios\.(goodix|input|buttons|virtio_input)\."),
("Storage and APFS", r"hakios\.(ufs|ufshc|virtio_blk|sparse|apfs_check)\.|apfs_vfsops\."),
diff --git a/tools/hakios-config.py b/tools/hakios-config.py
index 5f6d867..e64007c 100755
--- a/tools/hakios-config.py
+++ b/tools/hakios-config.py
@@ -72,6 +72,9 @@ SYMBOLS = [
("THERMAL_POLL", "int", "thermal_poll", "how often the sensors are read, in milliseconds (200 to 60000; 2000)", None, "THERMAL"),
("THERMAL_STOP", "int", "thermal_stop", "hand control back to the loader when the hottest sensor reads this many degrees three times in a row "
"(0: never; 105): the protection of a phone left running unattended", None, "THERMAL"),
+ ("BATTERY_STOP", "int", "battery_stop", "hand control back to the loader when the battery is at or below this percent three looks in a row (0: never); "
+ "acts only on a known charge, with a battery and no charger: for a run nobody watches", None, None),
+ ("BATTERY_STOP_POLL", "int", "battery_stop_poll", "how often the battery guard looks, in seconds (5 to 600; 20)", None, "BATTERY_STOP"),
("PMIC", "bool", "pmic", "list every block of the power-management chips the SPMI arbiter shows, and read the PMIC's clock; read only", None, None),
# Linux's CONFIG_CMDLINE: whatever the symbols above do not cover, as name=value words in quotes. It is not an escape
# from the checks: every name must be one the source reads (configs/boot-args.lst), so a typo is an error, not a no-op.
@@ -88,7 +91,7 @@ GROUP_OF = {
"WIFI": "Wi-Fi", "HAPTIC": "Haptics and torch", "CAM": "Cameras",
"MODEM": "Cellular", "MODEM_AGAIN": "Cellular", "MODEM_BUFFER": "Cellular",
"NET_TRACE": "Network", "NET_NAMES": "Network", "VIRTIO_NET": "Network",
- "SOCINFO": "Chip, thermal and PMIC", "THERMAL": "Chip, thermal and PMIC", "THERMAL_POLL": "Chip, thermal and PMIC",
+ "BATTERY_STOP": "Audio and battery", "BATTERY_STOP_POLL": "Audio and battery", "SOCINFO": "Chip, thermal and PMIC", "THERMAL": "Chip, thermal and PMIC", "THERMAL_POLL": "Chip, thermal and PMIC",
"THERMAL_STOP": "Chip, thermal and PMIC", "PMIC": "Chip, thermal and PMIC", "BOOTARGS": "Extra arguments",
}
LAYER_OF = {}
@@ -120,7 +123,7 @@ GENERATED = load_generated()
SYMBOLS += GENERATED
BY_NAME = {s[0]: s for s in SYMBOLS}
FIRMWARE_VALUES = ("device", "none")
-RANGES = {"CPUS": (1, 8), "BOOTCPU": (0, 7), "UFS": (1, 2), "THERMAL_POLL": (200, 60000), "THERMAL_STOP": (0, 160)}
+RANGES = {"CPUS": (1, 8), "BOOTCPU": (0, 7), "UFS": (1, 2), "THERMAL_POLL": (200, 60000), "THERMAL_STOP": (0, 160), "BATTERY_STOP": (0, 99), "BATTERY_STOP_POLL": (5, 600)}
class ConfigError(Exception):
diff --git a/xnu/hakios.patch b/xnu/hakios.patch
index 37c44c3..ef9c3a8 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..8412318d3703 100644
+index 44274016a4cf..0897299f8fa2 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
-@@ -109,6 +109,45 @@ bsd/dev/munge.c optional config_requires_u32_munging
+@@ -109,6 +109,46 @@ bsd/dev/munge.c optional config_requires_u32_munging
bsd/dev/unix_startup.c standard
bsd/dev/monotonic.c optional monotonic
@@ -105,13 +105,14 @@ index 44274016a4cf..8412318d3703 100644
+bsd/dev/hakios.wlfw.c standard
+bsd/dev/hakios.lx.c standard
+bsd/dev/hakios.sta.c standard
++bsd/dev/hakios.battguard.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 +194,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +195,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
@@ -126,7 +127,7 @@ index 44274016a4cf..8412318d3703 100644
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +408,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +409,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -134,7 +135,7 @@ index 44274016a4cf..8412318d3703 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +431,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +432,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