Stack Allocation

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
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Stack Allocation

Post by pcmattman »

Hello all, I've done the OSDev tutorial over at osdever.net (Bran's tutorial)... That was last month and I've since added quite a bit. The problem is, I'm implementing multitasking and I'm doing it this way:
  • Timer interrupt fires, check timeslice - if 0 then reset it and call resched, passing the pointer to the struct passed to the ISR
  • Resched saves the old EIP, loads the new one and modifies the pointer to tell the ISR to return to the new EIP
  • Timer ISR returns to next process
The problem is, I keep getting GPF's and Invalid Opcode errors... I think it might have something to do with the stack location. If not, then please tell me what's wrong.

It's probably good anyway to find out how to do stack allocation, can anyone give me any pointers? (pun intended :D )[/list]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

If you are getting invalid opcodes, you haven't returned to the EIP you were expecting. As you say, this is normally an incorrect number of values on the stack or stack misallignment.

* When you set up your tasks, are you definitely creating a new stack with the correct number of values?

*Have you loaded the correct value of ESP before trying to pop the values?

* Are you using paging? If so, have all the pages containing your code and data got the required privilege level?

* If you get A GPF, have you checked what the CPL is at the time of the error? How does it relate to the RPL for all your segments. Also, do you try to access any ports without setting the TSS bitmap or IOPL?

Hope some of these points help,
Adam
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Well, they all share the same stack. Today I'm implementing stack allocation, so any pointers as to how to do the switching of the stack would be really good.

I found a way of doing stack allocation in a book I came across last night.
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

Oftentimes you'll keep a kesp variable in every thread, pointing to the stack frame on that thread's kernel stack created by an interrupt. This frame would contain your general purpose registers, segment registers, eip, stack registers, etc.

To switch stacks, you would simply change the current esp based on kesp just before leaving your HLL kernel for assembler interrupt stubs. Then when the kernel went to do its interrupt-return, it would restore the stack frame from the new thread and return to it.

Note that this means the stack pointer must be saved in the exact same procedure that will later restore it.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I've taken what you said, implemented it, and now multitasking without a hitch... Thankyou for your help!
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

You're very welcome 8) .
Post Reply