diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c --- a/sys/amd64/amd64/machdep.c +++ b/sys/amd64/amd64/machdep.c @@ -1518,13 +1518,11 @@ /* * We initialize the PCB pointer early so that exception - * handlers will work. Also set up td_critnest to short-cut - * the page fault handler. + * handlers will work. */ cpu_max_ext_state_size = sizeof(struct savefpu); set_top_of_stack_td(&thread0); thread0.td_pcb = get_pcb_td(&thread0); - thread0.td_critnest = 1; /* * The console and kdb should be initialized even earlier than here, @@ -1615,7 +1613,6 @@ #ifdef FDT x86_init_fdt(); #endif - thread0.td_critnest = 0; kasan_init(); kmsan_init(); diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -104,7 +104,17 @@ static struct session session0; static struct pgrp pgrp0; struct proc proc0; -struct thread0_storage thread0_st __aligned(32); +struct thread0_storage thread0_st __aligned(32) = { + .t0st_thread = { + /* + * thread0.td_pflags is set with TDP_NOFAULTING to + * short-cut the vm page fault handler until it is + * ready. It is cleared in vm_init() after VM + * initialization. + */ + .td_pflags = TDP_NOFAULTING, + }, +}; struct vmspace vmspace0; struct proc *initproc; diff --git a/sys/vm/vm_init.c b/sys/vm/vm_init.c --- a/sys/vm/vm_init.c +++ b/sys/vm/vm_init.c @@ -159,6 +159,14 @@ pmap_init(); vm_pager_init(); + /* + * Now we can properly handle calls into vm_fault() from + * kernel page faults during initialization, typically to + * panic. Clear the nofaulting flag set for thread0 in the + * image, see kern/init_main.c + */ + curthread->td_pflags &= ~TDP_NOFAULTING; + #ifdef INVARIANTS vm_check_pagesizes(); #endif