OSDev.org
https://forum.osdev.org/

How to run process from kernel
https://forum.osdev.org/viewtopic.php?f=1&t=33303
Page 1 of 2

Author:  tomsk [ Thu Nov 08, 2018 4:43 am ]
Post subject:  How to run process from kernel

Hello, I would like to know how can I load and start process from my kernel in the most simplest way.

I don't have filesystem in my OS, so in my opinion it should work like this (I don't know if it is correct), so my process have to be loaded on some specific address and then from kernel I move instruction pointer to that specific address where starts binary code of that loaded process.

This is how my OS looks like:

linker.ld
Code:
OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
{
   . = 0x100000;
   .text : { *(.text) }
   .data : { *(.data) }
   .bss  : { *(.bss)  }
}


loader.asm
Code:
bits    32
section         .text
        align   4
        dd      0x1BADB002
        dd      0x00
        dd      - (0x1BADB002+0x00)
       
global start
extern kernel_main
start:
        cli
        call kernel_main
        hlt


kernel.c
Code:
#include "drivers/keyboard.h"

int kernel_main()
{
        clearScreen();
        print("TomOS v0.1 ");
        putchar('\n');
        putchar('\n');
//here it should call my process to write Hello World

        while (1)
        {
            string ch = readStr();
            print(ch);
        }
}


and I have process saved in file hello.bin which print string Hello World written in Assembly without Syscalls (because my kernel doesn’t support it yet).

So I just wonder how can I load that hello.bin into specific address and then run instructions from that address from my kernel (I don’t have a filesystem so I think linker have to load it to some specific address).

I start my OS with these commands:

Code:
ld -m elf_i386 -T linker.ld -o iso/boot/kernel.bin obj/loader.o obj/kernel.o obj/hardware_communication.o obj/string.o obj/display.o obj/keyboard.o

qemu-system-i386 -kernel iso/boot/kernel.bin


Thank you very much

Author:  Combuster [ Thu Nov 08, 2018 6:14 am ]
Post subject:  Re: How to run process from kernel

What you have is a minimal piece of code that boots. Its does not qualify as a "kernel" in the formal definition of the word, and it still depends on undefined behaviour, such as using GRUB's stack. Then you want a process, which is a rather vague term that depends on how you define it - Even factories have processes, and they are not generally defined in terms of software.

So unless you just want to add a print statement before the last curly brace and call that a process, you should rather start thinking about what defines your "process", and from that you can deduce the kind of components you need.

Author:  tomsk [ Thu Nov 08, 2018 9:51 am ]
Post subject:  Re: How to run process from kernel

By process I mean application, as I said I have Hello World application in Assembly language which outputs Hello World to screen through VideoMemory. So I just want to load it to specific address and run it from kernel (move instruction pointer to that specific address).

Author:  Combuster [ Thu Nov 08, 2018 10:02 am ]
Post subject:  Re: How to run process from kernel

Do you know:
- incbin or bin2obj?
- memcpy?
- how to jump to an absolute address?

Then you should have all the tools to just do it.

Author:  tomsk [ Thu Nov 08, 2018 4:09 pm ]
Post subject:  Re: How to run process from kernel

Yes I know that incbin is for including binary code, but I don't understand how when I don't have a filesystem and I don't have access to system libraries.

Memcpy is for copying and I don't know how to jump to absolute address.

Isn't possible to just say linker to load that process to some address? For example my kernel starts at address 0x100000 then I could say that my process will starts at address 0x200000 for example and then in kernel I move instruction pointer to 0x200000 somehow.

Author:  Combuster [ Fri Nov 09, 2018 6:43 am ]
Post subject:  Re: How to run process from kernel

tomsk wrote:
Yes I know that incbin is for including binary code, but I don't understand how when I don't have a filesystem and I don't have access to system libraries.
So basically, you don't :D

The purpose of such tools is to take any file, and convert it to source code so you can include it in a binary. You get a block of data that then works just like any other variable initialised at compile time: its stored directly in your binary, and you don't need any other further effort to load it from disk. There's no filesystem involved, there are no system libraries involved.


