Re: [PATCH v5 3/9] x86/head: Move early exception panic code into early_fixup_exception

From: Andy Lutomirski
Date: Sat Apr 02 2016 - 16:14:05 EST


On Sat, Apr 2, 2016 at 11:39 AM, Borislav Petkov <bp@xxxxxxxxx> wrote:
> On Sat, Apr 02, 2016 at 07:01:34AM -0700, Andy Lutomirski wrote:
>> This removes a bunch of assembly and adds some C code instead. It
>> changes the actual printouts on both 32-bit and 64-bit kernels, but
>> they still seem okay.
>>
>> Signed-off-by: Andy Lutomirski <luto@xxxxxxxxxx>
>> ---
>> arch/x86/include/asm/uaccess.h | 2 +-
>> arch/x86/kernel/head_32.S | 49 +++++-------------------------------------
>> arch/x86/kernel/head_64.S | 45 ++------------------------------------
>> arch/x86/mm/extable.c | 29 ++++++++++++++++++++-----
>> 4 files changed, 32 insertions(+), 93 deletions(-)
>
> ...
>
>> @@ -99,21 +101,38 @@ int __init early_fixup_exception(struct pt_regs *regs, int trapnr)
>>
>> /* Ignore early NMIs. */
>> if (trapnr == X86_TRAP_NMI)
>> - return 1;
>> + return;
>> +
>> + if (early_recursion_flag > 2)
>> + goto halt_loop;
>> +
>> + if (regs->cs != __KERNEL_CS)
>> + goto fail;
>>
>> e = search_exception_tables(regs->ip);
>> if (!e)
>> - return 0;
>> + goto fail;
>>
>> new_ip = ex_fixup_addr(e);
>> handler = ex_fixup_handler(e);
>>
>> /* special handling not supported during early boot */
>> if (handler != ex_handler_default)
>> - return 0;
>> + goto fail;
>>
>> regs->ip = new_ip;
>> - return 1;
>> + return;
>> +
>> +fail:
>> + early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
>> + (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
>> + regs->orig_ax, read_cr2());
>> +
>> + show_regs(regs);
>
> To make this even better, it could be something called early_show_regs()
> or so and be a simplified version of __show_regs() on both bitness but
> which calls early_printk().
>
> This way you'll be able to get out stuff to the console as early as
> possible.
>
> Btw, you don't need to dump rIP, CR2, etc in the PANIC message above
> since you're going to early_show_regs() anyway.

Given that I this isn't really a regression with my patches (it
probably never worked much better on 32-bit and the regs never would
have shown at all on 64-bit), I propose a different approach: make
printk work earlier. Something like:

if (early) {
early_printk(args);
}

or early_vprintk or whatever.

If the cost of a branch mattered, this could be alternative-patched
out later on, but that seems silly. I also bet that a more sensible
fallback could be created in which printk would try to use an early
console if there's no real console.

--Andy