Page 1 of 1

What's next for my kernel?

Posted: Tue Jul 22, 2008 12:25 am
by osdf_1
Hi, what should I try to add to my kernel now? Here's what it does:

- Set up new GDT
- Set up IDT
- Set up ISRs
- Set up IRQs
- Set up video functions (printChar(), print(), etc)
- Handle keyboard

I read somewhere, I believe in Bran's Kernel Development Tutorial (that's the one I read to go this far), that I should go on with memory management. Is this an easy task? What about running applications? Is that something you can do about now?

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 12:47 am
by leledumbo
... I should go on with memory management. Is this an easy task?
Absolutely NO! I've passed through thousands of triple faults because of it.
What about running applications? Is that something you can do about now?
Try SkelixOS. I don't think it's possible before you have a memory manager, how would you give memory space to your application without it?

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 2:11 am
by Stevo14
leledumbo wrote:
... I should go on with memory management. Is this an easy task?
Absolutely NO! I've passed through thousands of triple faults because of it.
I have to respectfully disagree. Memory management is probably the next thing that needs to be done in osdf_1's kernel.

I would recommend starting with a simple "watermark" allocator for physical memory. See the wiki: Memory_Allocation. Especially the first part called "a very simple memory manager".

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 2:26 am
by Brendan
Hi,
Stevo14 wrote:
leledumbo wrote:
... I should go on with memory management. Is this an easy task?
Absolutely NO! I've passed through thousands of triple faults because of it.
I have to respectfully disagree. Memory management is probably the next thing that needs to be done in osdf_1's kernel.

I agree with Stevo - memory manager should come next.
I also agree with Leledumbo - implementing a memory manager isn't an easy task (or at least, IMHO implementing memory manager/s *shouldn't* be an easy task).

Note 1: If you think memory manager/s are easy, then you're probably not trying hard enough (e.g. not optimizing enough, not supporting enough features, etc).... :D

Note 2: Regardless of how hard or easy it is, postponing it won't make it easier!


Cheers,

Brendan

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 4:51 am
by naiksidd_85
True I do think memory manager is most important part that should come up in the line.
Creating an memory manager is not and easy job but, again its your design desicion.
you may want your memory manager to just create pages of specified size and have a link list of it. Or you may want to implement something like variable size pages all linked so that no memory is wasted.

You may even go ahead and try to write drivers like basic devices eg floppy drive or cd rom butyou wont be able to continue far without memory manager

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 5:12 am
by jal
Brendan wrote:Note 1: If you think memory manager/s are easy, then you're probably not trying hard enough (e.g. not optimizing enough, not supporting enough features, etc).... :D
Memory managers are not easy, but a basic memory manager is simple enough. Optimizing and the like can wait for later. No need to dive into page-colouring-mixed-bitmap-stack-allocation and the like directly. First keep it simple, improve later.


