2
\$\begingroup\$

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!!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Well, i dont know the processor you are dealing with, but i will tell from a generic CPU point of view.

Modern CPUs are pipelined, this means that instructions are executed in discrete "stages". Each stage of the pipeline deals with a instruction, if the first instruction of the program has left the instruction fetch stage, that stage might fetch another instruction, as soon as the first instruction leaves the instruction decode stage, the instruction decode might fetch another instruction from the instruction fetch stage and so on.

Problem is, if you have an instruction like jnz (or bnz) "jump if not zero" in the middle of the instruction stream, it cannot be executed until all instructions that preceded it are completely executed and the flag register is stabilized into a known state. This will cause the pipeline to insert bubbles (effectively NOP instructions) until the pipeline is flush and the jnz can be executed. If the CPU has a branch prediction unit, it will execute the JNZ regardless of the other instructions (especulative execution) and later check if the jump was correct.

I believe this JNZ (BRNZ) or any other conditional jump instruction is what the book is talking about. Probably the processor being talked about has no especulative execution and inserting a conditional jump to the next instruction will have the side effect of flushing the pipeline.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.