Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248// |$|*file*DeployerStageTwo.c*| |$|*noclean*|// |$|*artifact*DeployerStageTwo.o*DeployerStageTwo.c*"gcc -ffreestanding -nostdlib -c {artifact0} -o {file}"*|// |$|*artifact*DeployerStageTwo.bin*bin.ld&DeployerStageTwo.o*"ld -flto -T {artifact0} {artifact1} -S -o {file}"*|#include <stdint.h>#include <stddef.h>#include <stdbool.h>
typedef void (*diskread_fn)(void *buffer, uint32_t lba);typedef struct SMAP_entry {
uint32_t BaseL; // base address uint64_t uint32_t BaseH; uint32_t LengthL; // length uint64_t uint32_t LengthH; uint32_t Type; // entry Type uint32_t ACPI; // extended
}__attribute__((packed)) SMAP_entry_t;typedef uint64_t page_table_entry_t;void KernelMain(SMAP_entry_t* e820_buffer, uint16_t e820_entry_count);diskread_fn diskRead;static uint16_t sector_stages_list;// RDI RSI RDX RCXvoid _start(diskread_fn a1, void* e820_buffer, uint16_t e820_entry_count, uint16_t sector_stages_list_){ diskRead = a1; sector_stages_list = sector_stages_list_; KernelMain(e820_buffer, e820_entry_count);}
// --- Constants ---#define PAGE_PRESENT (1 << 0)#define PAGE_WRITE (1 << 1)#define PAGE_USER (1 << 2)#define PAGE_SIZE_2MB (1 << 7)#define ALIGN_4K __attribute__((aligned(4096)))#define PHYS_MEM_OFFSET 0xFFFF800000000000#define BOOTSTRAP_MEM_END 0x200000 // 2MiB
// --- Hardcoded Page Tables ---page_table_entry_t *pml4 = (page_table_entry_t*)0x1000;page_table_entry_t *pdp = (page_table_entry_t*)0x2000;page_table_entry_t *pd = (page_table_entry_t*)0x3000;
volatile static uint64_t *debug = (uint64_t*)0x7200;volatile static uint64_t *trace = (uint64_t*)0x7208;volatile static uint64_t *calls = (uint64_t*)0x7210;
/*void add_block(void* addr, size_t size) { free_block_t* block = (free_block_t*)addr; block->size = size; free_block_t* curr = free_list; while(curr && curr->next) { curr = curr->next; if ((uintptr_t)curr->next > (uintptr_t)block) goto ok; } *debug = 0xEEEE0003; while(1){} ok: block->next = curr->next; curr->next = block;}*/
static inline void reload_cr3(uint64_t pml4_phys_addr) { asm volatile( "mov %0, %%cr3" : : "r"(pml4_phys_addr) : "memory" );}
typedef struct sec_stages_list { uint32_t deployer_2_lba; uint32_t deployer_2_seccount; void* deployer_2_addr; uint32_t kernel_lba; uint32_t kernel_seccount;} sec_stages_list_t;
void* bump_alloc = (void*)0x200000;void* bump_top = (void*)0x400000;
void* alloc_4k_aligned(){ if ((uint64_t)bump_alloc % 4096 != 0) { bump_alloc += 4096 - ((uint64_t)bump_alloc % 4096); } void* addr = bump_alloc; bump_alloc += 4096; if (bump_alloc >= bump_top) { *debug = 0xDEAD0005; // Out of memory while(1){} } return addr;}
// --- Paging ---void map_2mib_page(uint64_t phys_addr, uint64_t virt_addr) { uint64_t pml4_idx = (virt_addr >> 39) & 0x1FF; uint64_t pdp_idx = (virt_addr >> 30) & 0x1FF; uint64_t pd_idx = (virt_addr >> 21) & 0x1FF;
// Allocate PDP if not present if (!(pml4[pml4_idx] & PAGE_PRESENT)) { page_table_entry_t* new_pdp = alloc_4k_aligned(); for (int i = 0; i < 512; i++) new_pdp[i] = 0; pml4[pml4_idx] = (uint64_t)new_pdp | PAGE_PRESENT | PAGE_WRITE; } page_table_entry_t* pdp_ptr = (page_table_entry_t*)(pml4[pml4_idx] & ~0xFFF);
// Allocate PD if not present if (!(pdp_ptr[pdp_idx] & PAGE_PRESENT)) { page_table_entry_t* new_pd = alloc_4k_aligned(); for (int i = 0; i < 512; i++) new_pd[i] = 0; pdp_ptr[pdp_idx] = (uint64_t)new_pd | PAGE_PRESENT | PAGE_WRITE; } page_table_entry_t* pd_ptr = (page_table_entry_t*)(pdp_ptr[pdp_idx] & ~0xFFF);
// Map the 2 MiB page in the PD entry // Ensure phys_addr is 2 MiB aligned (lower 21 bits must be zero) phys_addr &= ~((1ULL << 21) - 1); pd_ptr[pd_idx] = phys_addr | PAGE_PRESENT | PAGE_WRITE | PAGE_SIZE_2MB;}
typedef struct memory_block { void* addr; size_t size; int usage; // 1 -> free} __attribute__((packed)) memory_block_t;
typedef void (*kernel_entry_t)(SMAP_entry_t* e820_buffer_, uint16_t e820_entry_count_, diskread_fn diskRead_, uint16_t sector_stages_list_, int mem_quotum_x_, void *bump_alloc_, void *bump_top_ );
// --- Kernel Main ---void KernelMain(SMAP_entry_t* e820_buffer, uint16_t e820_entry_count) { // Save the E820 map for later use (e.g., memory management) SMAP_entry_t e820_rescue[e820_entry_count]; for (int i = 0; i < e820_entry_count; i++) { e820_rescue[i] = e820_buffer[i]; }
// Zero out all page table structures for (int i = 0; i < 512; i++) { pml4[i] = 0; pdp[i] = 0; pd[i] = 0; }
pml4[0] = (uint64_t)pdp | PAGE_PRESENT | PAGE_WRITE; pdp[0] = (uint64_t)pd | PAGE_PRESENT | PAGE_WRITE; pd[0] = 0 | PAGE_PRESENT | PAGE_WRITE | PAGE_SIZE_2MB; // Map first 2MiB of physical memory to virtual memory
// Reload CR3 to activate the new page tables reload_cr3((uint64_t)pml4);
uint16_t memaccess[256];
diskRead(&memaccess, sector_stages_list);
int count_blocks_kernel = ((sec_stages_list_t*)memaccess)->kernel_seccount; int lba_kernel = ((sec_stages_list_t*)memaccess)->kernel_lba;
if (count_blocks_kernel % 4096 != 0) { count_blocks_kernel += 4096 - (count_blocks_kernel % 4096); } count_blocks_kernel /= 4096;
*trace = count_blocks_kernel;
for (int i = 0; i < e820_entry_count; i++) { if (e820_rescue[i].Type != 1) continue; // Only consider usable memory uint64_t base = ((uint64_t)e820_rescue[i].BaseH << 32) | e820_rescue[i].BaseL; uint64_t length = ((uint64_t)e820_rescue[i].LengthH << 32) | e820_rescue[i].LengthL; if (base % 0x200000 != 0) { base += 0x200000 - (base % 0x200000); length -= 0x200000 - (length % 0x200000); } if (length % 0x200000 != 0) { length -= length % 0x200000; } if (length < 0x200000) continue; // Skip regions smaller than 2MiB for (uint64_t addr = base; addr < base + length; addr += 0x200000) { if (addr == 0) goto skip_a; pd[1] = base | PAGE_PRESENT | PAGE_WRITE | PAGE_SIZE_2MB; // Map the first 2MiB of this region goto success; } skip_a: continue; } *debug = 0xE03F0003; // No usable memory region found while(1){} success: int quotum = count_blocks_kernel; void* mmap = (void*)0x400000; for (int i = 0; i < e820_entry_count; i++) { if (e820_rescue[i].Type != 1) continue; // Only consider usable memory uint64_t base = ((uint64_t)e820_rescue[i].BaseH << 32) | e820_rescue[i].BaseL; uint64_t length = ((uint64_t)e820_rescue[i].LengthH << 32) | e820_rescue[i].LengthL; if (base % 0x200000 != 0) { base += 0x200000 - (base % 0x200000); length -= 0x200000 - (length % 0x200000); } if (length % 0x200000 != 0) { length -= length % 0x200000; } if (length < 0x200000) continue; // Skip regions smaller than 2MiB for (uint64_t addr = base; addr < base + length; addr += 0x200000) { if (addr < 0x200000) continue; // Skip the first 2MiB, already identity mapped if (addr == pd[1] & ~0xFFF) continue; // Skip the region already mapped for the deployer //pd[1] = base | PAGE_PRESENT | PAGE_WRITE | PAGE_SIZE_2MB; // Map the first 2MiB of this region map_2mib_page(addr, (uint64_t)mmap); mmap += 0x200000; quotum--; if (quotum == 0) goto success_2; } skip_b: continue; } *debug = 0xE03F0004; // No usable memory region found while(1){} success_2: sec_stages_list_t *stages = (sec_stages_list_t*)memaccess; diskRead(memaccess, sector_stages_list); *debug = 0x1337cafe; uint16_t *kernel_location = (uint16_t *)0x400000; if ((uint64_t)kernel_location == 0) { *debug = 0xDEAD0004; while(1){} } for (uint32_t i = 0; i < stages->kernel_seccount; i++) { diskRead((void*)(kernel_location + (i * 256)), stages->kernel_lba + i); } kernel_entry_t kernel_entry = (kernel_entry_t)((uint64_t)kernel_location); kernel_entry(e820_rescue, e820_entry_count, diskRead, sector_stages_list, count_blocks_kernel, bump_alloc, bump_top); *debug = 0xF09E9999; while(1){}}