Skip to content

Commit 361d5e2

Browse files
Merge pull request #31 from Frost-Wing/codex/fix-invalid-opcode-in-busybox
Skip ENDBR64 in invalid-op exceptions, add SS to InterruptFrame, and show ELF load progress
2 parents ad5763d + 6e4e9ed commit 361d5e2

4 files changed

Lines changed: 2 additions & 158 deletions

File tree

source/includes/isr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ void registerInterruptHandler(uint8_t irq, irq_handler handler);
4141

4242
void rtl8139_handler(InterruptFrame* frame);
4343

44-
#endif
44+
#endif

source/kernel/C/executables/elf.c

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -131,132 +131,6 @@ static int elf_load_tls_template_from_vfs(const char* path, elf_image_info_t* in
131131
return 0;
132132
}
133133

134-
typedef uint64_t (*elf_ifunc_resolver_t)(void);
135-
136-
static int elf_apply_relocation_entries(const Elf64_Rela* relocs, uint64_t reloc_count)
137-
{
138-
if (!relocs || reloc_count == 0)
139-
return 0;
140-
141-
for (uint64_t i = 0; i < reloc_count; ++i) {
142-
const Elf64_Rela* reloc = &relocs[i];
143-
uint32_t reloc_type = ELF64_R_TYPE(reloc->r_info);
144-
uint64_t* target = (uint64_t*)reloc->r_offset;
145-
146-
if (!target) {
147-
eprintf("elf: invalid relocation target");
148-
return -1;
149-
}
150-
151-
switch (reloc_type) {
152-
case R_X86_64_RELATIVE:
153-
*target = (uint64_t)reloc->r_addend;
154-
break;
155-
156-
case R_X86_64_IRELATIVE: {
157-
elf_ifunc_resolver_t resolver = (elf_ifunc_resolver_t)(uint64_t)reloc->r_addend;
158-
*target = resolver ? resolver() : 0;
159-
break;
160-
}
161-
162-
default:
163-
break;
164-
}
165-
}
166-
167-
return 0;
168-
}
169-
170-
static int elf_apply_relocations_from_memory(void* file_base_address, uint64_t file_size, const Elf64_Ehdr* header)
171-
{
172-
if (!file_base_address || !header || header->e_shoff == 0 || header->e_shnum == 0)
173-
return 0;
174-
175-
uint64_t shdr_bytes = (uint64_t)header->e_shnum * header->e_shentsize;
176-
if (header->e_shentsize != sizeof(Elf64_Shdr) || header->e_shoff + shdr_bytes > file_size) {
177-
eprintf("elf: invalid section header table");
178-
return -1;
179-
}
180-
181-
Elf64_Shdr* shdrs = (Elf64_Shdr*)((uint8_t*)file_base_address + header->e_shoff);
182-
for (uint16_t i = 0; i < header->e_shnum; ++i) {
183-
Elf64_Shdr* sh = &shdrs[i];
184-
if (sh->sh_type != SHT_RELA || sh->sh_size == 0)
185-
continue;
186-
187-
if (sh->sh_offset + sh->sh_size > file_size || (sh->sh_size % sizeof(Elf64_Rela)) != 0) {
188-
eprintf("elf: invalid SHT_RELA bounds");
189-
return -1;
190-
}
191-
192-
if (elf_apply_relocation_entries((Elf64_Rela*)((uint8_t*)file_base_address + sh->sh_offset),
193-
sh->sh_size / sizeof(Elf64_Rela)) != 0)
194-
return -1;
195-
}
196-
197-
return 0;
198-
}
199-
200-
static int elf_apply_relocations_from_vfs(const char* path, uint32_t file_size, const Elf64_Ehdr* header)
201-
{
202-
if (!path || !header || header->e_shoff == 0 || header->e_shnum == 0)
203-
return 0;
204-
205-
uint64_t shdr_bytes = (uint64_t)header->e_shnum * header->e_shentsize;
206-
if (header->e_shentsize != sizeof(Elf64_Shdr) || header->e_shoff + shdr_bytes > file_size) {
207-
eprintf("elf: invalid section header table");
208-
return -1;
209-
}
210-
211-
Elf64_Shdr* shdrs = kmalloc(shdr_bytes);
212-
if (!shdrs) {
213-
eprintf("elf: failed to allocate section headers");
214-
return -1;
215-
}
216-
217-
if (elf_vfs_read_exact_path(path, (uint32_t)header->e_shoff, shdrs, (uint32_t)shdr_bytes) != 0) {
218-
eprintf("elf: failed to read section headers");
219-
kfree(shdrs);
220-
return -1;
221-
}
222-
223-
for (uint16_t i = 0; i < header->e_shnum; ++i) {
224-
Elf64_Shdr* sh = &shdrs[i];
225-
if (sh->sh_type != SHT_RELA || sh->sh_size == 0)
226-
continue;
227-
228-
if (sh->sh_offset + sh->sh_size > file_size || (sh->sh_size % sizeof(Elf64_Rela)) != 0) {
229-
eprintf("elf: invalid SHT_RELA bounds");
230-
kfree(shdrs);
231-
return -1;
232-
}
233-
234-
Elf64_Rela* relocs = kmalloc(sh->sh_size);
235-
if (!relocs) {
236-
eprintf("elf: failed to allocate relocations");
237-
kfree(shdrs);
238-
return -1;
239-
}
240-
241-
if (elf_vfs_read_exact_path(path, (uint32_t)sh->sh_offset, relocs, (uint32_t)sh->sh_size) != 0) {
242-
eprintf("elf: failed to read relocations");
243-
kfree(relocs);
244-
kfree(shdrs);
245-
return -1;
246-
}
247-
248-
int rc = elf_apply_relocation_entries(relocs, sh->sh_size / sizeof(Elf64_Rela));
249-
kfree(relocs);
250-
if (rc != 0) {
251-
kfree(shdrs);
252-
return -1;
253-
}
254-
}
255-
256-
kfree(shdrs);
257-
return 0;
258-
}
259-
260134
static uint64_t elf_runtime_addr_for_offset(Elf64_Phdr* headers, uint16_t phnum, uint64_t file_offset)
261135
{
262136
for (uint16_t i = 0; i < phnum; ++i) {
@@ -491,9 +365,6 @@ void* elf_load_from_memory_ex(void* file_base_address, uint64_t file_size, elf_i
491365
return NULL;
492366
}
493367

494-
if (elf_apply_relocations_from_memory(file_base_address, file_size, &header) != 0)
495-
return NULL;
496-
497368
return (void*)header.e_entry;
498369
}
499370

@@ -596,11 +467,6 @@ void* elf_load_from_vfs_ex(const char* path, elf_image_info_t* info)
596467
}
597468
}
598469

