-
Notifications
You must be signed in to change notification settings - Fork 1
Development #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Development #72
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "ELFloader.h" | ||
#include "MemOps.h" | ||
#include "VFS.h" | ||
#include "Process.h" | ||
#include "KernelHeap.h" | ||
|
||
void* CreateProcessFromElf(const char* filename) { | ||
// 1. Read ELF file from VFS | ||
uint8_t* elf_data = (uint8_t*)KernelMemoryAlloc(65536); // Max 64KB for simple ELFs | ||
if (!VfsReadFile(filename, (char*)elf_data, 65536)) { | ||
KernelFree(elf_data); | ||
return NULL; | ||
} | ||
|
||
// 2. Validate ELF header | ||
ElfHeader* header = (ElfHeader*)elf_data; | ||
if (*(uint32_t*)header->e_ident != ELF_MAGIC || | ||
header->e_machine != EM_X86_64) { | ||
KernelFree(elf_data); | ||
return NULL; | ||
} | ||
|
||
// 3. Find PT_LOAD segments and allocate memory | ||
ProgramHeader* ph = (ProgramHeader*)(elf_data + header->e_phoff); | ||
void* process_memory = NULL; | ||
uint64_t entry_point = header->e_entry; | ||
|
||
for (int i = 0; i < header->e_phnum; i++) { | ||
if (ph[i].p_type == PT_LOAD) { | ||
// Allocate memory for this segment | ||
void* segment_mem = KernelMemoryAlloc(ph[i].p_memsz); | ||
|
||
// Copy file data to memory | ||
FastMemcpy(segment_mem, elf_data + ph[i].p_offset, ph[i].p_filesz); | ||
|
||
// Zero out BSS section (if memsz > filesz) | ||
if (ph[i].p_memsz > ph[i].p_filesz) { | ||
memset((uint8_t*)segment_mem + ph[i].p_filesz, 0, | ||
ph[i].p_memsz - ph[i].p_filesz); | ||
} | ||
|
||
// For simplicity, use first LOAD segment as process memory | ||
if (!process_memory) { | ||
process_memory = segment_mem; | ||
} | ||
} | ||
} | ||
|
||
// 4. Create kernel process | ||
if (process_memory) { | ||
// Create process with ELF entry point | ||
void* process = (void*)CreateProcess((void*)entry_point); | ||
KernelFree(elf_data); | ||
return process; | ||
} | ||
|
||
KernelFree(elf_data); | ||
return NULL; | ||
} | ||
|
||
// Convenience function | ||
int LoadElfFromFile(const char* filename) { | ||
const void * process = CreateProcessFromElf(filename); | ||
return process ? 0 : -1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef VOIDFRAME_ELFLOADER_H | ||
#define VOIDFRAME_ELFLOADER_H | ||
|
||
#pragma once | ||
#include "stdint.h" | ||
|
||
typedef struct { | ||
uint8_t e_ident[16]; // Magic number and class info | ||
uint16_t e_type; // Object file type | ||
uint16_t e_machine; // Architecture | ||
uint32_t e_version; // Object file version | ||
uint64_t e_entry; // Entry point virtual address | ||
uint64_t e_phoff; // Program header table offset | ||
uint64_t e_shoff; // Section header table offset | ||
uint32_t e_flags; // Processor-specific flags | ||
uint16_t e_ehsize; // ELF header size | ||
uint16_t e_phentsize; // Program header table entry size | ||
uint16_t e_phnum; // Program header table entry count | ||
} ElfHeader; | ||
|
||
// Program Header | ||
typedef struct { | ||
uint32_t p_type; // Segment type (PT_LOAD, etc.) | ||
uint32_t p_flags; // Segment flags | ||
uint64_t p_offset; // Segment file offset | ||
uint64_t p_vaddr; // Segment virtual address | ||
uint64_t p_paddr; // Segment physical address | ||
uint64_t p_filesz; // Segment size in file | ||
uint64_t p_memsz; // Segment size in memory | ||
uint64_t p_align; // Segment alignment | ||
} ProgramHeader; | ||
|
||
#define ELF_MAGIC 0x464C457F // "\x7FELF" | ||
#define PT_LOAD 1 | ||
#define EM_X86_64 62 | ||
|
||
// Main functions | ||
int LoadElfFromFile(const char* filename); | ||
void* CreateProcessFromElf(const char* filename); | ||
|
||
#endif // VOIDFRAME_ELFLOADER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#ifndef EDITOR_H | ||
#define EDITOR_H | ||
|
||
void EditorOpen(const char* file); | ||
void EditorOpen(const char *file); | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify SMAP instructions are conditionally executed.
The
stac
andclac
instructions will cause an invalid opcode exception if SMAP is not supported by the CPU. The kernel should check for SMAP support before executing these instructions.Also applies to: 33-34
🏁 Script executed:
Length of output: 3294
To verify whether SMAP support is properly guarded around the
stac
/clac
in the syscall entry, let’s locate and inspect the assembly snippet directly:🏁 Script executed:
Length of output: 1642
Guard SMAP instructions with a CPU‐feature check
We currently execute
stac
(line 25) andclac
(line 34) unconditionally inarch/x86_64/syscall/SyscallEntry.asm. On CPUs without SMAP, these will #UD.
Please add a CPUID-leaf-7 EBX[21] test (or use the kernel’s global SMAP flag) before each SMAP instruction:
• Wrap lines 24–25 (
stac
) and 33–34 (clac
) in a runtime feature check• Cache the result at boot (e.g. in a per-CPU or global variable) to avoid repeated CPUID calls
For example:
This ensures
stac
/clac
only execute on CPUs that actually support SMAP.📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what, check static void SetupMemoryProtection(void) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
@assembler-0, let me check for that function to see if there's already existing SMAP detection logic.
🏁 Script executed:
Length of output: 6200
@assembler-0, you're absolutely right! I found the
SetupMemoryProtection()
function inkernel/core/Kernel.c
. It already performs the CPUID leaf 7 check for SMAP support (bit 20 in EBX) and enables CR4.SMAP if supported.However, the issue remains that while SMAP is being detected and conditionally enabled at boot time, the syscall entry assembly still executes
stac
andclac
unconditionally. Even thoughSetupMemoryProtection()
won't enable SMAP on unsupported CPUs, the assembly instructions will still cause #UD.The cleanest solution would be to:
SetupMemoryProtection()
functionFor example, in
kernel/core/Kernel.c
:Then in
SyscallEntry.asm
, check this flag instead of doing CPUID each time. This leverages your existing detection logic without duplication.🧠 Learnings used