Page 1 of 1

Executing program

Posted: Wed Jan 08, 2014 12:21 pm
by wrozowski
Hello,

This is my first post here. I'm newbie in os development, still I'm learning :D
I went through the James'M kernel tutorial. I've got one problem.
Orginally on initrd.img are two txt files.
I would like to add function, which could run a program from that disc img file.
But how to do it?

Should I allocate memory using kmalloc() and then copy program and jump into it?

Greetings
Wojtek ;-)

Re: Executing program

Posted: Wed Jan 08, 2014 1:56 pm
by sortie
Hi wrozowski,

You need to understand executable program formats considerably better. For instance, programs are linked at particular locations. When you load a program, you need to load each segment in the program to the appropriate memory locations (and potentially zero the end of the segment if the in-memory version is longer than the in-file version). Basically:

Code: Select all

verify_file_is_actually_a_program(file);
for ( segment in file->program_headers )
{
    if ( segment is not loadable )
        continue;
    allocate(segment->load_here to segment->load_here, segment->load_size);
    memcpy(segment->load_here, file + segment->offset_in_file, segment->size_in_file);
    memset(segment->load_here + segment->size_in_file, 0, segment->load_size - segment->size_in_file);
}
stack st = make_stack_for_program();
registers regs = setup_registers(file->program_entry_point, st);
create_thread(regs);
Now, that's the basic idea and it's wrong. You also need to deal with paging and put the memory inside user-space for this to work. You also need to pay attention to where the program itself is loaded. You also need a scheduler if you wish to jump between running processes. All this requires you understand the ELF used for programs and a number of concepts related to scheduling and memory management. Perhaps reconsider doing this if you don't have a working user-space, a scheduler or memory management. Ideally, you should reach a point where you don't need to ask questions like this because you can figure out the answer from your understanding of executable programs.

And no - the kmalloc and copy method won't work. For one thing, you need to load the memory at the correct location and the linker might disagree with you on that point. Secondly, the programs generated by the linker are not flat binaries they won't work properly if loaded naively. If you somehow manage to reliably convert a program into a flat image and load it at the same place the linker thinks you would, then this would actually work. But flat binaries are really silly and you'll want to immediately implement a real format like ELF.

I recommend learning the ELF executable format and reading the system V ABI (though this might be a bit advanced - but needed). You should also play around with tools like readelf(1) and objdump(1).

Re: Executing program

Posted: Fri Jan 10, 2014 5:06 am
by brunexgeek
wrozowski wrote:Should I allocate memory using kmalloc() and then copy program and jump into it?
sortie wrote:...If you somehow manage to reliably convert a program into a flat image and load it at the same place the linker thinks you would, then this would actually work....
Well, if your "program" is a flat binary that have no external dependencies you could do that. Basically that is what a [simple] bootloader do when load your kernel or a second stage bootloader. But keep in mind that your "program" must include everything it need (functions to allocate memory, print to screen, etc.) or should know how to call these functions from your kernel (I don't recommend this).

I think you can do that as a first attempt, but for a definitive or best solution the sortie recommendations should be taken into account.

Re: Executing program

Posted: Fri Jan 10, 2014 8:04 am
by wrozowski
Hi,

Thanks a lot sortie and brunexgeek, your replies really helped me with understandantig what really is an executable file.
Thank's sortie for your recommandation. I'll leran more about the ELF files.

Greetings
Wojtek :D