Skip to content
Advertisement

Transition of multi-core processor from real mode to protected mode in Linux

I’m trying to learn more about operating systems and I am currently taking a look how the Linux kernel is loaded and initialized.

Thanks to several related questions and this book, I was mostly able to understand how the transition from real mode to protected mode works.

However, the one thing I don’t understand is, how and when the rest of the cores are switched to protected mode for multi-core processors (as far as I know, every core has its own set of control registers and the switch seems only to happen once in the booting process).

(I only found this somehow related question with an (short) answer that it is not possible to have two cores in different modes, however there was no source given.) This answer is wrong (thanks to e.g. n.m. in the comments for pointing that out).

Edit: As the comments already pointed out, the kernel might do this later in the initialization process, however when does that happen?

Advertisement

Answer

Thanks to the input by sawdust, Peter Cordes and Michael Petch I was able to find the solution to my question (here for Linux v4.16):

Basically the transition from real mode to protected mode happens on every startup of a core (if hotplug is supported, that can happen at any time).

The first time a startup of a secondary processor happens, is during the SMP (Symmetric Multiprocessing) initialization (which is part of the kernel initialization). There, cpu_up() is called for all CPUs present during initialization:

for_each_present_cpu(cpu) {
  if (num_online_cpus() >= setup_max_cpus)
    break;
  if (!cpu_online(cpu))
    cpu_up(cpu); // Startup of the core in the following functions
}

This function call is leading to the activation of the cores using APIC (see INIT/SIPI signals).

At first, the woken cores are in real mode and get the real mode trampoline code for x86-64 as an entry point, in the case of x86-64.

There and in the following functions, the transition to protected mode for secondary processors happens (e.g. setting the PE bit):

# Enable protected mode
movl  $X86_CR0_PE, %eax  # protected mode (PE) bit
movl  %eax, %cr0         # into protected mode
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement