How big must be a kernel stack?

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
pepito

How big must be a kernel stack?

Post by pepito »

Some weeks ago I ask about when and where to create a stack for a kernel, now I like to know how can I calculate the size of a kernel stack.

Or where can I get information about stacks?

I know this maybe a stupid question for somebody, but I need this information.

Thank you,

pepito
carbonBased

RE:How big must be a kernel stack?

Post by carbonBased »

This will, obviously, be kernel dependant.

In order to make an estimate, just take a look at how your kernel uses the stack; do you have a lot of recursive code, functions calling functions, or functions with a lot of local variables?  If so, you'll probably like to use a larger stack.

Personally, I believe my stack is currently one page; 4096 bytes.  I have very little recursion, or local variables, and this does me just fine.  In fact, I could probably deal with half this, or even a quarter, but... there's no harm in going a little larger (aside from memory bloat).

As a general rule, run your OS through its paces, and TRY to cause a stack fault... if you don't, you've probably got a large enough stack (or perhaps too large... if you think it might be too large, make it smaller, and test again)

Cheers,
Jeff
jamethiel

RE:How big must be a kernel stack?

Post by jamethiel »

The answer is, as always, "it depends". Mainly on how much information you are going to store on the stack (function activation records, local variable storage, etc.)

My current OS has a couple device-driver tasks that use 128-byte stacks. Or 64-byte stacks, my memory is a little hazy on the issue.

I came up with one design that didn't really use a kernel stack. It pointed the ring-0 stack pointers into a section of the process state record -just large enough- to execute a PUSHAD instruction. Still haven't tried implementing it, but it should work...

My advice would be to pick an arbitrary amount of stack space to allocate, say 16k, and make sure you will know when you run over. If you do run over, increase the stack size a bit. Once you have some experience with this kind of thing you may find a better solution, but this one should work for now.

--Jamethiel
pepito

RE:How big must be a kernel stack?

Post by pepito »

Thank you very much!

pepito
Bird

RE:How big must be a kernel stack?

Post by Bird »

Instead of allocating a certain sized stack and trying to test the most stack-consuming stage, I would suggest you use an expand-on-demand one. It can be done within segment level: expand-down segment, or page level if your OS uses paging.
For exception handlers that use task gates, fixed-size stacks should probably be used, IMHO.
ka3r

RE:How big must be a kernel stack?

Post by ka3r »

I think this approach is good if you are coding a monolithic kernel and you cannot know how many stack space will your device drivers use.
On a well-coded modular kernel a page is enough.
Post Reply