简体   繁体   中英

Task switching on the x86-32

what does the ESP and SS refer to?. For example, if I do a task switch from TASK_1 with privilege level of ring 0 to another TASK_2 at the same privilege level of ring 0. which will be used (ESP and SS) or (SS0 and ESP0) after the context switch is completed.

1- Is the SS0, SS1, SS2 are used only in case of stack switch "within" the task?

2- Is the SS can be holding a stack segment of any privilege level or is it dedicated to stack segment for ring 3 only ?

I assume you're talking about a 32-bit TSS (Task State Segment) which is a structure in memory used by the CPU for various purposes. The ESP and SS fields are where the CPU saves the ESP and SS of the old task and restores the ESP and SS values of the new task during a CPU managed task switch. These fields are saved and restored during a task switch just like the other general purpose and segment register fields (eg. EAX or DS).

The ESP0, ESP1, ESP2, SS0, SS1, SS2 fields are never used during task switch. They're only used when an interrupt or exception results in a change to a more privileged level within the same task. This results in a stack switch, the appropriate ESP# and SS# values from the current TSS are loaded into the ESP and SS registers and the old ESP and SS values are saved on the new stack. These TSS fields are never changed by the CPU. When the interrupt handler returns to the less privileged code, the CPU restores the saved ESP and SS values from the stack.

While a task switch can cause the privilege level of the CPU to change, it never causes the privilege level of a task to change. The new task resumes executing at the same privilege level it had when it was previously executing.

Note that while 32-bit protected mode essentially requires that an OS sets up a TSS, it doesn't require that the OS use TSSes for task switching. It's also possible for an OS to switch tasks manually, using its own mechanisms, and this how most 32-bit x86 operating systems work. (And since the CPU doesn't provide a task switch mechanism in 64-bit long mode, it's how all 64-bit x86 OSes work.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM