Electronic – Pipeline Flushing in microprocessors

assemblymicroprocessorx86

I am reading the book "Writing Operating System from Scratch" by Nick Blundell. In one of the chapters, it is explained how we make transition from 16bit mode to 32bit mode. It says that before moving to 32bit mode we should ensure that all the instructions in 16bit mode which are currently inside the pipeline (instruction pipeline inside CPU) are executed. To accomplish this it says, we should make a far jump which causes a pipeline flush which the book says is completing all instructions currently in the different stages of the pipeline.
But what I understood about pipeline flushing is removing all the instructions after the conditional branch from the instruction pipeline due to changed flow of the program.
I am confused between these descriptions, could someone please explain what happens during pipeline flushing.
Further, the book says a near jump may not be sufficient to do pipeline flushing. I do not understand this as well.
There is a similar question :
Stalling and Flushing in MIPS Piplining

But could someone please explain about this in somewhat more detail.

Thanks in advance!!

Best Answer

Processors have a lot of really neat math tricks that they can do to optimize things and reduce cycle times, but most of those depend of the next step being predictable. A processor, by itself, cannot examine an instruction without executing it, so only certain commands can be put into the pipeline - because the next steps are all completely predictable.

Conditional logic cannot be predicted. The processor just knows that it has been instructed to go from where it is, to where you want it to be next. Remember that the pipeline has (or could have) unfinished business when it discovers this command. So, as a built in feature, before the processor executes the conditional logic - in this case, the jump - it will allow the pipeline to empty, and detect that it is empty internally.

In some cases, a near jump compiled into machine code may be optimized into something that the processor doesn't treat as conditional - if that near jump is for a common purpose, the processor might actually be able to continue pipelining. This is why a far jump is recommended to make sure the processor actually flushes the pipeline.

Related Topic