599-
if (elf_apply_relocations_from_vfs(path, size, &header) != 0) {
600-
kfree(program_headers);
601-
return NULL;
602-
}
603-
604470
kfree(program_headers);
605471
void* entry = (void*)header.e_entry;
606472
return entry;

source/kernel/C/interrupts/isr.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ static int skip_endbr64_if_present(InterruptFrame* frame) {
3737
return 0;
3838
}
3939

40-
static int emulate_syscall_instruction_if_present(InterruptFrame* frame) {
41-
if (!frame)
42-
return 0;
43-
44-
if ((frame->cs & 0x3) != 0x3)
45-
return 0;
46-
47-
uint8_t* ip = (uint8_t*)frame->rip;
48-
if (!ip)
49-
return 0;
50-
51-
if (ip[0] == 0x0F && ip[1] == 0x05) {
52-
debug_printf("isr: emulating SYSCALL as int 0x80 at rip=0x%X nr=%u\n", frame->rip, frame->rax);
53-
syscalls_handler(frame);
54-
frame->rip += 2;
55-
return 1;
56-
}
57-
58-
return 0;
59-
}
60-
6140
static void log_page_fault_details(InterruptFrame* frame) {
6241
if (!frame || frame->int_no != 14)
6342
return;
@@ -88,8 +67,6 @@ void exceptionHandler(InterruptFrame* frame) {
8867
case 6:
8968
if (skip_endbr64_if_present(frame))
9069
return;
91-
if (emulate_syscall_instruction_if_present(frame))
92-
return;
9370
meltdown_screen("Invalid opcode detected!", __FILE__, __LINE__, frame->err_code, getCR2(), frame->int_no, frame);
9471
break;
9572
case 8:

source/kernel/C/meltdown.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ void interrupt_frame_dump(InterruptFrame* frame) {
100100
printf("\tCS = 0x%X", frame->cs);
101101
printf("\tRFLAGS = 0x%X", frame->rflags);
102102
printf("\tRSP = 0x%X", frame->rsp);
103+
printf("\tSS = 0x%X", frame->ss);
103104

104105
printf("===============================");
105106
}

0 commit comments

Comments
 (0)