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.
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 )[/list]
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?
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.
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.