I started to implement software multitasking in my system, but question appeared: IRQ0 handler switches tasks, but how can I save EIP and CS and other segment registers? I tried not to use TSS but it must be wrong. Perhaps I understood word 'software' wrong
So, 'software' isn't absolutely software? Saving segment registers should be performed by hardware?
Software task switch
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Software task switch
if you use interrupt/trap gates (and not task gates) you stay within the same task's context, and an interrupt will load cs/ip/ss/esp from the IDT and TSS as the minimal necessary set to make safe entry into kernel mode. The old values for cs/ip/ss/esp/flags are stored on the stack for you.
The hardware still does a lot for you, but it can't do anything less and be able to maintain system consistency. That means that of the 50 registers you might want to save on a true task switch, only 5 are actually saved at that moment.
The hardware still does a lot for you, but it can't do anything less and be able to maintain system consistency. That means that of the 50 registers you might want to save on a true task switch, only 5 are actually saved at that moment.
Re: Software task switch
Thanks for so fast reply. I should read manuals more carefully... I've forgotten about stack usage.. Shame on me.
Re: Software task switch
Task switch can happen only in kernel mode. When thread enters kernel mode, the hardware is storing user mode ss/esp/eflags/cs/eip (interrupt and trap gates are using; lets not take into account fastcalls). Usually interrupt handlers are storing all general purpose registers and base data segment registers too. Kernel mode has partly uniform environment for any thread of any process including the time when task switching routine is called. For example in my kernel cs=KCODE, ds=es=ss=KDATA, df=0; interrupt sensitivity=off during executing task switching routine. This routine does following things:
- pushes fs;
- pushes gs;
- stores esp of the old thread;
- stores address of the new thread structure in "current" variable (it is the alias for TSS.SP0; thread structure is located at the upper part of the kernel stack region);
- loads esp for the new thread;
- pops gs;
- pops fs;
- sets CR0.TS;
- loads CR3 for the new thread;
- returns execution, i.e. pops eip from the new kernel stack.
- pushes fs;
- pushes gs;
- stores esp of the old thread;
- stores address of the new thread structure in "current" variable (it is the alias for TSS.SP0; thread structure is located at the upper part of the kernel stack region);
- loads esp for the new thread;
- pops gs;
- pops fs;
- sets CR0.TS;
- loads CR3 for the new thread;
- returns execution, i.e. pops eip from the new kernel stack.
If you have seen bad English in my words, tell me what's wrong, please.