The other two things you mentioned are things that can easily be researched.

Author:  tomsk [ Fri Nov 09, 2018 11:53 am ]
Post subject:  Re: How to run process from kernel

Quote:
Isn't possible to just say linker to load that process to some address? For example my kernel starts at address 0x100000 then I could say that my process will starts at address 0x200000 for example and then in kernel I move instruction pointer to 0x200000 somehow.

So is this correct?

Author:  tomsk [ Sat Nov 10, 2018 7:40 am ]
Post subject:  Re: How to run process from kernel

So I just used this command for linking my process.

Code:
ld -m elf_i386 -T linker.ld -o process.bin loader.o process.o


linker.ld (for process) contains:
Code:
OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
{
   . = 0x500000;
   .text : { *(.text) }
   .data : { *(.data) }
   .bss  : { *(.bss)  }
}


loader.o (for process) contains:
Code:
bits    32
section         .text
        align   4
        dd      0x1BADB002
        dd      0x00
        dd      - (0x1BADB002+0x00)
       
global start
extern main
start:
        cli
        call main
        hlt


then I merged process binary with my kernel binary with this command:
Code:
cat original_kernel.bin process.bin > kernel.bin


and added jump instruction to address 0x500000 (there should be my process located) into kernel.c:
Code:
#include "drivers/keyboard.h"

int kernel_main()
{
        clearScreen();
        print("TomOS v0.1 ");
        putchar('\n');
        putchar('\n');

        __asm__ __volatile__ ("jmp 0x500000");
       
        while (1)
        {
            string ch = readStr();
            print(ch);
        }
}


then I started my kernel and it starts just fine but my process didn't execute.

Author:  iansjack [ Sat Nov 10, 2018 9:27 am ]
Post subject:  Re: How to run process from kernel

Have you written the routines:

clearScreen()
print()
putchar()
readStr()

Author:  tomsk [ Sat Nov 10, 2018 10:19 am ]
Post subject:  Re: How to run process from kernel

Yea, clearScreen(), print(), putchar(), readstr() works, but my program didn't execute my hello world application at address 0x500000, because it should print Hello World na it didn't.

Author:  Combuster [ Sat Nov 10, 2018 12:46 pm ]
Post subject:  Re: How to run process from kernel

Quote:
there should be my process located
It is not.

As an exercise, post whatever documentation made you believe that.

Author:  tomsk [ Sat Nov 10, 2018 1:02 pm ]
Post subject:  Re: How to run process from kernel

Combuster wrote:
Quote:
there should be my process located
It is not.

As an exercise, post whatever documentation made you believe that.

I thought, so what is correct way to do it?

Author:  dseller [ Sat Nov 10, 2018 1:59 pm ]
Post subject:  Re: How to run process from kernel

tomsk wrote:
Combuster wrote:
Quote:
there should be my process located
It is not.

As an exercise, post whatever documentation made you believe that.

I thought, so what is correct way to do it?


The code for your executable resides at the end of your kernel. Because that’s where you placed it.
If you want it to be at the arbitrary address 0x500000 then you need to put it there first.

Author:  tomsk [ Sat Nov 10, 2018 3:12 pm ]
Post subject:  Re: How to run process from kernel

dseller wrote:
It is not.
The code for your executable resides at the end of your kernel. Because that’s where you placed it.
If you want it to be at the arbitrary address 0x500000 then you need to put it there first.

But how? Can you help me?

Author:  Combuster [ Sat Nov 10, 2018 3:54 pm ]
Post subject:  Re: How to run process from kernel

dseller wrote:
The code for your executable resides at the end of your kernel. Because that’s where you placed it.
Not even that.

The code for that executable is appended after a proper ELF file. qemu has a built-in bootloader that knows how to read ELF files, so it will look at the ELF headers and load what those headers state. It will not see the second ELF file, as there is no entry for that - its just garbage at the end.

There is also no way to tell a linker you are going to append blobs later - after all, that requires a file offset outside of the initial file which is illegal.

Page 1 of 2 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/