Re: [PATCH v5 13/20] x86/efistub: Perform 4/5 level paging switch from the stub

From: Ard Biesheuvel
Date: Thu Jun 08 2023 - 02:35:14 EST


On Thu, 8 Jun 2023 at 02:43, Yunhong Jiang
<yunhong.jiang@xxxxxxxxxxxxxxx> wrote:
>
> On Wed, Jun 07, 2023 at 10:31:43PM +0200, Ard Biesheuvel wrote:
> > On Wed, 7 Jun 2023 at 22:19, Yunhong Jiang
> > <yunhong.jiang@xxxxxxxxxxxxxxx> wrote:
> > >
> > > On Wed, Jun 07, 2023 at 09:23:35AM +0200, Ard Biesheuvel wrote:
...
> > > > +efi_status_t efi_setup_5level_paging(void)
> > > > +{
> > > > + u8 tmpl_size = (u8 *)&trampoline_ljmp_imm_offset - (u8 *)&trampoline_32bit_src;
> > > > + efi_status_t status;
> > > > + u8 *la57_code;
> > > > +
> > > > + if (!efi_is_64bit())
> > > > + return EFI_SUCCESS;
> > > > +
> > > > + /* check for 5 level paging support */
> > > > + if (native_cpuid_eax(0) < 7 ||
> > > > + !(native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31))))
> > > > + return EFI_SUCCESS;
> > > > +
> > > Do we need to check the need_toggle here instead of at efi_5level_switch and
> > > skip the whole setup if no need to switch the paging level? Sorry if I missed
> > > any point.
> > >
> >
> > No. There are reasons why firmware might run with 5 levels, and switch
> > to 4 levels at ExitBootServices() time.
>
> The need_toggle check at efi_5level_switch(), "need_toggle = want_la57 ^
> have_la57", should cover this scenario, right? If we check need_toggle on
> efi_setup_5level_paging() and it's false, then we don't need the setup in
> efi_setup_5level_paging(), right? I don't see the la57_toggle() called on other
> places.
>
> Or I misunderstand your response?
>

The actual, current number of paging levels could be 5 during
efi_setup_5level_paging() and 4 during efi_5level_switch(). So whether
we need to toggle can only be decided at switch time, at which point
we can no longer allocate memory. So the allocation logic in
efi_setup_5level_paging() cannot depend on the actual number of
levels, only on whether or not 5 level paging is supported at all (in
which case a switch is never needed)