diff --git a/sys/i386/i386/sys_machdep.c b/sys/i386/i386/sys_machdep.c index 1f4e90782f27..467ee651fe2d 100644 --- a/sys/i386/i386/sys_machdep.c +++ b/sys/i386/i386/sys_machdep.c @@ -510,7 +510,7 @@ i386_set_ldt(td, uap, descs) int error = 0, i; int largest_ld; struct mdproc *mdp = &td->td_proc->p_md; - struct proc_ldt *pldt = NULL; + struct proc_ldt *pldt; union descriptor *dp; #ifdef DEBUG @@ -697,14 +697,18 @@ i386_ldt_grow(struct thread *td, int len) return (ENOMEM); if (len < NLDT + 1) len = NLDT + 1; + + /* Allocate a user ldt. */ pldt = mdp->md_ldt; - /* allocate user ldt */ if (!pldt || len > pldt->ldt_len) { - struct proc_ldt *new_ldt = user_ldt_alloc(mdp, len); + struct proc_ldt *new_ldt; + + new_ldt = user_ldt_alloc(mdp, len); if (new_ldt == NULL) return (ENOMEM); pldt = mdp->md_ldt; - /* sched_lock was held by user_ldt_alloc */ + + /* sched_lock was acquired by user_ldt_alloc. */ if (pldt) { if (new_ldt->ldt_len > pldt->ldt_len) { old_ldt_base = pldt->ldt_base; @@ -720,7 +724,7 @@ i386_ldt_grow(struct thread *td, int len) } else { /* * If other threads already did the work, - * do nothing + * do nothing. */ mtx_unlock_spin(&sched_lock); kmem_free(kernel_map, diff --git a/sys/i386/i386/vm_machdep.c b/sys/i386/i386/vm_machdep.c index df0159220a7e..7a9837d53fbd 100644 --- a/sys/i386/i386/vm_machdep.c +++ b/sys/i386/i386/vm_machdep.c @@ -153,7 +153,9 @@ cpu_fork(td1, p2, td2, flags) if ((flags & RFMEM) == 0) { /* unshare user LDT */ struct mdproc *mdp1 = &p1->p_md; - struct proc_ldt *pldt = mdp1->md_ldt; + struct proc_ldt *pldt; + + pldt = mdp1->md_ldt; if (pldt && pldt->ldt_refcnt > 1) { pldt = user_ldt_alloc(mdp1, pldt->ldt_len); if (pldt == NULL) @@ -295,11 +297,12 @@ cpu_set_fork_handler(td, func, arg) void cpu_exit(struct thread *td) { - struct mdproc *mdp; - /* Reset pc->pcb_gs and %gs before possibly invalidating it. */ - mdp = &td->td_proc->p_md; - if (mdp->md_ldt) { + /* + * If this process has a custom LDT, release it. Reset pc->pcb_gs + * and %gs before we free it in case they refer to an LDT entry. + */ + if (td->td_proc->p_md.md_ldt) { td->td_pcb->pcb_gs = _udatasel; load_gs(_udatasel); user_ldt_free(td); @@ -309,16 +312,16 @@ cpu_exit(struct thread *td) void cpu_thread_exit(struct thread *td) { - struct pcb *pcb = td->td_pcb; #ifdef DEV_NPX if (td == PCPU_GET(fpcurthread)) npxdrop(); #endif - if (pcb->pcb_flags & PCB_DBREGS) { - /* disable all hardware breakpoints */ + + /* Disable any hardware breakpoints. */ + if (td->td_pcb->pcb_flags & PCB_DBREGS) { reset_dbregs(); - pcb->pcb_flags &= ~PCB_DBREGS; + td->td_pcb->pcb_flags &= ~PCB_DBREGS; } }