When I have to setup the EIP register, the program doesn't jump to the correct position. I'm expecting that jmp *%ecx
jumps at the right spot in memory setting EIP to around 0xC0100000 (label: StartInHigherHalf
) using LEA. I don't think that kmain
is necessary because the problem is before it is called. I'm going to post it anyway.
I've tried to debug it with the -d cpu
flag on QEMU, and before the jump (blocked with HLT) says that ECX is not loaded with the LEA function. Is it possible for LEA instruction not to execute? Why can that happen? How can I fix it?
Boot.S:
.set ALIGN, 1<<0
.set MEMINFO, 1<<1
.set FLAGS, ALIGN | MEMINFO
.set MAGIC, 0x1BADB002
.set CHECKSUM, -(MAGIC + FLAGS)
.set KERNEL_VIRTUAL_BASE, 0xC0000000
.set KERNEL_PAGE_NUMBER, (KERNEL_VIRTUAL_BASE >> 22)
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
.section .data
.align 0x1000
BootPageDirectory:
.quad 0x00000083
.rept KERNEL_PAGE_NUMBER - 1
.quad 0
.endr
.quad 0x00000083
.rept 0x400 - KERNEL_PAGE_NUMBER - 1
.quad 0
.endr
.set STACKSIZE, 0x4000
.global __start__
.set __start__, (setup)
setup:
mov $(BootPageDirectory - KERNEL_VIRTUAL_BASE), %ecx
mov %ecx, %cr3
mov %cr4, %ecx
or $0x00000010, %ecx
mov %ecx, %cr4
mov %cr0, %ecx
or $0x80000000, %ecx
mov %ecx, %cr0
lea StartInHigherHalf, %ecx
jmp *%ecx
StartInHigherHalf:
movl $0, (BootPageDirectory)
invlpg (0)
mov $(stack + STACKSIZE), %esp
push %eax
push %ebx
call _init
call kmain
cli
1: hlt
jmp 1
.section .bss
.align 32
.lcomm stack, STACKSIZE
.global gdtFlush
.extern gp
gdtFlush:
lgdt (gp)
mov $0x10, %eax
mov %eax, %ds
mov %eax, %es
mov %eax, %gs
mov %eax, %fs
mov %eax, %ss
ljmp $0x08, $setcs
setcs:
ret
linker.ld:
ENTRY(__start__)
OUTPUT_FORMAT(elf32-i386)
SECTIONS {
. = 0xC0100000;
.text ALIGN(0x1000) : AT(ADDR(.text) - 0xC0000000) {
*(.multiboot)
*(.text)
}
.rodata ALIGN(0x1000) : AT(ADDR(.rodata) - 0xC0000000) {
*(.rodata*)
}
.data ALIGN(0x1000) : AT(ADDR(.data) - 0xC0000000) {
*(.data)
}
.bss ALIGN(0x1000) : AT(ADDR(.bss) - 0xC0000000) {
_sbss = .;
*(COMMON)
*(.bss)
_ebss = .;
}
}
kmain.c:
#include <kernel/tty.h>
#include <kernel/log.h>
#include <kernel/stack-protector.h>
#include <kernel/gdt.h>
__attribute__ ((noreturn));
void kmain(void) {
gdtInstall();
initializeTerminal();
char c;
char *buffer = &c;
char *start = buffer;
char str[] = "Hello, kernel World!";
atomicallyLog(1, 1, str, buffer);
kprintf(start, 1);
}
ecx
to still have the value set by LEA, if you're only looking at it afterkmain
returns? Why not set a breakpoint on the LEA instruction? This doesn't appear to be a minimal reproducible example of anything, because you don't show the asm forkmain
, or show any numeric values that you actually saw vs. expected to see.hlt
/jmp
loop, then probablyjmp *%ecx
is working as you expect, otherwise you probably wouldn't end up there. Are you really only looking atecx
at that point like you say in the 2nd paragraph? If so, why don't you use the debugger to look at ECX earlier, before other stuff destroys it?