diff --git a/sys/amd64/amd64/cpu_switch.S b/sys/amd64/amd64/cpu_switch.S index 8abf262..f34b0cc 100644 --- a/sys/amd64/amd64/cpu_switch.S +++ b/sys/amd64/amd64/cpu_switch.S @@ -113,8 +113,8 @@ ENTRY(cpu_switch) movq PCB_GSBASE(%r8),%r10 testl $PCB_32BIT,PCB_FLAGS(%r8) - jnz store_gs /* static predict not taken */ -done_store_gs: + jnz store_seg +done_store_seg: testl $PCB_DBREGS,PCB_FLAGS(%r8) jnz store_dr /* static predict not taken */ @@ -176,6 +176,10 @@ sw1: testl $TDP_KTHREAD,TD_PFLAGS(%rsi) jnz do_kthread + testl $PCB_32BIT,PCB_FLAGS(%r8) + jnz load_seg +done_load_seg: + cmpq PCB_FSBASE(%r8),%r9 jz 1f /* Restore userland %fs */ @@ -184,7 +188,6 @@ sw1: movl PCB_FSBASE+4(%r8),%edx wrmsr 1: - cmpq PCB_GSBASE(%r8),%r10 jz 2f /* Restore userland %gs */ @@ -193,8 +196,8 @@ sw1: movl PCB_GSBASE+4(%r8),%edx wrmsr 2: -do_tss: +do_tss: /* Update the TSS_RSP0 pointer for the next interrupt */ movq PCPU(TSSP), %rax movq %r8, PCPU(RSP0) @@ -208,10 +211,6 @@ do_tss: jnz load_dr /* static predict not taken */ done_load_dr: - testl $PCB_32BIT,PCB_FLAGS(%r8) - jnz load_gs /* static predict not taken */ -done_load_gs: - /* Restore context. */ movq PCB_R15(%r8),%r15 movq PCB_R14(%r8),%r14 @@ -243,23 +242,35 @@ do_kthread: movq %r10,PCB_GSBASE(%r8) jmp do_tss -store_gs: +store_seg: movl %gs,PCB_GS(%r8) - movq PCB_GS32P(%r8),%rax + testl $PCB_GS32BIT,PCB_FLAGS(%r8) + jnz 2f +1: movl %ds,PCB_DS(%r8) + movl %es,PCB_ES(%r8) + movl %fs,PCB_FS(%r8) + jmp done_store_seg +2: movq PCB_GS32P(%r8),%rax movq (%rax),%rax movq %rax,PCB_GS32SD(%r8) - jmp done_store_gs + jmp 1b -load_gs: - /* Restore userland %gs while preserving kernel gsbase */ - movq PCB_GS32P(%r8),%rax - movq PCB_GS32SD(%r8),%rcx - movq %rcx,(%rax) - movl $MSR_GSBASE,%ecx +load_seg: + testl $PCB_GS32BIT,PCB_FLAGS(%r8) + jnz 2f +1: movl $MSR_GSBASE,%ecx rdmsr movl PCB_GS(%r8),%gs wrmsr - jmp done_load_gs + movl PCB_DS(%r8),%ds + movl PCB_ES(%r8),%es + movl PCB_FS(%r8),%fs + jmp done_load_seg + /* Restore userland %gs while preserving kernel gsbase */ +2: movq PCB_GS32P(%r8),%rax + movq PCB_GS32SD(%r8),%rcx + movq %rcx,(%rax) + jmp 1b store_dr: movq %dr7,%rax /* yes, do the save */ diff --git a/sys/amd64/amd64/genassym.c b/sys/amd64/amd64/genassym.c index baa1f15..fec0380 100644 --- a/sys/amd64/amd64/genassym.c +++ b/sys/amd64/amd64/genassym.c @@ -140,6 +140,7 @@ ASSYM(PCB_DR6, offsetof(struct pcb, pcb_dr6)); ASSYM(PCB_DR7, offsetof(struct pcb, pcb_dr7)); ASSYM(PCB_DBREGS, PCB_DBREGS); ASSYM(PCB_32BIT, PCB_32BIT); +ASSYM(PCB_GS32BIT, PCB_GS32BIT); ASSYM(PCB_FULLCTX, PCB_FULLCTX); ASSYM(PCB_FLAGS, offsetof(struct pcb, pcb_flags)); diff --git a/sys/amd64/ia32/ia32_reg.c b/sys/amd64/ia32/ia32_reg.c index 8abc6fc..9d31224 100644 --- a/sys/amd64/ia32/ia32_reg.c +++ b/sys/amd64/ia32/ia32_reg.c @@ -228,3 +228,10 @@ set_dbregs32(struct thread *td, struct dbreg32 *regs) dr.dr[i] = 0; return (set_dbregs(td, &dr)); } + +void +set_pcb32(struct thread *td) +{ + + td->td_pcb->pcb_flags |= PCB_32BIT; +} diff --git a/sys/amd64/include/pcb.h b/sys/amd64/include/pcb.h index 2a11598..647d5c9 100644 --- a/sys/amd64/include/pcb.h +++ b/sys/amd64/include/pcb.h @@ -70,6 +70,7 @@ struct pcb { struct savefpu pcb_save; #define PCB_DBREGS 0x02 /* process using debug registers */ #define PCB_FPUINITDONE 0x08 /* fpu state is initialized */ +#define PCB_GS32BIT 0x20 /* linux gs switch */ #define PCB_32BIT 0x40 /* process has 32 bit context (segs etc) */ #define PCB_FULLCTX 0x80 /* full context restore on sysret */ diff --git a/sys/amd64/linux32/linux32_machdep.c b/sys/amd64/linux32/linux32_machdep.c index 565e6d2..6f1401c 100644 --- a/sys/amd64/linux32/linux32_machdep.c +++ b/sys/amd64/linux32/linux32_machdep.c @@ -696,7 +696,7 @@ linux_clone(struct thread *td, struct linux_clone_args *args) td2->td_pcb->pcb_gs32sd = sd; td2->td_pcb->pcb_gs32p = &gdt[GUGS32_SEL]; td2->td_pcb->pcb_gs = GSEL(GUGS32_SEL, SEL_UPL); - td2->td_pcb->pcb_flags |= PCB_32BIT; + td2->td_pcb->pcb_flags |= PCB_GS32BIT | PCB_32BIT; } } diff --git a/sys/compat/ia32/ia32_reg.h b/sys/compat/ia32/ia32_reg.h index 5a9cdf2..c2cf639 100644 --- a/sys/compat/ia32/ia32_reg.h +++ b/sys/compat/ia32/ia32_reg.h @@ -138,4 +138,9 @@ int set_fpregs32(struct thread *, struct fpreg32 *); int fill_dbregs32(struct thread *, struct dbreg32 *); int set_dbregs32(struct thread *, struct dbreg32 *); +/* + * Mark the process pcb as belonging to 32-bit process. + */ +void set_pcb32(struct thread *); + #endif /* !_COMPAT_IA32_IA32_REG_H_ */ diff --git a/sys/ia64/ia32/ia32_reg.c b/sys/ia64/ia32/ia32_reg.c index 2066f18..10b1900 100644 --- a/sys/ia64/ia32/ia32_reg.c +++ b/sys/ia64/ia32/ia32_reg.c @@ -78,3 +78,8 @@ set_dbregs32(struct thread *td, struct dbreg32 *regs) return (EOPNOTSUPP); } + +void +set_pcb32(struct thread *td) +{ +} diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c index bec4b68..8910b8d 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -843,6 +843,10 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) imgp->auxargs = elf_auxargs; imgp->interpreted = 0; +#if defined(COMPAT_IA32) && __ELF_WORD_SIZE == 32 + set_pcb32(curthread); +#endif + /* * Try to fetch the osreldate for FreeBSD binary from the ELF * OSABI-note. Only the first page of the image is searched,