bobgimax wrote:
I understood that the bootloader must switch the CPU to protected prior to jumping to the kernel code so I read Intel Software Developer's Manual and in Volume 3, section 9.9.1 Switching to Protected Mode and it's said that after switching to protected mode the system must execute a task (even if the system won't use hardware multitasking). That implies setting up a TSS etc before switching to PM.
I conclude that the kernel must be the first task executed by the system and that this must be done a few instructions after switching to PM.
I think there is a mistake in your modelling: The kernel is not just one task. Indeed it could be argued that all tasks are kernel tasks, just some spend the majority of their time executing user space programs.
The CPU will be satisfied with a valid value in the Task Register, and even that will only be necessary once multiple privilege levels get involved. It is completely proper for the bootloader not to set up a TSS and leave that to the kernel. Common sequence is that:
- Bootloader uses BIOS Interrupts to load kernel et. al. into memory.
- Bootloader loads GDT.
- Bootloader switches to protected mode and jumps to kernel
- Kernel sets up paging.
- Kernel loads another GDT/IDT, now that the addresses are fixed
- Kernel loads the Task Register with a Task Gate.
Admittedly, the paging set up might also be done by the bootloader. It depends on the protocol. In my case, I have a 64-bit OS, so it is a bit more complicated. But having to load a new GDT in the kernel is nothing out of the ordinary, and only once the final GDT has been loaded does it make sense to load the Task Register.
Notice that the CPU has absolutely no interest in the task organization you have going on in your OS, especially if you don't use hardware task switching. I will build my OS such that the first task, which automatically gets PID 0, will end up being the idle task, and the second task, getting PID 1, will be the init task. PID 1 is special and reserved by POSIX, so I have to do it like this. But after that, anything goes.