JAL

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 5:44 am
by AndrewAPrice
I think you should decide to go with a memory manager. Memory management isn't a scary field like it's made out to be. Are you going to use paging (must easier than a flat memory model since you don't need to reply on position independent code)? It's not that difficult.. You just need to divide the memory in to pages (usually 4KB but you can do 4MB also), keep a list of free/used pages (a huge bitmap with 1 set if a page is allocated, 0 if it's free), and have a function for finding the next free page. Set up a simple data structure in memory (an array of addresses with a few special bit set), pass the processor the address, and you're all set :)

I recommend porting a stable malloc implementation. liballoc is nice since it only requires a few hook functions (so it can use your code to find the next free page). Then you'll have malloc/free and life will be much simpler for the time being (since you can use dynamically sized strings in paths, allocate objects during runtime, you can use linked lists).

After you have a memory manager I would recommend you get programs loading and executing (Grub modules help a lot since you don't need a file system or disk driver). Each program will require it's own table of pages.

That should keep you occupied for a while. Once that is done get a system working to communicate between os<->program (interrupts are commonly used), then I would focus on what level of abstraction for your drivers (you already have a keyboard and screen driver!) One of the problems I had earlier on was getting ahead of myself here and forgetting what level of abstraction I wanted.

If any of that seems scary, I assure you it's not. We're here to help :)

Just remember, divide your goals (and your code) up in to small easy chunks, and do them one by one. e.g.

Code: Select all

void init_paging()
{
    /* Step 1: construct a list of pages and mark everyone as free (except for the first few MB for the kernel/grub) */
    /* (skip step 2/3 until you want to start loading programs) */
    /* Step 2: make the relevant page tables (huge array of addresses) and page directory (a table of page tables)*/
    /* Step 3: give the processor the location of the page directory */
}

unsigned int allocate_page_for_kernel(unsigned int noPages)
{
    /* Step 1: scan through list and find free pages */
    /* Step 2: mark those pages as being used */
    /* Step 3: return the address of the first page */
}

vod free_page_from_kernel(unsigned int firstpage, unsigned int noPages)
{
    /* Step 1: loop through each page allocated, mark each one as free */
}

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 8:07 am
by lukem95
@ MessiahAndrw

That's some great advice, if there was a rating system on osdev, i'd give you 5 stars :)

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 9:06 am
by inflater
That's some great advice, if there was a rating system on osdev, i'd give you 5 stars
Oh please. :roll:

Before adding a memory manager, I would create a simple fdd/hdd driver and separate the shell off the kernel file. I think kernel itself needs no memory managment, as the kernel developer should know what parts of RAM are used or not (or to reserve e.g. two megabytes before the shell and applications).

Also I would forget about paging and virtual memory completely, but that's just me :)

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 10:37 am
by JamesM
inflater wrote:
That's some great advice, if there was a rating system on osdev, i'd give you 5 stars
Oh please. :roll:

Before adding a memory manager, I would create a simple fdd/hdd driver and separate the shell off the kernel file. I think kernel itself needs no memory managment, as the kernel developer should know what parts of RAM are used or not (or to reserve e.g. two megabytes before the shell and applications).

Also I would forget about paging and virtual memory completely, but that's just me :)
:| What horrible advice! Loading drivers and user programs before you have memory management? That means you have to retrofit and change all your abstractions once you finally implement one.

IMHO that's just a hack to get yourself to the stage where you can see results quicker. Not the best idea in the slightest.

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 10:58 am
by inflater
JamesM wrote:Loading drivers and user programs before you have memory management?
Nope.
me wrote:I would create a simple fdd/hdd driver and separate the shell off the kernel file.
I do this because my kernel starts from real mode (and kernels not-using GRUB do that too) and plus, separating it will give you a stability increase. :)

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 11:56 am
by Korona
inflater wrote:I think kernel itself needs no memory managment, as the kernel developer should know what parts of RAM are used or not (or to reserve e.g. two megabytes before the shell and applications).
If the kernel has no memory manager it cannot allocate memory for task, thread and ipc structures. I would suggest to implement paging code early. If you wait until your kernel becomes bigger you'll have to modify lots of code fragments that have to use virtual addresses instead of physical ones when paging is enabled.
Implementing memory management is a good idea as it makes other tasks easier/possible. (such as writing drivers for a monolithic kernel).

Re: What's next for my kernel?

Posted: Tue Jul 22, 2008 2:19 pm
by bewing
There is a big difference between a physical memory allocator, and a virtual memory allocator.

During the kernel initialization phase, you certainly need to do some simple physical memory allocation. You certainly do not need to use virtual memory at all.

Your next step (to be more detailed about it) is going to be to parse the hardware memory map that you get from GRUB, or the BIOS. Then you probably need to allocate some important buffers and arrays for your kernel. Then you need to set up a physical memory heap. Then you need to write (or find) a nice physical memory malloc/free function.

You may (or may not) find it useful (several steps from now) to create temporary singletasking-mode drivers, that will read disks and things until you have multitasking up and running (several steps after that).

Re: What's next for my kernel?

Posted: Mon Jul 28, 2008 3:55 am
by osdf_1
Is all this needed if I'm writing a single-task kernel? If not, would my next step be implementing a kind of file system?