Writing programs for OS -- HELP!!!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Mastermind

Writing programs for OS -- HELP!!!

Post by Mastermind »

I have a very basic kernel written in C and would like to, instead of adding new functions to the kernel, be able to make the kernel run programs made for it. How do I add a basic library that these programs could use? After I have the library, how should I modify my kernel?
gtsphere

Re:Writing programs for OS -- HELP!!!

Post by gtsphere »

For the loading of a new program you have a few options. Either you can write the program to a Cylinder Head and Sector and call that each time the program gets called(not such a great way to do it, because what if your program is larger than the sector, then you have to make sure you find the rest of it, etc. But its a start)

Or you could design/use a filesystem and write routines to access the program on the disk. This will involve the devlopment of a memory manager, to handle your memory and the loading of the program into the memory.

As for the libraries that the programs can use, that really depends. If its a library thats used while in development, make a simple static or dynamic library under linux using your own calls. (This can be looked up in google or yahoo.com) But make sure those calls are able to be ran in your OS and don't need an inital platform, like windows/*nix.

I hope this helps and if anyone see's something wrong or would like to add, please do!
-GT
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Writing programs for OS -- HELP!!!

Post by Pype.Clicker »

GT is right. If you want programs to be loaded, you need at least a file system (even if it is trivial, like just having all files in memory a priori :)

Now, remember that your program will not be able to do anything the OS can't do. For instance, if the OS cannot display stuffs on the screen, the user program will not be able to do it, because it cannot access raw video memory ...
Mastermind

Writing programs for OS -- HELP!!!

Post by Mastermind »

Pype.Clicker wrote: GT is right. If you want programs to be loaded, you need at least a file system (even if it is trivial, like just having all files in memory a priori :)
I put my OS on a FAT12 formatted floppy. What should I do to read from the floppy? I've tried a modified version of this "fabien ..." floppy driver, but it didn't work...
Now, remember that your program will not be able to do anything the OS can't do. For instance, if the OS cannot display stuffs on the screen, the user program will not be able to do it, because it cannot access raw video memory ...
I kinda knew that... I will be adding new functions to the OS. I want to create a basic library that could only print to the screen (i'll be expanding the lib later). I know how to do that, and my OS uses this function. How can I put that function in a library so that other programs could use it. GT said sth about making a static or a dynamic lib. How would I do that?

I know that I am asking WAY too many questions. If anyone has any solutions, please help me!!

Thanks in advance.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Writing programs for OS -- HELP!!!

Post by Pype.Clicker »

what you need is not a library but rather an API : some well-defined way for the user programs to invoke the kernel (just as MS-DOS had an INT 21h.)

It is usually done by an interrupt that will decode %eax as a function code and extract parameters from the registers.

For instance, you could have

Code: Select all

int 0x32 -- eax=0 : terminate program
int 0x32 -- eax=1 : print null-terminated string at ds:esi
int 0x32 -- eax=2 : read a line from the input device and store it to ds:edi, a maximum of ecx characters will be copied
...

Now, what your "library" will look like is something like

Code: Select all

static inline exit() { asm volatile("int $0x32"::"a"(0x00)); }
static inline print(char *txt) { asm volatile ("int $0x32"::"a"(0x01),"s"(txt); }
...
Mastermind

Re:Writing programs for OS -- HELP!!!

Post by Mastermind »

I?ve got TONS of homework, so I had to cut down OS-Dev time? Sorry about the absence.

Thanks for the help, though
Chris Giese

Re:Writing programs for OS -- HELP!!!

Post by Chris Giese »

Pype.Clicker wrote: GT is right. If you want programs to be loaded, you need at least a file system (even if it is trivial, like just having all files in memory a priori :)
He could load programs from a GRUB module...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Writing programs for OS -- HELP!!!

Post by Pype.Clicker »

right. But once you had GRUB loading them, you still have to locate the different files ... I don't know how you usually do this (never used GRUB myself), but you will certainly need a "open(<filename>)" after GRUB loaded them.
Looking up directories (which is no more than a name->start-of-file table), locating the file and creating a descriptor that will have some read(), write() and seek() functions and references that will help it managing the current position in the file...
Chris Giese

Re:Writing programs for OS -- HELP!!!

Post by Chris Giese »

Pype.Clicker wrote: right. But once you had GRUB loading them, you still have to locate the different files ... I don't know how you usually do this (never used GRUB myself), but you will certainly need a "open(<filename>)" after GRUB loaded them.
After GRUB has loaded the file, you can consult the Multiboot module table to find out where it is. Then, instead of open() and read(), just form a pointer to the loaded file, and do whatever you want to it.

If it's an executable file you want to run, things get more tricky. A normal executable is linked to run at a certain address, and GRUB probably didn't load it at that address. So you need
  • Segment-based address translation (a jaw-breaking name, but it's not too difficult), or
  • Page-based address translation, or
  • Executables in some kind of relocatable format:
    • Win32-style base relocations, or
    • ELF relocations (ld --emit-relocs ...), or
    • Relocatable object file (.o file, ELF or COFF)
Later, you can add filesystem code (with open, read, etc.), and load a RAM disk as a GRUB module. The RAM disk is very simple. It's not mechanical, so you never have to wait for it, so there's no need for request queues. The RAM disk doesn't need a disk cache, either (it serves as it's own disk cache).
Post Reply