0

I am trying to build my own hobby operating system and is trying to create load and run flat binary file into the heap and then execute it in protected mode?

I have tried creating a pointer and then copied a 45 bytes function to its address and then jump into it, but it gave interrupt 13 (General Protection fault)

Here is my code https://github.com/s0ubhik/myos

void aa(){
    printkc("H OH O H", 0xb);
}


void kernel_main(unsigned long kernel_stack){
    ...

    unsigned char* m = (unsigned char*) &aa;
    unsigned char* b = (unsigned char*) 0x600000;

    for (int i=0; i < 45; i++){
        print_hex(m[i], 2);
        b[i] = m[i];
        printk(" ");
    }
    printk("\n");
    for (int i=0; i < 45; i++){
        print_hex(b[i], 2);
        printk(" ");
    }
    printk("\n");

    void (*func_ptr)(void) = (void (*)(void))0x600000;
    func_ptr();

    ....
}

Look at this section where I load the contents of function aa into the heap 0x600000 and then try to jump to it enter image description here

3
  • Post a minimal reproducible example please. There's no way to guess what's wrong without seeing the code. Commented May 21, 2023 at 3:33
  • @nate-eldredge Okay just added it Commented May 21, 2023 at 3:53
  • Having a function call in your aa function is a problem. The x86 call instruction is relative, so you can't relocate the function and expect it to work, unless you fix up all those addresses. From building your code, it looks like it's trying to be position-independent and make that call through the GOT, but the calls it makes to locate the GOT don't work either... Commented May 21, 2023 at 5:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.