Thu Nov 30 14:03:41 2017 UTC ()
Pull up following revision(s) (requested by maxv in ticket #401):
	sys/arch/amd64/amd64/machdep.c: revision 1.267
Mmh, don't forget to clear the TLS gdt slots on Xen. Otherwise, when doing
a lwp32->lwp64 context switch, the new lwp can use the slots to reconstruct
the address of the previous lwp's TLS space (and defeat ASLR?).


(martin)
diff -r1.255.6.1 -r1.255.6.2 src/sys/arch/amd64/amd64/machdep.c

cvs diff -r1.255.6.1 -r1.255.6.2 src/sys/arch/amd64/amd64/machdep.c (expand / switch to unified diff)

--- src/sys/arch/amd64/amd64/machdep.c 2017/09/04 20:41:28 1.255.6.1
+++ src/sys/arch/amd64/amd64/machdep.c 2017/11/30 14:03:41 1.255.6.2
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: machdep.c,v 1.255.6.1 2017/09/04 20:41:28 snj Exp $ */ 1/* $NetBSD: machdep.c,v 1.255.6.2 2017/11/30 14:03:41 martin Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011 4 * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
5 * The NetBSD Foundation, Inc. 5 * The NetBSD Foundation, Inc.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * This code is derived from software contributed to The NetBSD Foundation 8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 9 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
10 * Simulation Facility, NASA Ames Research Center. 10 * Simulation Facility, NASA Ames Research Center.
11 * 11 *
12 * This code is derived from software contributed to The NetBSD Foundation 12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Coyote Point Systems, Inc. which was written under contract to Coyote 13 * by Coyote Point Systems, Inc. which was written under contract to Coyote
14 * Point by Jed Davis and Devon O'Dell. 14 * Point by Jed Davis and Devon O'Dell.
@@ -101,27 +101,27 @@ @@ -101,27 +101,27 @@
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE. 108 * SUCH DAMAGE.
109 * 109 *
110 * @(#)machdep.c 7.4 (Berkeley) 6/3/91 110 * @(#)machdep.c 7.4 (Berkeley) 6/3/91
111 */ 111 */
112 112
113#include <sys/cdefs.h> 113#include <sys/cdefs.h>
114__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.255.6.1 2017/09/04 20:41:28 snj Exp $"); 114__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.255.6.2 2017/11/30 14:03:41 martin Exp $");
115 115
116/* #define XENDEBUG_LOW */ 116/* #define XENDEBUG_LOW */
117 117
118#include "opt_modular.h" 118#include "opt_modular.h"
119#include "opt_user_ldt.h" 119#include "opt_user_ldt.h"
120#include "opt_ddb.h" 120#include "opt_ddb.h"
121#include "opt_kgdb.h" 121#include "opt_kgdb.h"
122#include "opt_cpureset_delay.h" 122#include "opt_cpureset_delay.h"
123#include "opt_mtrr.h" 123#include "opt_mtrr.h"
124#include "opt_realmem.h" 124#include "opt_realmem.h"
125#include "opt_xen.h" 125#include "opt_xen.h"
126#ifndef XEN 126#ifndef XEN
127#include "opt_physmem.h" 127#include "opt_physmem.h"
@@ -417,48 +417,51 @@ x86_64_switch_context(struct pcb *new) @@ -417,48 +417,51 @@ x86_64_switch_context(struct pcb *new)
417 HYPERVISOR_stack_switch(GSEL(GDATA_SEL, SEL_KPL), new->pcb_rsp0); 417 HYPERVISOR_stack_switch(GSEL(GDATA_SEL, SEL_KPL), new->pcb_rsp0);
418 struct physdev_op physop; 418 struct physdev_op physop;
419 physop.cmd = PHYSDEVOP_SET_IOPL; 419 physop.cmd = PHYSDEVOP_SET_IOPL;
420 physop.u.set_iopl.iopl = new->pcb_iopl; 420 physop.u.set_iopl.iopl = new->pcb_iopl;
421 HYPERVISOR_physdev_op(&physop); 421 HYPERVISOR_physdev_op(&physop);
422} 422}
423 423
424void 424void
425x86_64_tls_switch(struct lwp *l) 425x86_64_tls_switch(struct lwp *l)
426{ 426{
427 struct cpu_info *ci = curcpu(); 427 struct cpu_info *ci = curcpu();
428 struct pcb *pcb = lwp_getpcb(l); 428 struct pcb *pcb = lwp_getpcb(l);
429 struct trapframe *tf = l->l_md.md_regs; 429 struct trapframe *tf = l->l_md.md_regs;
 430 uint64_t zero = 0;
430 431
431 /* 432 /*
432 * Raise the IPL to IPL_HIGH. 433 * Raise the IPL to IPL_HIGH.
433 * FPU IPIs can alter the LWP's saved cr0. Dropping the priority 434 * FPU IPIs can alter the LWP's saved cr0. Dropping the priority
434 * is deferred until mi_switch(), when cpu_switchto() returns. 435 * is deferred until mi_switch(), when cpu_switchto() returns.
435 */ 436 */
436 (void)splhigh(); 437 (void)splhigh();
437 /* 438 /*
438 * If our floating point registers are on a different CPU, 439 * If our floating point registers are on a different CPU,
439 * set CR0_TS so we'll trap rather than reuse bogus state. 440 * set CR0_TS so we'll trap rather than reuse bogus state.
440 */ 441 */
441 if (l != ci->ci_fpcurlwp) { 442 if (l != ci->ci_fpcurlwp) {
442 HYPERVISOR_fpu_taskswitch(1); 443 HYPERVISOR_fpu_taskswitch(1);
443 } 444 }
444 445
445 /* Update TLS segment pointers */ 446 /* Update TLS segment pointers */
446 if (pcb->pcb_flags & PCB_COMPAT32) { 447 if (pcb->pcb_flags & PCB_COMPAT32) {
447 update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &pcb->pcb_fs); 448 update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &pcb->pcb_fs);
448 update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &pcb->pcb_gs); 449 update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &pcb->pcb_gs);
449 setfs(tf->tf_fs); 450 setfs(tf->tf_fs);
450 HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, tf->tf_gs); 451 HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, tf->tf_gs);
451 } else { 452 } else {
 453 update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &zero);
 454 update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &zero);
452 setfs(0); 455 setfs(0);
453 HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, 0); 456 HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, 0);
454 HYPERVISOR_set_segment_base(SEGBASE_FS, pcb->pcb_fs); 457 HYPERVISOR_set_segment_base(SEGBASE_FS, pcb->pcb_fs);
455 HYPERVISOR_set_segment_base(SEGBASE_GS_USER, pcb->pcb_gs); 458 HYPERVISOR_set_segment_base(SEGBASE_GS_USER, pcb->pcb_gs);
456 } 459 }
457} 460}
458#endif /* XEN */ 461#endif /* XEN */
459 462
460/* 463/*
461 * Set up proc0's TSS and LDT. 464 * Set up proc0's TSS and LDT.
462 */ 465 */
463void 466void
464x86_64_proc0_tss_ldt_init(void) 467x86_64_proc0_tss_ldt_init(void)