How to execute PE apps in Kernel?

Programming, for all ages and all languages.
Post Reply
motosftos
Posts: 13
Joined: Fri Nov 29, 2019 11:26 pm
Libera.chat IRC: MotoSoft

How to execute PE apps in Kernel?

Post by motosftos »

Hello, i am developing an Operating System. Now i want to execute some PE files. I have no idea on the code. Can anyone please help me with the code? I am following OSDEV tutorials. Please help. [-o< [-o< [-o<
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to execute PE apps in Kernel?

Post by Octocontrabass »

Which part of these steps do you need help with?

Since you're new here, I recommend reading this post to make sure you're familiar with the fundamentals of OS development. Welcome!
User avatar
thosakwe
Posts: 3
Joined: Mon Apr 30, 2018 2:24 am
Location: The Stallion project
Contact:

Re: How to execute PE apps in Kernel?

Post by thosakwe »

Do yourself a favor and start with ELF - much simpler to get a simpler implementation. If you're insistent on PE, though, Microsoft has good docs about the format. Be prepared to use paging to map pages to virtual addresses.
Another OSDev hopeful, just like you.

Project | Github | Twitter | Website
motosftos
Posts: 13
Joined: Fri Nov 29, 2019 11:26 pm
Libera.chat IRC: MotoSoft

Re: How to execute PE apps in Kernel?

Post by motosftos »

Sorry for late reply....

I have some knowledge on them i read them in Brokenthorn Entertainment and Even in OSdev Wiki, But i donot have idea on code. Please help me in coding it.
motosftos
Posts: 13
Joined: Fri Nov 29, 2019 11:26 pm
Libera.chat IRC: MotoSoft

Re: How to execute PE apps in Kernel?

Post by motosftos »

And sorry, now i migrated to program my OS in assembly.

But it would be helpful if i get assembly code.

Thanks in advance.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to execute PE apps in Kernel?

Post by iansjack »

motosftos wrote:And sorry, now i migrated to program my OS in assembly.
Bad move.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: How to execute PE apps in Kernel?

Post by nullplan »

motosftos wrote:Sorry for late reply....

I have some knowledge on them i read them in Brokenthorn Entertainment and Even in OSdev Wiki, But i donot have idea on code. Please help me in coding it.
This sounds like the guy who wanted to make a game, and had a good idea, so now he only needs someone to do the artwork, music, sounds, mechanics, and programming for him.

If you have an understanding of the file formats and of your OS, no code is needed, since that would just be a description of how to connect the two. And if you lack understanding of either the file format or your OS, no code can help you fix that.

Executable file formats are mostly about mapping memory and then having the code execute. In order for the code to do anything useful, it needs to talk to your OS. So, in order for this to be important at all, you already need working pmem allocator, paging, vmem allocator, multitasking, FS driver, partition table driver, HDD driver, and this will more than likely include a working interrupt system and DMA allocator. By the time you have all of this, the executable file format should be but a side note.

Also, please be advised that all of this will only work, if you have new executables to run, linked against a libc that works for your OS. Emulating an existing OS's system call interface is going to be difficult at best. I mean, Korona's Managarm is compatible with Linux (no small feat), and ReactOS tries to be compatible with Windows, and that endeavour is still in progress.
Carpe diem!
motosftos
Posts: 13
Joined: Fri Nov 29, 2019 11:26 pm
Libera.chat IRC: MotoSoft

Re: How to execute PE apps in Kernel?

Post by motosftos »

I migrated to assembly because i don't have much knowledge compared to assembly.

@nullplan, I actually wanted to boot the kernel written in assembly.

I want to boot the kernel from stage2 bootloader.

I used

Code: Select all

extern _osstart
and in Kernel written in assembly i used

Code: Select all

Global _osstart:
but when i compile it with this,:-

ld -melf_i386 -Ttext=0x9000 -nostdlib --nmagic -o stage2.elf stage2.o os.o

i get
boot/stage2.asm:(.text+0xba): undefined reference to `_osstart'

So why i asked it. Please help.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: How to execute PE apps in Kernel?

Post by bzt »

Hi,
motosftos wrote:Hello, i am developing an Operating System.
Please don't get this the wrong way, but you are clearly not up to the task. Do yourself a favour, and start with some smaller projects first. Do experiments on each component, and when you're okay with them, only then try to put everything together into an OS. And learn. There's extremely huge amount of theory behind OSdev you must be familiar with. It is said that OSdev is the most difficult task a programmer can do, and they say that for a good reason.
motosftos wrote:I have no idea on the code. Can anyone please help me with the code?
No. You must have at least an idea what you want to do, nobody can help you with that. When the idea is clear, then, and only then you can start coding. These things are not magic, nobody is able to code something without knowing what and how it supposed to do. (And in that I don't mean "load the OS", I mean how it should exactly work to the smallest detail.)

A simple thing that loading the kernel can be divided into many steps:
1. how your initializer code gets loaded by the firmware (first sector? EFI executable? to which address? in which CPU mode?)
2. how can you use the available environment to load a file from disk (can you use interrupts? do you need to load raw sectors or does the firmware provide file abstraction?)
3. how to parse that file (is it a PE or ELF executable at all? does it have more segments? what's phdr and bss?)
4. how to set up the environment for that executable (does it need stack? special mapping? is it position independent?)
5. how to pass control to that executable (what ABI? does it differ to the usual one? Linux kernel for example has a special kernel entry ABI, and GRUB gives you another.)

So many things to figure out and solve, and let me remind you, all of the above is needed and must be 100% correct before the CPU could start executing the first instruction at _osstart: label. And I haven't spoken about how to create that kernel executable (which is different to normal user space applications) before you could save it to disk for your loader to find. And everything I wrote so far are totally independent to in which language your kernel was written in. Could be Assembly, or could be C. I've even seen some (pretty good may I add) kernels written in Pascal.

Cheers,
bzt
Last edited by bzt on Tue Dec 17, 2019 7:36 am, edited 1 time in total.
motosftos
Posts: 13
Joined: Fri Nov 29, 2019 11:26 pm
Libera.chat IRC: MotoSoft

Re: How to execute PE apps in Kernel?

Post by motosftos »

Thanks @bzt.

Can you please refer me any sources to learn that in you tube? i have tried many, but they did not satisfy. I use to read OSdev but if they are visual(video), i can easily understand. -Sorry for bad english
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: How to execute PE apps in Kernel?

Post by bzt »

motosftos wrote:Can you please refer me any sources to learn that in you tube?
I know that it wasn't your intention, but that's actually an insult. OSdev can't be learned from a youtube tutorial. Most of us spent years if not decades to learn the OSdev things, there are countless hours of painful trial-and-error experiments behind us to get the knowledge you seek. No youtube video can give you that.

Start the old-fashioned way: buy a book, read it, try to understand it, then write small programs putting the theory you've learned into practice. Tannenbaum's Modern Operating Systems would be a good start, but there are many other perfect books. When you've understood the theory, and you gained some experience with it in practice, then start downloading and reading hardware specifications. Those are a different kind of beasts, but you also have to learn them and learn how to control them. Most specification is a PDF book. What is common in all quality OSdev materials, neither of them is a youtube video.

A good collection of related books: https://github.com/concerttttt/books/ (Modern Operating Systems included). Also books on C and C++ programming and compiler theory.

Cheers,
bzt
motosftos
Posts: 13
Joined: Fri Nov 29, 2019 11:26 pm
Libera.chat IRC: MotoSoft

Re: How to execute PE apps in Kernel?

Post by motosftos »

Thanks
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Re: How to execute PE apps in Kernel?

Post by iocoder »

motosftos wrote:Please help. [-o< [-o< [-o<
This really touched my heart. This article is a good start.
motosftos
Posts: 13
Joined: Fri Nov 29, 2019 11:26 pm
Libera.chat IRC: MotoSoft

Re: How to execute PE apps in Kernel?

Post by motosftos »

@bzt link is not working
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: How to execute PE apps in Kernel?

Post by bzt »

motosftos wrote:@bzt link is not working
It was when I wrote that post. That's not my repo, I'm afraid there's not much I can do about it. Use your search skills, that's what I did. I'm sure you can find those books elsewhere too.

Cheers,
bzt
Post Reply