Skip to content

Growing the Stack

April 18, 2012

For a long time I have meant to look at code to detect stack underflow and grow the stack accordingly, but never got round to it. This was brought to a head when writing the disk buffering code; I was getting some unexplained errors that turned out to be stack underflows due to excessively recursive function calls (which I solved separately).

I’ve always thought this would be quite hard but I had a look, and it turned out to be very easy. I’ve now added code that will detect an underflow of the user stack (which is the only one that we don’t have tight control of) and allocate a new page if needed. I haven’t yet incorporated it in the code on the web, but for those interested, here is the revised page fault handler (where all the work is done).

You will need to make one more change for this code to work. Currently the user stack is located at the page starting 0xA00000. The page immediately before that is actually part of the kernel stack address space and is not accessible by user programs. So UserStack needs to be changed in “memory.h” and “memory.inc”. I’ve made it 0xBFF000; that gives quite a few available pages for the user stack; if it grows bigger than this there’s probably something wrong. (But you can set aside as much of the virtual address space as you like.)

Advertisements

From → IanOS

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: