CVS commit: src/sys/arch/x86/x86

2021-07-24 Thread Jared D. McNeill
Module Name:src
Committed By:   jmcneill
Date:   Sat Jul 24 20:45:45 UTC 2021

Modified Files:
src/sys/arch/x86/x86: bios32.c

Log Message:
Build fix: vtophys takes vaddr_t, not a ptr


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/x86/x86/bios32.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/bios32.c
diff -u src/sys/arch/x86/x86/bios32.c:1.6 src/sys/arch/x86/x86/bios32.c:1.7
--- src/sys/arch/x86/x86/bios32.c:1.6	Sat Jul 24 11:39:19 2021
+++ src/sys/arch/x86/x86/bios32.c	Sat Jul 24 20:45:45 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: bios32.c,v 1.6 2021/07/24 11:39:19 jmcneill Exp $	*/
+/*	$NetBSD: bios32.c,v 1.7 2021/07/24 20:45:45 jmcneill Exp $	*/
 
 /*
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -86,7 +86,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bios32.c,v 1.6 2021/07/24 11:39:19 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bios32.c,v 1.7 2021/07/24 20:45:45 jmcneill Exp $");
 
 #include 
 #include 
@@ -252,7 +252,7 @@ smbios2_map_kva(const uint8_t *p)
 	if (eva == 0)
 		return;
 
-	smbios_entry.hdrphys = vtophys(p);
+	smbios_entry.hdrphys = vtophys((vaddr_t)p);
 	smbios_entry.tabphys = sh->addr;
 	smbios_entry.addr = (uint8_t *)(eva + (sh->addr & PGOFSET));
 	smbios_entry.len = sh->size;
@@ -287,7 +287,7 @@ smbios3_map_kva(const uint8_t *p)
 	if (eva == 0)
 		return;
 
-	smbios_entry.hdrphys = vtophys(p);
+	smbios_entry.hdrphys = vtophys((vaddr_t)p);
 	smbios_entry.tabphys = sh->addr;
 	smbios_entry.addr = (uint8_t *)(eva + ((vaddr_t)sh->addr & PGOFSET));
 	smbios_entry.len = sh->size;



CVS commit: src/sys/arch/x86/x86

2021-07-10 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Sat Jul 10 17:33:28 UTC 2021

Modified Files:
src/sys/arch/x86/x86: procfs_machdep.c

Log Message:
 Add v_spec_ctrl, avx512_fp16, sme, sev and sev_es. Tested by nonaka@.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/sys/arch/x86/x86/procfs_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/procfs_machdep.c
diff -u src/sys/arch/x86/x86/procfs_machdep.c:1.40 src/sys/arch/x86/x86/procfs_machdep.c:1.41
--- src/sys/arch/x86/x86/procfs_machdep.c:1.40	Mon Nov 30 00:04:02 2020
+++ src/sys/arch/x86/x86/procfs_machdep.c	Sat Jul 10 17:33:28 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.40 2020/11/30 00:04:02 msaitoh Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.41 2021/07/10 17:33:28 msaitoh Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.40 2020/11/30 00:04:02 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.41 2021/07/10 17:33:28 msaitoh Exp $");
 
 #include 
 #include 
@@ -82,11 +82,13 @@ static const char * const x86_features[]
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (3) Linux mapping */
-	"cxmmx", NULL, "cyrix_arr", "centaur_mcr", NULL,
-	"constant_tsc", NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
+	"cxmmx", "k6_mtrr", "cyrix_arr", "centaur_mcr", NULL, NULL, NULL, NULL,
+	"constant_tsc", "up", "art", "arch_perfmon",
+	"pebs", "bts", NULL, NULL,
+	"rep_good", NULL, NULL, "acc_power",
+	"nopl", NULL, "xtopology", "tsc_reliable",
+	"nonstop_tsc", "cpuid", "extd_apicid", "amd_dcm",
+	"aperfmperf", "rapl", "nonstop_tsc_s3", "tsc_known_freq"},
 
 	{ /* (4) Intel-defined: 0x0001 ecx */
 	"pni", "pclmulqdq", "dtes64", "monitor", "ds_cpl", "vmx", "smx", "est",
@@ -112,7 +114,7 @@ static const char * const x86_features[]
 
 	{ /* (7) Linux mapping */
 	NULL, NULL, "cpb", "ebp", NULL, "pln", "pts", "dtherm",
-	"hw_pstate", "proc_feedback", "sme", NULL,
+	"hw_pstate", "proc_feedback", NULL, NULL,
 	NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
 	NULL, "ibrs", "ibpb", "stibp", NULL, NULL, NULL, NULL},
@@ -165,12 +167,12 @@ static const char * const x86_features[]
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
 
-	{ /* (15) 0x800a edx */
+	{ /* (15) AMD 0x800a edx */
 	"npt", "lbrv", "svm_lock", "nrip_save",
 	"tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists",
 	NULL, NULL, "pausefilter", NULL, "pfthreshold", "avic", NULL,
 	"v_vmsave_vmload",
-	"vgif", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+	"vgif", NULL, NULL, NULL, "v_spec_ctrl", NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (16) 0x0007:0 ecx */
@@ -190,9 +192,15 @@ static const char * const x86_features[]
 	{ /* (18) Intel 0x0007 edx */
 	NULL, NULL, "avx512_4vnniw", "avx512_4fmaps", "fsrm", NULL, NULL, NULL,
 	"vp2intersect", NULL, "md_clear", NULL, NULL, NULL, "serialize", NULL,
-	"tsxldtrk", NULL, "pconfig", NULL, NULL, NULL, NULL, NULL,
+	"tsxldtrk", NULL, "pconfig", NULL, NULL, NULL, NULL, "avx512_fp16",
 	NULL, NULL, NULL, NULL,
 	"flush_l1d", "arch_capabilities", NULL, "ssbd"},
+
+	{ /* (19) AMD 0x801f eax */
+	"sme", "sev", NULL, "sev_es", NULL, NULL, NULL, NULL,
+	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
 };
 
 static int	procfs_getonecpu(int, struct cpu_info *, char *, size_t *);
@@ -354,6 +362,14 @@ procfs_getonecpufeatures(struct cpu_info
 		diff = last - *left;
 	}
 
+	if ((cpu_vendor == CPUVENDOR_AMD)
+	&& (ci->ci_max_ext_cpuid >= 0x8019)) {
+		x86_cpuid(0x801f, descs);
+		procfs_getonefeatreg(descs[0], x86_features[19], p + diff,
+		left);
+		diff = last - *left;
+	}
+
 	return 0; /* XXX */
 }
 



CVS commit: src/sys/arch/x86/x86

2021-06-02 Thread Nia Alarie
Module Name:src
Committed By:   nia
Date:   Wed Jun  2 06:48:10 UTC 2021

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
when warning about TSC going backwards, provide advice to the sysadmin.


To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.55 src/sys/arch/x86/x86/tsc.c:1.56
--- src/sys/arch/x86/x86/tsc.c:1.55	Tue Jun  1 21:29:24 2021
+++ src/sys/arch/x86/x86/tsc.c	Wed Jun  2 06:48:10 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.55 2021/06/01 21:29:24 riastradh Exp $	*/
+/*	$NetBSD: tsc.c,v 1.56 2021/06/02 06:48:10 nia Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.55 2021/06/01 21:29:24 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.56 2021/06/02 06:48:10 nia Exp $");
 
 #include 
 #include 
@@ -427,7 +427,8 @@ tsc_get_timecount(struct timecounter *tc
 			ticks = getticks();
 			if (ticks - lastwarn >= hz) {
 printf(
-"WARNING: TSC time went backwards by %u\n",
+"WARNING: TSC time went backwards by %u - "
+"change sysctl(7) kern.timecounter?\n",
 (unsigned)(prev - cur));
 lastwarn = ticks;
 			}



CVS commit: src/sys/arch/x86/x86

2021-05-12 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Wed May 12 22:17:40 UTC 2021

Modified Files:
src/sys/arch/x86/x86: mpacpi.c

Log Message:
In mpacpi_pci_attach_hook(), set the device handle of the PCI bus instance
to the associated ACPI handle if a device handle is not already set.

XXX This is a mess. Sure would be nice if it looked / worked more like
XXX the ARM code.


To generate a diff of this commit:
cvs rdiff -u -r1.105 -r1.106 src/sys/arch/x86/x86/mpacpi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/mpacpi.c
diff -u src/sys/arch/x86/x86/mpacpi.c:1.105 src/sys/arch/x86/x86/mpacpi.c:1.106
--- src/sys/arch/x86/x86/mpacpi.c:1.105	Sat Apr 24 23:36:51 2021
+++ src/sys/arch/x86/x86/mpacpi.c	Wed May 12 22:17:40 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: mpacpi.c,v 1.105 2021/04/24 23:36:51 thorpej Exp $	*/
+/*	$NetBSD: mpacpi.c,v 1.106 2021/05/12 22:17:40 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: mpacpi.c,v 1.105 2021/04/24 23:36:51 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mpacpi.c,v 1.106 2021/05/12 22:17:40 thorpej Exp $");
 
 #include "acpica.h"
 #include "opt_acpi.h"
@@ -90,7 +90,7 @@ ACPI_MODULE_NAME   ("mpacpi")
 #if NPCI > 0
 struct mpacpi_pcibus {
 	TAILQ_ENTRY(mpacpi_pcibus) mpr_list;
-	ACPI_HANDLE mpr_handle;		/* Same thing really, but.. */
+	devhandle_t mpr_devhandle;
 	ACPI_BUFFER mpr_buf;		/* preserve _PRT */
 	int mpr_seg;			/* PCI segment number */
 	int mpr_bus;			/* PCI bus number */
@@ -509,7 +509,7 @@ mpacpi_pci_foundbus(struct acpi_devnode 
 	}
 
 	mpr = kmem_zalloc(sizeof(struct mpacpi_pcibus), KM_SLEEP);
-	mpr->mpr_handle = ad->ad_handle;
+	mpr->mpr_devhandle = devhandle_from_acpi(ad->ad_handle);
 	mpr->mpr_buf = buf;
 	mpr->mpr_seg = ad->ad_pciinfo->ap_segment;
 	mpr->mpr_bus = ad->ad_pciinfo->ap_downbus;
@@ -953,6 +953,29 @@ mpacpi_find_interrupts(void *self)
 
 #if NPCI > 0
 
+static void
+mpacpi_set_devhandle(device_t self, struct pcibus_attach_args *pba)
+{
+	devhandle_t devhandle = device_handle(self);
+	struct mpacpi_pcibus *mpr;
+
+	/* If we already have a valid handle, eject now. */
+	if (devhandle_type(devhandle) != DEVHANDLE_TYPE_INVALID) {
+		return;
+	}
+
+	TAILQ_FOREACH(mpr, _pcibusses, mpr_list) {
+		/* XXX Assuming always segment 0 on x86. */
+		if (mpr->mpr_seg != 0) {
+			continue;
+		}
+		if (mpr->mpr_bus == pba->pba_bus) {
+			device_set_handle(self, mpr->mpr_devhandle);
+			return;
+		}
+	}
+}
+
 int
 mpacpi_pci_attach_hook(device_t parent, device_t self,
 		   struct pcibus_attach_args *pba)
@@ -984,13 +1007,16 @@ mpacpi_pci_attach_hook(device_t parent, 
 	if (mpb->mb_name != NULL) {
 		if (strcmp(mpb->mb_name, "pci"))
 			return EINVAL;
-	} else
+	} else {
 		/*
 		 * As we cannot find all PCI-to-PCI bridge in
 		 * mpacpi_find_pcibusses, some of the MP_busses may remain
 		 * uninitialized.
 		 */
 		mpb->mb_name = "pci";
+	}
+
+	mpacpi_set_devhandle(self, pba);
 
 	mpb->mb_dev = self;
 	mpb->mb_pci_bridge_tag = pba->pba_bridgetag;



CVS commit: src/sys/arch/x86/x86

2021-04-17 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Sat Apr 17 18:03:21 UTC 2021

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Make pat_init() a NOOP on XENPV; it causes a trap with Xen 4.15


To generate a diff of this commit:
cvs rdiff -u -r1.409 -r1.410 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.409 src/sys/arch/x86/x86/pmap.c:1.410
--- src/sys/arch/x86/x86/pmap.c:1.409	Sat Feb  6 21:24:19 2021
+++ src/sys/arch/x86/x86/pmap.c	Sat Apr 17 18:03:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.409 2021/02/06 21:24:19 jdolecek Exp $	*/
+/*	$NetBSD: pmap.c,v 1.410 2021/04/17 18:03:21 bouyer Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.409 2021/02/06 21:24:19 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.410 2021/04/17 18:03:21 bouyer Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -915,6 +915,7 @@ pmap_exec_fixup(struct vm_map *map, stru
 void
 pat_init(struct cpu_info *ci)
 {
+#ifndef XENPV
 	uint64_t pat;
 
 	if (!(ci->ci_feat_val[0] & CPUID_PAT))
@@ -928,6 +929,7 @@ pat_init(struct cpu_info *ci)
 
 	wrmsr(MSR_CR_PAT, pat);
 	cpu_pat_enabled = true;
+#endif
 }
 
 static pt_entry_t



CVS commit: src/sys/arch/x86/x86

2021-04-11 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Apr 12 02:23:02 UTC 2021

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
make a numeric literal unsigned as it is bit-negated.


To generate a diff of this commit:
cvs rdiff -u -r1.120 -r1.121 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.120 src/sys/arch/x86/x86/identcpu.c:1.121
--- src/sys/arch/x86/x86/identcpu.c:1.120	Sat Mar  6 19:16:45 2021
+++ src/sys/arch/x86/x86/identcpu.c	Mon Apr 12 02:23:01 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.120 2021/03/06 19:16:45 bouyer Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.121 2021/04/12 02:23:01 mrg Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.120 2021/03/06 19:16:45 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.121 2021/04/12 02:23:01 mrg Exp $");
 
 #include "opt_xen.h"
 
@@ -509,7 +509,7 @@ cpu_probe_cyrix_cmn(struct cpu_info *ci)
 	/* cyrix's workaround  for the "coma bug" */
 	cyrix_write_reg(0x31, cyrix_read_reg(0x31) | 0xf8);
 	cyrix_write_reg(0x32, cyrix_read_reg(0x32) | 0x7f);
-	cyrix_write_reg(0x33, cyrix_read_reg(0x33) & ~0xff);
+	cyrix_write_reg(0x33, cyrix_read_reg(0x33) & ~0xffu);
 	cyrix_write_reg(0x3c, cyrix_read_reg(0x3c) | 0x87);
 	/* disable access to ccr4/ccr5 */
 	cyrix_write_reg(0xC3, c3);



CVS commit: src/sys/arch/x86/x86

2021-03-06 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Sat Mar  6 19:16:45 UTC 2021

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
return early from identify_hypervisor() if we already know we're running
Xen PV or PVH, as this was before 1.119.
Trying to read the BIOS faults (as expected, as there's no BIOS in this case).
Problem pointed out and fix tested by Brian Marcotte


To generate a diff of this commit:
cvs rdiff -u -r1.119 -r1.120 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.119 src/sys/arch/x86/x86/identcpu.c:1.120
--- src/sys/arch/x86/x86/identcpu.c:1.119	Fri Feb 19 02:15:24 2021
+++ src/sys/arch/x86/x86/identcpu.c	Sat Mar  6 19:16:45 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.119 2021/02/19 02:15:24 christos Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.120 2021/03/06 19:16:45 bouyer Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.119 2021/02/19 02:15:24 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.120 2021/03/06 19:16:45 bouyer Exp $");
 
 #include "opt_xen.h"
 
@@ -1135,18 +1135,14 @@ identify_hypervisor(void)
 	const char *p;
 	int i;
 
-#if 0	
-	/* 
-	 * This is called from cpu_probe() and cpu_configure()
-	 * During cpu_probe() we have not called platform_init()
-	 * yet, so the bios tables have not been loaded.
-	 * We allow this to be called twice in order to override
-	 * the cpuid setting because some hypervisors don't return
-	 * specific enough info with cpuid it.
-	 */
-	if (vm_guest != VM_GUEST_NO)
+	switch(vm_guest) {
+	case VM_GUEST_XENPV:
+	case VM_GUEST_XENPVH:
+		/* guest type already known, no bios info */
 		return;
-#endif
+	default:
+		break;
+	}
 
 	/*
 	 * [RFC] CPUID usage for interaction between Hypervisors and Linux.



CVS commit: src/sys/arch/x86/x86

2021-02-18 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Feb 19 05:34:37 UTC 2021

Modified Files:
src/sys/arch/x86/x86: intr.c

Log Message:
Fix x86's pci_intr_disestablish clean up routine.  Pointed out by t-kusaba@IIJ, 
thanks.

Fix panic on x86 by the following code.

sc_ih = pci_intr_establish_xname(sc_pc, ...);
pci_intr_disestablish(sc_pc, sc_ih);
sc_ih = pci_intr_establish(sc_pc, ...);

ena(4) do such processing when ifconfig down/up.

XXX pullup-8,9


To generate a diff of this commit:
cvs rdiff -u -r1.153 -r1.154 src/sys/arch/x86/x86/intr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/intr.c
diff -u src/sys/arch/x86/x86/intr.c:1.153 src/sys/arch/x86/x86/intr.c:1.154
--- src/sys/arch/x86/x86/intr.c:1.153	Wed Nov 18 16:36:43 2020
+++ src/sys/arch/x86/x86/intr.c	Fri Feb 19 05:34:37 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.153 2020/11/18 16:36:43 bouyer Exp $	*/
+/*	$NetBSD: intr.c,v 1.154 2021/02/19 05:34:37 knakahara Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.153 2020/11/18 16:36:43 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.154 2021/02/19 05:34:37 knakahara Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -636,6 +636,9 @@ intr_source_free(struct cpu_info *ci, in
 	ci->ci_isources[slot] = NULL;
 	if (pic != _pic)
 		idt_vec_free(iv, idtvec);
+
+	isp->is_recurse = NULL;
+	isp->is_resume = NULL;
 }
 
 #ifdef MULTIPROCESSOR
@@ -1136,12 +1139,12 @@ intr_disestablish_xcall(void *arg1, void
 	else if (source->is_mask_count == 0)
 		(*pic->pic_hwunmask)(pic, ih->ih_pin);
 
-	/* Re-enable interrupts. */
-	x86_write_psl(psl);
-
 	/* If the source is free we can drop it now. */
 	intr_source_free(ci, ih->ih_slot, pic, idtvec);
 
+	/* Re-enable interrupts. */
+	x86_write_psl(psl);
+
 	DPRINTF(("%s: remove slot %d (pic %s pin %d vec %d)\n",
 	device_xname(ci->ci_dev), ih->ih_slot, pic->pic_name,
 	ih->ih_pin, idtvec));



CVS commit: src/sys/arch/x86/x86

2021-02-18 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Feb 19 03:28:53 UTC 2021

Modified Files:
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
It is not VirtualBo give some more space.


To generate a diff of this commit:
cvs rdiff -u -r1.147 -r1.148 src/sys/arch/x86/x86/x86_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.147 src/sys/arch/x86/x86/x86_machdep.c:1.148
--- src/sys/arch/x86/x86/x86_machdep.c:1.147	Thu Feb 18 21:18:09 2021
+++ src/sys/arch/x86/x86/x86_machdep.c	Thu Feb 18 22:28:53 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.147 2021/02/19 02:18:09 christos Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.148 2021/02/19 03:28:53 christos Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.147 2021/02/19 02:18:09 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.148 2021/02/19 03:28:53 christos Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -1329,7 +1329,7 @@ sysctl_machdep_hypervisor(SYSCTLFN_ARGS)
 {
 	struct sysctlnode node;
 	const char *t = NULL;
-	char buf[10];
+	char buf[64];
 
 	node = *rnode;
 	node.sysctl_data = buf;



CVS commit: src/sys/arch/x86/x86

2021-02-18 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Feb 19 02:18:10 UTC 2021

Modified Files:
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
add VirtualBox


To generate a diff of this commit:
cvs rdiff -u -r1.146 -r1.147 src/sys/arch/x86/x86/x86_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.146 src/sys/arch/x86/x86/x86_machdep.c:1.147
--- src/sys/arch/x86/x86/x86_machdep.c:1.146	Sun Aug  9 11:32:44 2020
+++ src/sys/arch/x86/x86/x86_machdep.c	Thu Feb 18 21:18:09 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.146 2020/08/09 15:32:44 christos Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.147 2021/02/19 02:18:09 christos Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.146 2020/08/09 15:32:44 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.147 2021/02/19 02:18:09 christos Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -1321,6 +1321,7 @@ static const char * const vm_guest_name[
 	[VM_GUEST_HV] =		"Hyper-V",
 	[VM_GUEST_VMWARE] =	"VMware",
 	[VM_GUEST_KVM] =	"KVM",
+	[VM_GUEST_VIRTUALBOX] =	"VirtualBox",
 };
 
 static int



CVS commit: src/sys/arch/x86/x86

2021-02-18 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Feb 19 02:15:58 UTC 2021

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
Penalize TSC on VirtualBox because it is not accurate enough.


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.53 src/sys/arch/x86/x86/tsc.c:1.54
--- src/sys/arch/x86/x86/tsc.c:1.53	Wed Feb 17 01:33:47 2021
+++ src/sys/arch/x86/x86/tsc.c	Thu Feb 18 21:15:58 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.53 2021/02/17 06:33:47 rillig Exp $	*/
+/*	$NetBSD: tsc.c,v 1.54 2021/02/19 02:15:58 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.53 2021/02/17 06:33:47 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.54 2021/02/19 02:15:58 christos Exp $");
 
 #include 
 #include 
@@ -224,6 +224,8 @@ tsc_tc_init(void)
 		invariant = false;
 	} else if (vm_guest == VM_GUEST_NO) {
 		delay_func = tsc_delay;
+	} else if (vm_guest == VM_GUEST_VIRTUALBOX) {
+		tsc_timecounter.tc_quality = -100;
 	}
 
 	if (tsc_freq != 0) {



CVS commit: src/sys/arch/x86/x86

2021-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Feb 17 06:33:48 UTC 2021

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
x86/tsc: fix double space in warning about TSC going backwards


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.52 src/sys/arch/x86/x86/tsc.c:1.53
--- src/sys/arch/x86/x86/tsc.c:1.52	Mon Jun 15 20:27:30 2020
+++ src/sys/arch/x86/x86/tsc.c	Wed Feb 17 06:33:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.52 2020/06/15 20:27:30 riastradh Exp $	*/
+/*	$NetBSD: tsc.c,v 1.53 2021/02/17 06:33:47 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.52 2020/06/15 20:27:30 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.53 2021/02/17 06:33:47 rillig Exp $");
 
 #include 
 #include 
@@ -424,8 +424,9 @@ tsc_get_timecount(struct timecounter *tc
 		__cpu_simple_lock_try()) {
 			ticks = getticks();
 			if (ticks - lastwarn >= hz) {
-printf("WARNING: TSC time went backwards "
-" by %u\n", (unsigned)(prev - cur));
+printf(
+"WARNING: TSC time went backwards by %u\n",
+(unsigned)(prev - cur));
 lastwarn = ticks;
 			}
 			__cpu_simple_unlock();



CVS commit: src/sys/arch/x86/x86

2021-02-06 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Sat Feb  6 21:24:20 UTC 2021

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
use __builtin_assume_aligned() on places where the memset() or memcpy()
parameters are known to be PAGE_SIZE-aligned, so compiler doesn't need
to emit atrocious alignment check code when inlining them

this particularly improves pmap_zero_page() and pmap_copy_page(),
which are now reduced to only 'rep stosq', and close to what a hand-written
assembly would do

Note: on CPUs supporting ERMS, 'rep stosb' would still be slightly faster, but
this is a solid stop-gap improvement

suggested by Mateusz Guzik in:
http://mail-index.netbsd.org/tech-kern/2020/07/19/msg026620.html


To generate a diff of this commit:
cvs rdiff -u -r1.408 -r1.409 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.408 src/sys/arch/x86/x86/pmap.c:1.409
--- src/sys/arch/x86/x86/pmap.c:1.408	Mon Nov 30 17:06:02 2020
+++ src/sys/arch/x86/x86/pmap.c	Sat Feb  6 21:24:19 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.408 2020/11/30 17:06:02 bouyer Exp $	*/
+/*	$NetBSD: pmap.c,v 1.409 2021/02/06 21:24:19 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.408 2020/11/30 17:06:02 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.409 2021/02/06 21:24:19 jdolecek Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -339,6 +339,9 @@ paddr_t pmap_pa_end;   /* PA of last phy
 #define	PMAP_CHECK_PP(pp) \
 KASSERTMSG((pp)->pp_lock.mtx_ipl._ipl == IPL_VM, "bad pmap_page %p", pp)
 
+#define PAGE_ALIGNED(pp)	\
+	__builtin_assume_aligned((void *)(pp), PAGE_SIZE)
+
 /*
  * Other data structures
  */
@@ -1297,7 +1300,7 @@ pmap_bootstrap(vaddr_t kva_start)
 	xen_dummy_user_pgd = xen_dummy_page - KERNBASE;
 
 	/* Zero fill it, the less checks in Xen it requires the better */
-	memset((void *)(xen_dummy_user_pgd + KERNBASE), 0, PAGE_SIZE);
+	memset(PAGE_ALIGNED(xen_dummy_user_pgd + KERNBASE), 0, PAGE_SIZE);
 	/* Mark read-only */
 	HYPERVISOR_update_va_mapping(xen_dummy_user_pgd + KERNBASE,
 	pmap_pa2pte(xen_dummy_user_pgd) | PTE_P | pmap_pg_nx,
@@ -1542,7 +1545,7 @@ pmap_init_pcpu(void)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		memset((void *)tmpva, 0, PAGE_SIZE);
+		memset(PAGE_ALIGNED(tmpva), 0, PAGE_SIZE);
 
 		L4_BASE[L4e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1556,7 +1559,7 @@ pmap_init_pcpu(void)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		memset((void *)tmpva, 0, PAGE_SIZE);
+		memset(PAGE_ALIGNED(tmpva), 0, PAGE_SIZE);
 
 		L3_BASE[L3e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1571,7 +1574,7 @@ pmap_init_pcpu(void)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		memset((void *)tmpva, 0, PAGE_SIZE);
+		memset(PAGE_ALIGNED(tmpva), 0, PAGE_SIZE);
 
 		L2_BASE[L2e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1663,7 +1666,7 @@ pmap_init_directmap(struct pmap *kpm)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		memset((void *)tmpva, 0, PAGE_SIZE);
+		memset(PAGE_ALIGNED(tmpva), 0, PAGE_SIZE);
 
 		L4_BASE[L4e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1677,7 +1680,7 @@ pmap_init_directmap(struct pmap *kpm)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		memset((void *)tmpva, 0, PAGE_SIZE);
+		memset(PAGE_ALIGNED(tmpva), 0, PAGE_SIZE);
 
 		L3_BASE[L3e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -2627,7 +2630,7 @@ pmap_pdp_init(pd_entry_t *pdir)
 	int s;
 #endif
 
-	memset(pdir, 0, PDP_SIZE * PAGE_SIZE);
+	memset(PAGE_ALIGNED(pdir), 0, PDP_SIZE * PAGE_SIZE);
 
 	/*
 	 * NOTE: This is all done unlocked, but we will check afterwards
@@ -3805,7 +3808,7 @@ void
 pmap_zero_page(paddr_t pa)
 {
 #if defined(__HAVE_DIRECT_MAP)
-	memset((void *)PMAP_DIRECT_MAP(pa), 0, PAGE_SIZE);
+	memset(PAGE_ALIGNED(PMAP_DIRECT_MAP(pa)), 0, PAGE_SIZE);
 #else
 #if defined(XENPV)
 	if (XEN_VERSION_SUPPORTED(3, 4))
@@ -3829,7 +3832,7 @@ pmap_zero_page(paddr_t pa)
 	pmap_pte_flush();
 	pmap_update_pg(zerova);		/* flush TLB */
 
-	memset((void *)zerova, 0, PAGE_SIZE);
+	memset(PAGE_ALIGNED(zerova), 0, PAGE_SIZE);
 
 #if defined(DIAGNOSTIC) || defined(XENPV)
 	pmap_pte_set(zpte, 0);/* zap ! */
@@ -3847,7 +3850,7 @@ pmap_copy_page(paddr_t srcpa, paddr_t ds
 	vaddr_t srcva = PMAP_DIRECT_MAP(srcpa);
 	vaddr_t dstva = PMAP_DIRECT_MAP(dstpa);
 
-	memcpy((void *)dstva, (void *)srcva, PAGE_SIZE);
+	memcpy(PAGE_ALIGNED(dstva), PAGE_ALIGNED(srcva), PAGE_SIZE);
 #else
 #if defined(XENPV)
 	if (XEN_VERSION_SUPPORTED(3, 4)) {
@@ -3877,7 +3880,7 

CVS commit: src/sys/arch/x86/x86

2021-01-16 Thread Jared D. McNeill
Module Name:src
Committed By:   jmcneill
Date:   Sat Jan 16 15:26:23 UTC 2021

Modified Files:
src/sys/arch/x86/x86: identcpu_subr.c

Log Message:
trailing whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/x86/x86/identcpu_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu_subr.c
diff -u src/sys/arch/x86/x86/identcpu_subr.c:1.7 src/sys/arch/x86/x86/identcpu_subr.c:1.8
--- src/sys/arch/x86/x86/identcpu_subr.c:1.7	Fri Jul 10 06:15:23 2020
+++ src/sys/arch/x86/x86/identcpu_subr.c	Sat Jan 16 15:26:23 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: identcpu_subr.c,v 1.7 2020/07/10 06:15:23 msaitoh Exp $ */
+/* $NetBSD: identcpu_subr.c,v 1.8 2021/01/16 15:26:23 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  * See src/usr.sbin/cpuctl/{Makefile, arch/i386.c}).
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.7 2020/07/10 06:15:23 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.8 2021/01/16 15:26:23 jmcneill Exp $");
 
 #ifdef _KERNEL_OPT
 #include "lapic.h"
@@ -69,7 +69,7 @@ cpu_tsc_freq_cpuid(struct cpu_info *ci)
 
 	if (!((ci->ci_max_cpuid >= 0x15) && (cpu_vendor == CPUVENDOR_INTEL)))
 		return 0;
-		
+
 	x86_cpuid(0x15, descs);
 	denominator = descs[0];
 	numerator = descs[1];



CVS commit: src/sys/arch/x86/x86

2020-11-29 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Mon Nov 30 00:04:03 UTC 2020

Modified Files:
src/sys/arch/x86/x86: procfs_machdep.c

Log Message:
 Add sgx, sgx_lc, serialize and tsxldtrk.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/sys/arch/x86/x86/procfs_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/procfs_machdep.c
diff -u src/sys/arch/x86/x86/procfs_machdep.c:1.39 src/sys/arch/x86/x86/procfs_machdep.c:1.40
--- src/sys/arch/x86/x86/procfs_machdep.c:1.39	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/procfs_machdep.c	Mon Nov 30 00:04:02 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.39 2020/04/25 15:26:18 bouyer Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.40 2020/11/30 00:04:02 msaitoh Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.39 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.40 2020/11/30 00:04:02 msaitoh Exp $");
 
 #include 
 #include 
@@ -126,7 +126,7 @@ static const char * const x86_features[]
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (9) Intel-defined: 0007 ebx */
-	"fsgsbase", "tsc_adjust", NULL, "bmi1", "hle", "avx2", NULL, "smep",
+	"fsgsbase", "tsc_adjust", "sgx", "bmi1", "hle", "avx2", NULL, "smep",
 	"bmi2", "erms", "invpcid", "rtm", "cqm", NULL, "mpx", "rdt_a",
 	"avx512f", "avx512dq", "rdseed", "adx",
 	"smap", "avx512ifma", NULL, "clflushopt",
@@ -179,7 +179,7 @@ static const char * const x86_features[]
 	"gfni", "vaes", "vpclmulqdq", "avx512_vnni",
 	"avx512_bitalg", "tme", "avx512_vpopcntdq", NULL,
 	"la57", NULL, NULL, NULL, NULL, NULL, "rdpid", NULL,
-	NULL, "cldemote", NULL, "movdiri", "movdir64b", NULL, NULL, NULL},
+	NULL, "cldemote", NULL, "movdiri", "movdir64b", NULL, "sgx_lc", NULL},
 
 	{ /* (17) 0x8007 ebx */
 	"overflow_recov", "succor", NULL, "smca", NULL, NULL, NULL, NULL,
@@ -189,8 +189,8 @@ static const char * const x86_features[]
 
 	{ /* (18) Intel 0x0007 edx */
 	NULL, NULL, "avx512_4vnniw", "avx512_4fmaps", "fsrm", NULL, NULL, NULL,
-	"vp2intersect", NULL, "md_clear", NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, "pconfig", NULL, NULL, NULL, NULL, NULL,
+	"vp2intersect", NULL, "md_clear", NULL, NULL, NULL, "serialize", NULL,
+	"tsxldtrk", NULL, "pconfig", NULL, NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL,
 	"flush_l1d", "arch_capabilities", NULL, "ssbd"},
 };



CVS commit: src/sys/arch/x86/x86

2020-11-18 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Nov 18 16:36:43 UTC 2020

Modified Files:
src/sys/arch/x86/x86: intr.c

Log Message:
Preserve Xen SIR slots for VM_GUEST_XENPVH.


To generate a diff of this commit:
cvs rdiff -u -r1.152 -r1.153 src/sys/arch/x86/x86/intr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/intr.c
diff -u src/sys/arch/x86/x86/intr.c:1.152 src/sys/arch/x86/x86/intr.c:1.153
--- src/sys/arch/x86/x86/intr.c:1.152	Tue Jul 14 00:45:53 2020
+++ src/sys/arch/x86/x86/intr.c	Wed Nov 18 16:36:43 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.152 2020/07/14 00:45:53 yamaguchi Exp $	*/
+/*	$NetBSD: intr.c,v 1.153 2020/11/18 16:36:43 bouyer Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.152 2020/07/14 00:45:53 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.153 2020/11/18 16:36:43 bouyer Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -450,16 +450,20 @@ intr_allocate_slot_cpu(struct cpu_info *
 		slot = pin;
 	} else {
 		int start = 0;
+		int max = MAX_INTR_SOURCES;
 		slot = -1;
 
 		/* avoid reserved slots for legacy interrupts. */
 		if (CPU_IS_PRIMARY(ci) && msipic_is_msi_pic(pic))
 			start = NUM_LEGACY_IRQS;
+		/* don't step over Xen's slots */
+		if (vm_guest == VM_GUEST_XENPVH)
+			max = SIR_XENIPL_VM; 
 		/*
 		 * intr_allocate_slot has checked for an existing mapping.
 		 * Now look for a free slot.
 		 */
-		for (i = start; i < MAX_INTR_SOURCES ; i++) {
+		for (i = start; i < max ; i++) {
 			if (ci->ci_isources[i] == NULL) {
 slot = i;
 break;



CVS commit: src/sys/arch/x86/x86

2020-10-15 Thread Michał Górny
Module Name:src
Committed By:   mgorny
Date:   Thu Oct 15 17:43:30 UTC 2020

Modified Files:
src/sys/arch/x86/x86: convert_xmm_s87.c

Log Message:
Remove unnecessary  include


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/x86/x86/convert_xmm_s87.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/convert_xmm_s87.c
diff -u src/sys/arch/x86/x86/convert_xmm_s87.c:1.6 src/sys/arch/x86/x86/convert_xmm_s87.c:1.7
--- src/sys/arch/x86/x86/convert_xmm_s87.c:1.6	Thu Oct 15 17:43:08 2020
+++ src/sys/arch/x86/x86/convert_xmm_s87.c	Thu Oct 15 17:43:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: convert_xmm_s87.c,v 1.6 2020/10/15 17:43:08 mgorny Exp $	*/
+/*	$NetBSD: convert_xmm_s87.c,v 1.7 2020/10/15 17:43:30 mgorny Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2000, 2001, 2008 The NetBSD Foundation, Inc.
@@ -30,11 +30,10 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: convert_xmm_s87.c,v 1.6 2020/10/15 17:43:08 mgorny Exp $");
+__KERNEL_RCSID(0, "$NetBSD: convert_xmm_s87.c,v 1.7 2020/10/15 17:43:30 mgorny Exp $");
 
 
 #include 
-#include 
 #include 
 
 void



CVS commit: src/sys/arch/x86/x86

2020-10-15 Thread Michał Górny
Module Name:src
Committed By:   mgorny
Date:   Thu Oct 15 17:42:31 UTC 2020

Added Files:
src/sys/arch/x86/x86: convert_xmm_s87.c

Log Message:
Revert "Merge convert_xmm_s87.c into fpu.c"

I am going to add ATF tests for these two functions, and having them
in a separate file will make it more convenient to build and run them
in userspace.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.5 src/sys/arch/x86/x86/convert_xmm_s87.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: src/sys/arch/x86/x86/convert_xmm_s87.c
diff -u /dev/null src/sys/arch/x86/x86/convert_xmm_s87.c:1.5
--- /dev/null	Thu Oct 15 17:42:31 2020
+++ src/sys/arch/x86/x86/convert_xmm_s87.c	Thu Oct 15 17:42:31 2020
@@ -0,0 +1,156 @@
+/*	$NetBSD: convert_xmm_s87.c,v 1.5 2020/10/15 17:42:31 mgorny Exp $	*/
+
+/*-
+ * Copyright (c) 1998, 2000, 2001, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Charles M. Hannum; by Jason R. Thorpe of Wasabi Systems, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include 
+__KERNEL_RCSID(0, "$NetBSD: convert_xmm_s87.c,v 1.5 2020/10/15 17:42:31 mgorny Exp $");
+
+
+#include 
+#include 
+#include 
+
+void
+process_xmm_to_s87(const struct fxsave *sxmm, struct save87 *s87)
+{
+	unsigned int tag, ab_tag;
+	const struct fpaccfx *fx_reg;
+	struct fpacc87 *s87_reg;
+	int i;
+
+	/*
+	 * For historic reasons core dumps and ptrace all use the old save87
+	 * layout.  Convert the important parts.
+	 * getucontext gets what we give it.
+	 * setucontext should return something given by getucontext, but
+	 * we are (at the moment) willing to change it.
+	 *
+	 * It really isn't worth setting the 'tag' bits to 01 (zero) or
+	 * 10 (NaN etc) since the processor will set any internal bits
+	 * correctly when the value is loaded (the 287 believed them).
+	 *
+	 * Additionally the s87_tw and s87_tw are 'indexed' by the actual
+	 * register numbers, whereas the registers themselves have ST(0)
+	 * first. Pairing the values and tags can only be done with
+	 * reference to the 'top of stack'.
+	 *
+	 * If any x87 registers are used, they will typically be from
+	 * r7 downwards - so the high bits of the tag register indicate
+	 * used registers. The conversions are not optimised for this.
+	 *
+	 * The ABI we use requires the FP stack to be empty on every
+	 * function call. I think this means that the stack isn't expected
+	 * to overflow - overflow doesn't drop a core in my testing.
+	 *
+	 * Note that this code writes to all of the 's87' structure that
+	 * actually gets written to userspace.
+	 */
+
+	/* FPU control/status */
+	s87->s87_cw = sxmm->fx_cw;
+	s87->s87_sw = sxmm->fx_sw;
+	/* tag word handled below */
+	s87->s87_ip = sxmm->fx_ip;
+	s87->s87_opcode = sxmm->fx_opcode;
+	s87->s87_dp = sxmm->fx_dp;
+
+	/* FP registers (in stack order) */
+	fx_reg = sxmm->fx_87_ac;
+	s87_reg = s87->s87_ac;
+	for (i = 0; i < 8; fx_reg++, s87_reg++, i++)
+		*s87_reg = fx_reg->r;
+
+	/* Tag word and registers. */
+	ab_tag = sxmm->fx_tw & 0xff;	/* Bits set if valid */
+	if (ab_tag == 0) {
+		/* none used */
+		s87->s87_tw = 0x;
+		return;
+	}
+
+	tag = 0;
+	/* Separate bits of abridged tag word with zeros */
+	for (i = 0x80; i != 0; tag <<= 1, i >>= 1)
+		tag |= ab_tag & i;
+	/* Replicate and invert so that 0 => 0b11 and 1 => 0b00 */
+	s87->s87_tw = (tag | tag >> 1) ^ 0x;
+}
+
+void
+process_s87_to_xmm(const struct save87 *s87, struct fxsave *sxmm)
+{
+	unsigned int tag, ab_tag;
+	struct fpaccfx *fx_reg;
+	const struct fpacc87 *s87_reg;
+	int i;
+
+	/*
+	 

CVS commit: src/sys/arch/x86/x86

2020-10-12 Thread Ryo ONODERA
Module Name:src
Committed By:   ryoon
Date:   Mon Oct 12 12:11:03 UTC 2020

Modified Files:
src/sys/arch/x86/x86: hyperv.c

Log Message:
Fix typo in comment


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/x86/x86/hyperv.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/hyperv.c
diff -u src/sys/arch/x86/x86/hyperv.c:1.11 src/sys/arch/x86/x86/hyperv.c:1.12
--- src/sys/arch/x86/x86/hyperv.c:1.11	Tue Jul 14 00:45:53 2020
+++ src/sys/arch/x86/x86/hyperv.c	Mon Oct 12 12:11:03 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: hyperv.c,v 1.11 2020/07/14 00:45:53 yamaguchi Exp $	*/
+/*	$NetBSD: hyperv.c,v 1.12 2020/10/12 12:11:03 ryoon Exp $	*/
 
 /*-
  * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
@@ -29,11 +29,11 @@
  */
 
 /**
- * Implements low-level interactions with Hyper-V/Azuree
+ * Implements low-level interactions with Hyper-V/Azure
  */
 #include 
 #ifdef __KERNEL_RCSID
-__KERNEL_RCSID(0, "$NetBSD: hyperv.c,v 1.11 2020/07/14 00:45:53 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: hyperv.c,v 1.12 2020/10/12 12:11:03 ryoon Exp $");
 #endif
 #ifdef __FBSDID
 __FBSDID("$FreeBSD: head/sys/dev/hyperv/vmbus/hyperv.c 331757 2018-03-30 02:25:12Z emaste $");



CVS commit: src/sys/arch/x86/x86

2020-10-09 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Oct  9 21:14:05 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu.c

Log Message:
Don't do extra work finding the power of 2 for values we are not going to
use. Explain that cpu_hatch has not been called yet, so no cpu_probe either
so the cache info is 0 for AP's.


To generate a diff of this commit:
cvs rdiff -u -r1.198 -r1.199 src/sys/arch/x86/x86/cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.198 src/sys/arch/x86/x86/cpu.c:1.199
--- src/sys/arch/x86/x86/cpu.c:1.198	Sun Aug  9 11:32:44 2020
+++ src/sys/arch/x86/x86/cpu.c	Fri Oct  9 17:14:05 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.198 2020/08/09 15:32:44 christos Exp $	*/
+/*	$NetBSD: cpu.c,v 1.199 2020/10/09 21:14:05 christos Exp $	*/
 
 /*
  * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.198 2020/08/09 15:32:44 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.199 2020/10/09 21:14:05 christos Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -274,11 +274,17 @@ cpu_pcpuarea_init(struct cpu_info *ci)
 static void
 cpu_vm_init(struct cpu_info *ci)
 {
-	int ncolors = 2, i;
+	unsigned int ncolors = 2;
 
-	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
+	/*
+	 * XXX: for AP's the cache info has not been initialized yet
+	 * but that does not matter because uvm only pays attention at
+	 * the maximum only. We should fix it once cpus have different
+	 * cache sizes.
+	 */
+	for (unsigned int i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
 		struct x86_cache_info *cai;
-		int tcolors;
+		unsigned int tcolors;
 
 		cai = >ci_cinfo[i];
 
@@ -293,24 +299,27 @@ cpu_vm_init(struct cpu_info *ci)
 		default:
 			tcolors /= cai->cai_associativity;
 		}
-		ncolors = uimax(ncolors, tcolors);
-		/*
-		 * If the desired number of colors is not a power of
-		 * two, it won't be good.  Find the greatest power of
-		 * two which is an even divisor of the number of colors,
-		 * to preserve even coloring of pages.
-		 */
-		if (ncolors & (ncolors - 1) ) {
-			int try, picked = 1;
-			for (try = 1; try < ncolors; try *= 2) {
-if (ncolors % try == 0) picked = try;
-			}
-			if (picked == 1) {
-panic("desired number of cache colors %d is "
-" > 1, but not even!", ncolors);
-			}
-			ncolors = picked;
+		if (tcolors <= ncolors)
+			continue;
+		ncolors = tcolors;
+	}
+
+	/*
+	 * If the desired number of colors is not a power of
+	 * two, it won't be good.  Find the greatest power of
+	 * two which is an even divisor of the number of colors,
+	 * to preserve even coloring of pages.
+	 */
+	if (ncolors & (ncolors - 1) ) {
+		unsigned int try, picked = 1;
+		for (try = 1; try < ncolors; try *= 2) {
+			if (ncolors % try == 0) picked = try;
+		}
+		if (picked == 1) {
+			panic("desired number of cache colors %u is "
+			" > 1, but not even!", ncolors);
 		}
+		ncolors = picked;
 	}
 
 	/*



CVS commit: src/sys/arch/x86/x86

2020-09-02 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Sep  2 17:37:57 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
pmap_enter_gnt():
An empty PTP has a wire_count of 1, so KASSERT > 1 if we're sure we have
at last one entry.


To generate a diff of this commit:
cvs rdiff -u -r1.405 -r1.406 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.405 src/sys/arch/x86/x86/pmap.c:1.406
--- src/sys/arch/x86/x86/pmap.c:1.405	Wed Sep  2 17:07:45 2020
+++ src/sys/arch/x86/x86/pmap.c	Wed Sep  2 17:37:57 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.405 2020/09/02 17:07:45 bouyer Exp $	*/
+/*	$NetBSD: pmap.c,v 1.406 2020/09/02 17:37:57 bouyer Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.405 2020/09/02 17:07:45 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.406 2020/09/02 17:37:57 bouyer Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -5263,7 +5263,7 @@ pmap_enter_gnt(struct pmap *pmap, vaddr_
 		if (!have_oldpa) {
 			ptp->wire_count++;
 		}
-		KASSERT(ptp->wire_count >= 1);
+		KASSERT(ptp->wire_count > 1);
 		/* Remember minimum VA in PTP. */
 		pmap_ptp_range_set(ptp, va);
 	}



CVS commit: src/sys/arch/x86/x86

2020-09-02 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Sep  2 17:07:45 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
pmap_enter_gnt(): call pmap_free_ptp() if needed. We can have a 0 wire count
if we had an old mapping and grant map hypercall failed, and this was the
only page in this ptp.
while there remove ptp != NULL checks for gnt operations: we always have
a ptp here.


To generate a diff of this commit:
cvs rdiff -u -r1.404 -r1.405 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.404 src/sys/arch/x86/x86/pmap.c:1.405
--- src/sys/arch/x86/x86/pmap.c:1.404	Tue Sep  1 11:24:14 2020
+++ src/sys/arch/x86/x86/pmap.c	Wed Sep  2 17:07:45 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.404 2020/09/01 11:24:14 bouyer Exp $	*/
+/*	$NetBSD: pmap.c,v 1.405 2020/09/02 17:07:45 bouyer Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.404 2020/09/01 11:24:14 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.405 2020/09/02 17:07:45 bouyer Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -5255,21 +5255,20 @@ pmap_enter_gnt(struct pmap *pmap, vaddr_
 	if (__predict_false(op->status != GNTST_okay)) {
 		printf("%s: GNTTABOP_map_grant_ref status: %d\n",
 		__func__, op->status);
-		if (ptp != NULL) {
-			if (have_oldpa) {
-ptp->wire_count--;
-			}
+		if (have_oldpa) {
+			ptp->wire_count--;
 		}
 	} else {
 		pgnt->pd_gnt_refs++;
-		if (ptp != NULL) {
-			if (!have_oldpa) {
-ptp->wire_count++;
-			}
-			/* Remember minimum VA in PTP. */
-			pmap_ptp_range_set(ptp, va);
+		if (!have_oldpa) {
+			ptp->wire_count++;
 		}
+		KASSERT(ptp->wire_count >= 1);
+		/* Remember minimum VA in PTP. */
+		pmap_ptp_range_set(ptp, va);
 	}
+	if (ptp->wire_count <= 1)
+		pmap_free_ptp(pmap, ptp, va, ptes, pdes);
 
 	/*
 	 * Done with the PTEs: they can now be unmapped.
@@ -5280,7 +5279,6 @@ pmap_enter_gnt(struct pmap *pmap, vaddr_
 	 * Update statistics and PTP's reference count.
 	 */
 	pmap_stats_update_bypte(pmap, 0, opte);
-	KASSERT(ptp == NULL || ptp->wire_count >= 1);
 
 	/*
 	 * If old page is pv-tracked, remove pv_entry from its list.
@@ -5376,7 +5374,7 @@ pmap_remove_gnt(struct pmap *pmap, vaddr
 		 * being used, free it!
 		 */
 
-		if (ptp && ptp->wire_count <= 1)
+		if (ptp->wire_count <= 1)
 			pmap_free_ptp(pmap, ptp, va, ptes, pdes);
 		pmap_unmap_ptes(pmap, pmap2);
 	}



CVS commit: src/sys/arch/x86/x86

2020-09-01 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Tue Sep  1 11:24:14 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Fix braino in pmap_find_gnt(), really return the gnt entry covering the range
and not one that starts just after.
Fixes a KASSERT in pmap_remove_gnt().


To generate a diff of this commit:
cvs rdiff -u -r1.403 -r1.404 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.403 src/sys/arch/x86/x86/pmap.c:1.404
--- src/sys/arch/x86/x86/pmap.c:1.403	Tue Aug  4 06:23:46 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Sep  1 11:24:14 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.403 2020/08/04 06:23:46 skrll Exp $	*/
+/*	$NetBSD: pmap.c,v 1.404 2020/09/01 11:24:14 bouyer Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.403 2020/08/04 06:23:46 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.404 2020/09/01 11:24:14 bouyer Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -5082,7 +5082,7 @@ pmap_find_gnt(struct pmap *pmap, vaddr_t
 	headp = pmap->pm_data;
 	KASSERT(headp != NULL);
 	SLIST_FOREACH(pgnt, headp, pd_gnt_list) {
-		if (pgnt->pd_gnt_sva >= sva && pgnt->pd_gnt_sva <= eva)
+		if (pgnt->pd_gnt_sva <= sva && eva <= pgnt->pd_gnt_eva)
 			return pgnt;
 		/* check that we're not overlapping part of a region */
 		KASSERT(pgnt->pd_gnt_sva >= eva || pgnt->pd_gnt_eva <= sva);



CVS commit: src/sys/arch/x86/x86

2020-08-09 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Aug  9 15:32:44 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu.c x86_machdep.c

Log Message:
move lcall sniffer to x86_machdep since xen/pv has its own cpu.c


To generate a diff of this commit:
cvs rdiff -u -r1.197 -r1.198 src/sys/arch/x86/x86/cpu.c
cvs rdiff -u -r1.145 -r1.146 src/sys/arch/x86/x86/x86_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.197 src/sys/arch/x86/x86/cpu.c:1.198
--- src/sys/arch/x86/x86/cpu.c:1.197	Sat Aug  8 15:08:48 2020
+++ src/sys/arch/x86/x86/cpu.c	Sun Aug  9 11:32:44 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.197 2020/08/08 19:08:48 christos Exp $	*/
+/*	$NetBSD: cpu.c,v 1.198 2020/08/09 15:32:44 christos Exp $	*/
 
 /*
  * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.197 2020/08/08 19:08:48 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.198 2020/08/09 15:32:44 christos Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -1451,26 +1451,3 @@ cpu_kick(struct cpu_info *ci)
 {
 	x86_send_ipi(ci, X86_IPI_AST);
 }
-
-int
-x86_cpu_is_lcall(const void *ip)
-{
-static const uint8_t lcall[] = { 0x9a, 0, 0, 0, 0 };
-	int error;
-const size_t sz = sizeof(lcall) + 2;
-uint8_t tmp[sizeof(lcall) + 2];
-
-	if ((error = copyin(ip, tmp, sz)) != 0)
-		return error;
-
-	if (memcmp(tmp, lcall, sizeof(lcall)) != 0 || tmp[sz - 1] != 0)
-		return EINVAL;
-
-	switch (tmp[sz - 2]) {
-case (uint8_t)0x07: /* NetBSD */
-	case (uint8_t)0x87: /* BSD/OS */
-		return 0;
-	default:
-		return EINVAL;
-}
-}

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.145 src/sys/arch/x86/x86/x86_machdep.c:1.146
--- src/sys/arch/x86/x86/x86_machdep.c:1.145	Sun Jul 19 10:31:31 2020
+++ src/sys/arch/x86/x86/x86_machdep.c	Sun Aug  9 11:32:44 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.145 2020/07/19 14:31:31 maxv Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.146 2020/08/09 15:32:44 christos Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.145 2020/07/19 14:31:31 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.146 2020/08/09 15:32:44 christos Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -1508,3 +1508,26 @@ cpu_initclocks(void)
 	/* Now start the clocks on this CPU (the boot CPU). */
 	(*x86_initclock_func)();
 }
+
+int
+x86_cpu_is_lcall(const void *ip)
+{
+static const uint8_t lcall[] = { 0x9a, 0, 0, 0, 0 };
+	int error;
+const size_t sz = sizeof(lcall) + 2;
+uint8_t tmp[sizeof(lcall) + 2];
+
+	if ((error = copyin(ip, tmp, sz)) != 0)
+		return error;
+
+	if (memcmp(tmp, lcall, sizeof(lcall)) != 0 || tmp[sz - 1] != 0)
+		return EINVAL;
+
+	switch (tmp[sz - 2]) {
+case (uint8_t)0x07: /* NetBSD */
+	case (uint8_t)0x87: /* BSD/OS */
+		return 0;
+	default:
+		return EINVAL;
+}
+}



CVS commit: src/sys/arch/x86/x86

2020-08-04 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Tue Aug  4 06:23:46 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Trailing whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.402 -r1.403 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.402 src/sys/arch/x86/x86/pmap.c:1.403
--- src/sys/arch/x86/x86/pmap.c:1.402	Tue Aug  4 06:22:54 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Aug  4 06:23:46 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.402 2020/08/04 06:22:54 skrll Exp $	*/
+/*	$NetBSD: pmap.c,v 1.403 2020/08/04 06:23:46 skrll Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.402 2020/08/04 06:22:54 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.403 2020/08/04 06:23:46 skrll Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -233,7 +233,7 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.4
  * pg->uobject->vmobjlock, pg->uanon->an_lock
  *
  * 	For managed pages, these per-object locks are taken by the VM system
- *	before calling into the pmap module - either a read or write hold. 
+ *	before calling into the pmap module - either a read or write hold.
  *	The lock hold prevent pages from changing identity while the pmap is
  *	operating on them.  For example, the same lock is held across a call
  *	to pmap_remove() and the following call to pmap_update(), so that a
@@ -250,7 +250,7 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.4
  *
  * pmaps_lock
  *
- *	This lock protects the list of active pmaps (headed by "pmaps"). 
+ *	This lock protects the list of active pmaps (headed by "pmaps").
  *	It's acquired when adding or removing pmaps or adjusting kernel PDEs.
  *
  * pp_lock
@@ -2066,7 +2066,7 @@ pmap_free_pv(struct pmap *pmap, struct p
 		/* part -> full */
 		LIST_REMOVE(pvp, pvp_list);
 		LIST_INSERT_HEAD(>pm_pvp_full, pvp, pvp_list);
-	} 
+	}
 }
 
 /*
@@ -2394,7 +2394,7 @@ pmap_freepage(struct pmap *pmap, struct 
 	/*
 	 * Enqueue the PTP to be freed by pmap_update().  We can't remove
 	 * the page from the uvm_object, as that can take further locks
-	 * (intolerable right now because the PTEs are likely mapped in). 
+	 * (intolerable right now because the PTEs are likely mapped in).
 	 * Instead mark the PTP as free and if we bump into it again, we'll
 	 * either ignore or reuse (depending on what's useful at the time).
 	 */
@@ -3091,7 +3091,7 @@ pmap_zap_ptp(struct pmap *pmap, struct v
 		 * tree by skipping to the next VA in the tree whenever
 		 * there is a match here.  The tree will be cleared out in
 		 * one pass before return to pmap_remove_all().
-		 */ 
+		 */
 		oattrs = pmap_pte_to_pp_attrs(opte);
 		if (pve != NULL && pve->pve_pte.pte_va == va) {
 			pp = pve->pve_pp;
@@ -3153,7 +3153,7 @@ pmap_zap_ptp(struct pmap *pmap, struct v
  *
  * Ordinarily when removing mappings it's important to hold the UVM object's
  * lock, so that pages do not gain a new identity while retaining stale TLB
- * entries (the same lock hold covers both pmap_remove() and pmap_update()). 
+ * entries (the same lock hold covers both pmap_remove() and pmap_update()).
  * Here it's known that the address space is no longer visible to any user
  * process, so we don't need to worry about that.
  */
@@ -3172,7 +3172,7 @@ pmap_remove_all(struct pmap *pmap)
 	if (pmap->pm_remove != NULL) {
 		return false;
 	}
- 
+
 	for (;;) {
 		/* Fetch a block of PTPs from tree. */
 		mutex_enter(>pm_lock);
@@ -4371,7 +4371,7 @@ pmap_pp_remove(struct pmap_page *pp, pad
 		"va %lx pmap %p ptp %p is free", va, pmap, ptp);
 		KASSERTMSG(ptp == NULL || ptp->wire_count > 1,
 		"va %lx pmap %p ptp %p is empty", va, pmap, ptp);
-		
+
 #ifdef DEBUG
 		pmap_check_pv(pmap, ptp, pp, pvpte->pte_va, true);
 		rb_tree_t *tree = (ptp != NULL ?
@@ -4841,7 +4841,7 @@ pmap_enter_ma(struct pmap *pmap, vaddr_t
 	else
 #endif
 		new_pg = PHYS_TO_VM_PAGE(pa);
-		
+
 	if (new_pg != NULL) {
 		/* This is a managed page */
 		npte |= PTE_PVLIST;
@@ -5063,7 +5063,7 @@ same_pa:
 
 struct pmap_data_gnt {
 	SLIST_ENTRY(pmap_data_gnt) pd_gnt_list;
-	vaddr_t pd_gnt_sva; 
+	vaddr_t pd_gnt_sva;
 	vaddr_t pd_gnt_eva; /* range covered by this gnt */
 	int pd_gnt_refs; /* ref counter */
 	struct gnttab_map_grant_ref pd_gnt_ops[1]; /* variable length */



CVS commit: src/sys/arch/x86/x86

2020-08-04 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Tue Aug  4 06:22:55 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
typo in comment


To generate a diff of this commit:
cvs rdiff -u -r1.401 -r1.402 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.401 src/sys/arch/x86/x86/pmap.c:1.402
--- src/sys/arch/x86/x86/pmap.c:1.401	Sun Jul 19 13:58:26 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Aug  4 06:22:54 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.401 2020/07/19 13:58:26 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.402 2020/08/04 06:22:54 skrll Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.401 2020/07/19 13:58:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.402 2020/08/04 06:22:54 skrll Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -251,7 +251,7 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.4
  * pmaps_lock
  *
  *	This lock protects the list of active pmaps (headed by "pmaps"). 
- *	It's acqired when adding or removing pmaps or adjusting kernel PDEs.
+ *	It's acquired when adding or removing pmaps or adjusting kernel PDEs.
  *
  * pp_lock
  *



CVS commit: src/sys/arch/x86/x86

2020-08-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Aug  2 18:23:33 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Revert "Add kthread_fpu_enter/exit support to x86." for now.

Need to find all the paths out of interrupts back into _kernel_
context to add HANDLE_DEFERRED_FPU, I think, before this can be
enabled.


To generate a diff of this commit:
cvs rdiff -u -r1.73 -r1.74 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.73 src/sys/arch/x86/x86/fpu.c:1.74
--- src/sys/arch/x86/x86/fpu.c:1.73	Sat Aug  1 02:13:34 2020
+++ src/sys/arch/x86/x86/fpu.c	Sun Aug  2 18:23:33 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.73 2020/08/01 02:13:34 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.74 2020/08/02 18:23:33 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.73 2020/08/01 02:13:34 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.74 2020/08/02 18:23:33 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -107,7 +107,6 @@ __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.73
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 
@@ -132,35 +131,13 @@ void fpu_switch(struct lwp *, struct lwp
 
 uint32_t x86_fpu_mxcsr_mask __read_mostly = 0;
 
-/*
- * True if this a thread that is allowed to use the FPU -- either a
- * user thread, or a system thread with LW_SYSTEM_FPU enabled.
- */
-static inline bool
-lwp_can_haz_fpu(struct lwp *l)
-{
-
-	return (l->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) != LW_SYSTEM;
-}
-
-/*
- * True if this is a system thread with its own private FPU state.
- */
-static inline bool
-lwp_system_fpu_p(struct lwp *l)
-{
-
-	return (l->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) ==
-	(LW_SYSTEM|LW_SYSTEM_FPU);
-}
-
 static inline union savefpu *
 fpu_lwp_area(struct lwp *l)
 {
 	struct pcb *pcb = lwp_getpcb(l);
 	union savefpu *area = >pcb_savefpu;
 
-	KASSERT(lwp_can_haz_fpu(l));
+	KASSERT((l->l_flag & LW_SYSTEM) == 0);
 	if (l == curlwp) {
 		fpu_save();
 	}
@@ -178,7 +155,7 @@ fpu_save_lwp(struct lwp *l)
 
 	s = splvm();
 	if (l->l_md.md_flags & MDL_FPU_IN_CPU) {
-		KASSERT(lwp_can_haz_fpu(l));
+		KASSERT((l->l_flag & LW_SYSTEM) == 0);
 		fpu_area_save(area, x86_xsave_features);
 		l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
 	}
@@ -330,7 +307,7 @@ fpu_switch(struct lwp *oldlwp, struct lw
 	cpu_index(ci), ci->ci_ilevel);
 
 	if (oldlwp->l_md.md_flags & MDL_FPU_IN_CPU) {
-		KASSERT(lwp_can_haz_fpu(oldlwp));
+		KASSERT(!(oldlwp->l_flag & LW_SYSTEM));
 		pcb = lwp_getpcb(oldlwp);
 		fpu_area_save(>pcb_savefpu, x86_xsave_features);
 		oldlwp->l_md.md_flags &= ~MDL_FPU_IN_CPU;
@@ -345,11 +322,11 @@ fpu_lwp_fork(struct lwp *l1, struct lwp 
 	union savefpu *fpu_save;
 
 	/* Kernel threads have no FPU. */
-	if (__predict_false(!lwp_can_haz_fpu(l2))) {
+	if (__predict_false(l2->l_flag & LW_SYSTEM)) {
 		return;
 	}
 	/* For init(8). */
-	if (__predict_false(!lwp_can_haz_fpu(l1))) {
+	if (__predict_false(l1->l_flag & LW_SYSTEM)) {
 		memset(>pcb_savefpu, 0, x86_fpu_save_size);
 		return;
 	}
@@ -373,8 +350,6 @@ fpu_lwp_abandon(struct lwp *l)
 
 /* -- */
 
-static const union savefpu zero_fpu __aligned(64);
-
 /*
  * fpu_kern_enter()
  *
@@ -394,11 +369,6 @@ fpu_kern_enter(void)
 	struct cpu_info *ci;
 	int s;
 
-	if (lwp_system_fpu_p(l) && !cpu_intr_p()) {
-		KASSERT(!cpu_softintr_p());
-		return;
-	}
-
 	s = splvm();
 
 	ci = curcpu();
@@ -431,16 +401,10 @@ fpu_kern_enter(void)
 void
 fpu_kern_leave(void)
 {
-	struct cpu_info *ci;
+	static const union savefpu zero_fpu __aligned(64);
+	struct cpu_info *ci = curcpu();
 	int s;
 
-	if (lwp_system_fpu_p(curlwp) && !cpu_intr_p()) {
-		KASSERT(!cpu_softintr_p());
-		return;
-	}
-
-	ci = curcpu();
-
 	KASSERT(ci->ci_ilevel == IPL_VM);
 	KASSERT(ci->ci_kfpu_spl != -1);
 
@@ -462,23 +426,6 @@ fpu_kern_leave(void)
 	splx(s);
 }
 
-void
-kthread_fpu_enter_md(void)
-{
-
-	/* Enable the FPU by clearing CR0_TS.  */
-	clts();
-}
-
-void
-kthread_fpu_exit_md(void)
-{
-
-	/* Zero the FPU state and disable the FPU by setting CR0_TS.  */
-	fpu_area_restore(_fpu, x86_xsave_features);
-	stts();
-}
-
 /* -- */
 
 /*



CVS commit: src/sys/arch/x86/x86

2020-07-31 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Aug  1 02:13:34 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Add kthread_fpu_enter/exit support to x86.


To generate a diff of this commit:
cvs rdiff -u -r1.72 -r1.73 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.72 src/sys/arch/x86/x86/fpu.c:1.73
--- src/sys/arch/x86/x86/fpu.c:1.72	Mon Jul 20 16:43:03 2020
+++ src/sys/arch/x86/x86/fpu.c	Sat Aug  1 02:13:34 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.72 2020/07/20 16:43:03 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.73 2020/08/01 02:13:34 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.72 2020/07/20 16:43:03 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.73 2020/08/01 02:13:34 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -107,6 +107,7 @@ __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.72
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -131,13 +132,35 @@ void fpu_switch(struct lwp *, struct lwp
 
 uint32_t x86_fpu_mxcsr_mask __read_mostly = 0;
 
+/*
+ * True if this a thread that is allowed to use the FPU -- either a
+ * user thread, or a system thread with LW_SYSTEM_FPU enabled.
+ */
+static inline bool
+lwp_can_haz_fpu(struct lwp *l)
+{
+
+	return (l->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) != LW_SYSTEM;
+}
+
+/*
+ * True if this is a system thread with its own private FPU state.
+ */
+static inline bool
+lwp_system_fpu_p(struct lwp *l)
+{
+
+	return (l->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) ==
+	(LW_SYSTEM|LW_SYSTEM_FPU);
+}
+
 static inline union savefpu *
 fpu_lwp_area(struct lwp *l)
 {
 	struct pcb *pcb = lwp_getpcb(l);
 	union savefpu *area = >pcb_savefpu;
 
-	KASSERT((l->l_flag & LW_SYSTEM) == 0);
+	KASSERT(lwp_can_haz_fpu(l));
 	if (l == curlwp) {
 		fpu_save();
 	}
@@ -155,7 +178,7 @@ fpu_save_lwp(struct lwp *l)
 
 	s = splvm();
 	if (l->l_md.md_flags & MDL_FPU_IN_CPU) {
-		KASSERT((l->l_flag & LW_SYSTEM) == 0);
+		KASSERT(lwp_can_haz_fpu(l));
 		fpu_area_save(area, x86_xsave_features);
 		l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
 	}
@@ -307,7 +330,7 @@ fpu_switch(struct lwp *oldlwp, struct lw
 	cpu_index(ci), ci->ci_ilevel);
 
 	if (oldlwp->l_md.md_flags & MDL_FPU_IN_CPU) {
-		KASSERT(!(oldlwp->l_flag & LW_SYSTEM));
+		KASSERT(lwp_can_haz_fpu(oldlwp));
 		pcb = lwp_getpcb(oldlwp);
 		fpu_area_save(>pcb_savefpu, x86_xsave_features);
 		oldlwp->l_md.md_flags &= ~MDL_FPU_IN_CPU;
@@ -322,11 +345,11 @@ fpu_lwp_fork(struct lwp *l1, struct lwp 
 	union savefpu *fpu_save;
 
 	/* Kernel threads have no FPU. */
-	if (__predict_false(l2->l_flag & LW_SYSTEM)) {
+	if (__predict_false(!lwp_can_haz_fpu(l2))) {
 		return;
 	}
 	/* For init(8). */
-	if (__predict_false(l1->l_flag & LW_SYSTEM)) {
+	if (__predict_false(!lwp_can_haz_fpu(l1))) {
 		memset(>pcb_savefpu, 0, x86_fpu_save_size);
 		return;
 	}
@@ -350,6 +373,8 @@ fpu_lwp_abandon(struct lwp *l)
 
 /* -- */
 
+static const union savefpu zero_fpu __aligned(64);
+
 /*
  * fpu_kern_enter()
  *
@@ -369,6 +394,11 @@ fpu_kern_enter(void)
 	struct cpu_info *ci;
 	int s;
 
+	if (lwp_system_fpu_p(l) && !cpu_intr_p()) {
+		KASSERT(!cpu_softintr_p());
+		return;
+	}
+
 	s = splvm();
 
 	ci = curcpu();
@@ -401,10 +431,16 @@ fpu_kern_enter(void)
 void
 fpu_kern_leave(void)
 {
-	static const union savefpu zero_fpu __aligned(64);
-	struct cpu_info *ci = curcpu();
+	struct cpu_info *ci;
 	int s;
 
+	if (lwp_system_fpu_p(curlwp) && !cpu_intr_p()) {
+		KASSERT(!cpu_softintr_p());
+		return;
+	}
+
+	ci = curcpu();
+
 	KASSERT(ci->ci_ilevel == IPL_VM);
 	KASSERT(ci->ci_kfpu_spl != -1);
 
@@ -426,6 +462,23 @@ fpu_kern_leave(void)
 	splx(s);
 }
 
+void
+kthread_fpu_enter_md(void)
+{
+
+	/* Enable the FPU by clearing CR0_TS.  */
+	clts();
+}
+
+void
+kthread_fpu_exit_md(void)
+{
+
+	/* Zero the FPU state and disable the FPU by setting CR0_TS.  */
+	fpu_area_restore(_fpu, x86_xsave_features);
+	stts();
+}
+
 /* -- */
 
 /*



CVS commit: src/sys/arch/x86/x86

2020-07-30 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Jul 30 17:26:24 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu_rng.c

Log Message:
Cite Cryptography Research evaluation of VIA RNG and give live URL.

(URL verified to be archived in the Internet Archive for posterity)


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/x86/x86/cpu_rng.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.18 src/sys/arch/x86/x86/cpu_rng.c:1.19
--- src/sys/arch/x86/x86/cpu_rng.c:1.18	Sat Jul 25 22:10:34 2020
+++ src/sys/arch/x86/x86/cpu_rng.c	Thu Jul 30 17:26:23 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.18 2020/07/25 22:10:34 riastradh Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.19 2020/07/30 17:26:23 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -228,6 +228,11 @@ cpu_rng_via(uint64_t *out)
 	 * The Cryptography Research paper on the VIA RNG estimates
 	 * 0.75 bits of entropy per output bit and advises users to
 	 * be "even more conservative".
+	 *
+	 *	`Evaluation of VIA C3 Nehemiah Random Number
+	 *	Generator', Cryptography Research, Inc., February 27,
+	 *	2003.
+	 *	https://www.rambus.com/wp-content/uploads/2015/08/VIA_rng.pdf
 	 */
 	return nbytes * NBBY/2;
 }



CVS commit: src/sys/arch/x86/x86

2020-07-28 Thread Frederic Cambus
Module Name:src
Committed By:   fcambus
Date:   Tue Jul 28 14:49:55 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu.c

Log Message:
Use CPU_IS_PRIMARY macro in cpu_stop(), cpu_resume(), and cpu_get_tsc_freq()
on x86.

OK kamil@


To generate a diff of this commit:
cvs rdiff -u -r1.195 -r1.196 src/sys/arch/x86/x86/cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.195 src/sys/arch/x86/x86/cpu.c:1.196
--- src/sys/arch/x86/x86/cpu.c:1.195	Tue Jul 14 00:45:53 2020
+++ src/sys/arch/x86/x86/cpu.c	Tue Jul 28 14:49:55 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.195 2020/07/14 00:45:53 yamaguchi Exp $	*/
+/*	$NetBSD: cpu.c,v 1.196 2020/07/28 14:49:55 fcambus Exp $	*/
 
 /*
  * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.195 2020/07/14 00:45:53 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.196 2020/07/28 14:49:55 fcambus Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -1224,7 +1224,7 @@ cpu_stop(device_t dv)
 
 	KASSERT((ci->ci_flags & CPUF_PRESENT) != 0);
 
-	if ((ci->ci_flags & CPUF_PRIMARY) != 0)
+	if (CPU_IS_PRIMARY(ci))
 		return true;
 
 	if (ci->ci_data.cpu_idlelwp == NULL)
@@ -1269,7 +1269,7 @@ cpu_resume(device_t dv, const pmf_qual_t
 	if ((ci->ci_flags & CPUF_PRESENT) == 0)
 		return true;
 
-	if ((ci->ci_flags & CPUF_PRIMARY) != 0)
+	if (CPU_IS_PRIMARY(ci))
 		goto out;
 
 	if (ci->ci_data.cpu_idlelwp == NULL)
@@ -1312,7 +1312,7 @@ cpu_get_tsc_freq(struct cpu_info *ci)
 	uint64_t freq = 0, freq_from_cpuid, t0, t1;
 	int64_t overhead;
 
-	if ((ci->ci_flags & CPUF_PRIMARY) != 0 && cpu_hascounter()) {
+	if (CPU_IS_PRIMARY(ci) && cpu_hascounter()) {
 		/*
 		 * If it's the first call of this function, try to get TSC
 		 * freq from CPUID by calling cpu_tsc_freq_cpuid().



CVS commit: src/sys/arch/x86/x86

2020-07-25 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jul 25 22:44:02 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
Nix outdated comment.

The substance of the change that introduced it was reverted, but I
neglected to revert the comment when reverting the substance.


To generate a diff of this commit:
cvs rdiff -u -r1.114 -r1.115 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.114 src/sys/arch/x86/x86/identcpu.c:1.115
--- src/sys/arch/x86/x86/identcpu.c:1.114	Sat Jul 25 22:12:56 2020
+++ src/sys/arch/x86/x86/identcpu.c	Sat Jul 25 22:44:02 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.114 2020/07/25 22:12:56 riastradh Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.115 2020/07/25 22:44:02 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.114 2020/07/25 22:12:56 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.115 2020/07/25 22:44:02 riastradh Exp $");
 
 #include "opt_xen.h"
 
@@ -1001,12 +1001,6 @@ cpu_probe(struct cpu_info *ci)
 		/* Early patch of text segment. */
 		x86_patch(true);
 #endif
-		/*
-		 * XXX There is a bug with FPU in kernel that we
-		 * haven't been able to track down yet, and all of the
-		 * accelerated AES code relies on that, so it is
-		 * disabled temporarily while we diagnose the bug.
-		 */
 #ifdef __x86_64__	/* not yet implemented on i386 */
 		if (cpu_feature[1] & CPUID2_AES)
 			aes_md_init(_ni_impl);



CVS commit: src/sys/arch/x86/x86

2020-07-25 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jul 25 22:10:34 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu_rng.c

Log Message:
Tweak VIA CPU RNG.

- Cite source for documentation.
- Omit needless kpreempt_disable/enable.
- Explain what's going on.
- Use "D"(out) rather than "+D"(out) -- no REP so no register update.
- Fix interpretation of number of bytes returned.

The last one is likely to address

[   4.0518619] aes: VIA ACE

[  11.7018582] cpu_rng via: failed repetition test
[  12.4718583] entropy: ready

reported by Andrius V.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/x86/x86/cpu_rng.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.17 src/sys/arch/x86/x86/cpu_rng.c:1.18
--- src/sys/arch/x86/x86/cpu_rng.c:1.17	Mon Jun 15 01:24:20 2020
+++ src/sys/arch/x86/x86/cpu_rng.c	Sat Jul 25 22:10:34 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.17 2020/06/15 01:24:20 riastradh Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.18 2020/07/25 22:10:34 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -30,9 +30,9 @@
  */
 
 /*
- * The VIA RNG code in this file is inspired by Jason Wright and
- * Theo de Raadt's OpenBSD version but has been rewritten in light of
- * comments from Henric Jungheim on the t...@openbsd.org mailing list.
+ * For reference on VIA XSTORERNG, see the VIA PadLock Programming
+ * Guide (`VIA PPG'), August 4, 2005.
+ * http://linux.via.com.tw/support/beginDownload.action?eleid=181=261
  *
  * For reference on Intel RDRAND/RDSEED, see the Intel Digital Random
  * Number Generator Software Implementation Guide (`Intel DRNG SIG'),
@@ -183,48 +183,53 @@ cpu_rng_rdseed_rdrand(uint64_t *out)
 	return n;
 }
 
+/*
+ * VIA PPG says EAX[4:0] is nbytes, but the only documented numbers of
+ * bytes are 0,1,2,4,8 -- and there's only 8 bytes of output buffer
+ * anyway, so let's ignore bit 4 and treat it like EAX[3:0] instead.
+ */
+#define	VIA_RNG_STATUS_NBYTES	__BITS(3,0)
+#define	VIA_RNG_STATUS_MSR110B	__BITS(31,5)
+
 static size_t
 cpu_rng_via(uint64_t *out)
 {
 	u_long psl;
-	uint32_t creg0, rndsts;
+	uint32_t cr0, status, nbytes;
 
 	/*
-	 * Sadly, we have to monkey with the coprocessor enable and fault
-	 * registers, which are really for the FPU, in order to read
-	 * from the RNG.
-	 *
-	 * Don't remove CR0_TS from the call below -- comments in the Linux
-	 * driver indicate that the xstorerng instruction can generate
-	 * spurious DNA faults though no FPU or SIMD state is changed
-	 * even if such a fault is generated.
-	 *
-	 * XXX can this really happen if we don't use "rep xstorrng"?
+	 * The XSTORE instruction is handled by the SSE unit, which
+	 * requires the CR0 TS and CR0 EM bits to be clear.  We disable
+	 * all processor interrupts so there is no danger of any
+	 * interrupt handler changing CR0 while we work -- although
+	 * really, software splvm or fpu_kern_enter/leave should be
+	 * enough (but we'll do that in a separate change for the
+	 * benefit of bisection in case I'm wrong).
 	 */
-	kpreempt_disable();
 	psl = x86_read_psl();
 	x86_disable_intr();
-	creg0 = rcr0();
-	lcr0(creg0 & ~(CR0_EM|CR0_TS)); /* Permit access to SIMD/FPU path */
-	/*
-	 * The VIA RNG has an output queue of 8-byte values.  Read one.
-	 * This is atomic, so if the FPU were already enabled, we could skip
-	 * all the preemption and interrupt frobbing.  If we had bread,
-	 * we could have a ham sandwich, if we had any ham.
-	 */
-	__asm __volatile("xstorerng"
-	: "=a" (rndsts), "+D" (out) : "d" (0) : "memory");
-	/* Put CR0 back how it was */
-	lcr0(creg0);
+	cr0 = rcr0();
+	lcr0(cr0 & ~(CR0_EM|CR0_TS));
+
+	/* Read up to eight bytes out of the buffer.  */
+	asm volatile("xstorerng"
+	: "=a"(status)
+	: "D"(out), "d"(0) /* EDX[1:0]=00 -> wait for 8 bytes or fail */
+	: "memory");
+
+	/* Restore CR0 and interrupts.  */
+	lcr0(cr0);
 	x86_write_psl(psl);
-	kpreempt_enable();
+
+	/* Get the number of bytes stored.  (Should always be 8 or 0.)  */
+	nbytes = __SHIFTOUT(status, VIA_RNG_STATUS_NBYTES);
 
 	/*
 	 * The Cryptography Research paper on the VIA RNG estimates
 	 * 0.75 bits of entropy per output bit and advises users to
 	 * be "even more conservative".
 	 */
-	return (rndsts & 0xf) ? 0 : sizeof(uint64_t) * NBBY/2;
+	return nbytes * NBBY/2;
 }
 
 static size_t



CVS commit: src/sys/arch/x86/x86

2020-07-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul 20 16:45:41 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
Revert 1.112 "Disable x86 in-kernel AES temporarily."

The bug in fpu_kern_enter motivating this appears to have been fixed.


To generate a diff of this commit:
cvs rdiff -u -r1.112 -r1.113 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.112 src/sys/arch/x86/x86/identcpu.c:1.113
--- src/sys/arch/x86/x86/identcpu.c:1.112	Mon Jul 20 15:11:29 2020
+++ src/sys/arch/x86/x86/identcpu.c	Mon Jul 20 16:45:41 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.112 2020/07/20 15:11:29 riastradh Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.113 2020/07/20 16:45:41 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.112 2020/07/20 15:11:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.113 2020/07/20 16:45:41 riastradh Exp $");
 
 #include "opt_xen.h"
 
@@ -1007,17 +1007,17 @@ cpu_probe(struct cpu_info *ci)
 		 * disabled temporarily while we diagnose the bug.
 		 */
 #ifdef __x86_64__	/* not yet implemented on i386 */
-		if (0 & cpu_feature[1] & CPUID2_AES)
+		if (cpu_feature[1] & CPUID2_AES)
 			aes_md_init(_ni_impl);
 		else
 #endif
-		if (0 & cpu_feature[4] & CPUID_VIA_HAS_ACE)
+		if (cpu_feature[4] & CPUID_VIA_HAS_ACE)
 			aes_md_init(_via_impl);
-		else if (0 & i386_has_sse && i386_has_sse2 &&
+		else if (i386_has_sse && i386_has_sse2 &&
 		(cpu_feature[1] & CPUID2_SSE3) &&
 		(cpu_feature[1] & CPUID2_SSSE3))
 			aes_md_init(_ssse3_impl);
-		else if (0 & i386_has_sse && i386_has_sse2)
+		else if (i386_has_sse && i386_has_sse2)
 			aes_md_init(_sse2_impl);
 	} else {
 		/*



CVS commit: src/sys/arch/x86/x86

2020-07-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul 20 16:43:03 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Fix fpu_kern_enter in a softint that interrupted a softint.

We need to find the lwp that was originally interrupted to save its
fpu state.

With this, fpu-heavy programs (like firefox) are once again stable,
at least under modest stress testing, on systems configured to use
wifi with WPA2 and CCMP.


To generate a diff of this commit:
cvs rdiff -u -r1.71 -r1.72 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.71 src/sys/arch/x86/x86/fpu.c:1.72
--- src/sys/arch/x86/x86/fpu.c:1.71	Mon Jul 20 16:41:18 2020
+++ src/sys/arch/x86/x86/fpu.c	Mon Jul 20 16:43:03 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.71 2020/07/20 16:41:18 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.72 2020/07/20 16:43:03 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.71 2020/07/20 16:41:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.72 2020/07/20 16:43:03 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -380,11 +380,9 @@ fpu_kern_enter(void)
 	 * If we are in a softint and have a pinned lwp, the fpu state is that
 	 * of the pinned lwp, so save it there.
 	 */
-	if ((l->l_pflag & LP_INTR) && (l->l_switchto != NULL)) {
-		fpu_save_lwp(l->l_switchto);
-	} else {
-		fpu_save_lwp(l);
-	}
+	while ((l->l_pflag & LP_INTR) && (l->l_switchto != NULL))
+		l = l->l_switchto;
+	fpu_save_lwp(l);
 
 	/*
 	 * Clear CR0_TS, which fpu_save_lwp set if it saved anything --



CVS commit: src/sys/arch/x86/x86

2020-07-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul 20 16:41:18 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Save fpu state at IPL_VM to exclude fpu_kern_enter/leave.

This way fpu_kern_enter/leave cannot interrupt the transition, so the
transition from state-on-CPU to state-in-memory (with TS set) is
atomic whether in an interrupt or not.

(I am not 100% convinced that this is necessary, but it makes
reasoning about the transition simpler.)


To generate a diff of this commit:
cvs rdiff -u -r1.70 -r1.71 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.70 src/sys/arch/x86/x86/fpu.c:1.71
--- src/sys/arch/x86/x86/fpu.c:1.70	Mon Jul 20 16:38:47 2020
+++ src/sys/arch/x86/x86/fpu.c	Mon Jul 20 16:41:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.70 2020/07/20 16:38:47 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.71 2020/07/20 16:41:18 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.70 2020/07/20 16:38:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.71 2020/07/20 16:41:18 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -151,14 +151,15 @@ fpu_save_lwp(struct lwp *l)
 {
 	struct pcb *pcb = lwp_getpcb(l);
 	union savefpu *area = >pcb_savefpu;
+	int s;
 
-	kpreempt_disable();
+	s = splvm();
 	if (l->l_md.md_flags & MDL_FPU_IN_CPU) {
 		KASSERT((l->l_flag & LW_SYSTEM) == 0);
 		fpu_area_save(area, x86_xsave_features);
 		l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
 	}
-	kpreempt_enable();
+	splx(s);
 }
 
 /*
@@ -299,8 +300,12 @@ fpu_handle_deferred(void)
 void
 fpu_switch(struct lwp *oldlwp, struct lwp *newlwp)
 {
+	struct cpu_info *ci __diagused = curcpu();
 	struct pcb *pcb;
 
+	KASSERTMSG(ci->ci_ilevel >= IPL_SCHED, "cpu%d ilevel=%d",
+	cpu_index(ci), ci->ci_ilevel);
+
 	if (oldlwp->l_md.md_flags & MDL_FPU_IN_CPU) {
 		KASSERT(!(oldlwp->l_flag & LW_SYSTEM));
 		pcb = lwp_getpcb(oldlwp);
@@ -334,11 +339,13 @@ fpu_lwp_fork(struct lwp *l1, struct lwp 
 void
 fpu_lwp_abandon(struct lwp *l)
 {
+	int s;
+
 	KASSERT(l == curlwp);
-	kpreempt_disable();
+	s = splvm();
 	l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
 	stts();
-	kpreempt_enable();
+	splx(s);
 }
 
 /* -- */



CVS commit: src/sys/arch/x86/x86

2020-07-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul 20 16:38:47 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Revert 1.66 "Fix race in fpu save with fpu_kern_enter in softint."

This only fixed part of the race, and we can do it more simply.


To generate a diff of this commit:
cvs rdiff -u -r1.69 -r1.70 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.69 src/sys/arch/x86/x86/fpu.c:1.70
--- src/sys/arch/x86/x86/fpu.c:1.69	Mon Jul 20 16:37:34 2020
+++ src/sys/arch/x86/x86/fpu.c	Mon Jul 20 16:38:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.69 2020/07/20 16:37:34 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.70 2020/07/20 16:38:47 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.69 2020/07/20 16:37:34 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.70 2020/07/20 16:38:47 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -126,8 +126,6 @@ __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.69
 #define stts() HYPERVISOR_fpu_taskswitch(1)
 #endif
 
-static void fpu_area_do_save(void *, uint64_t);
-
 void fpu_handle_deferred(void);
 void fpu_switch(struct lwp *, struct lwp *);
 
@@ -157,24 +155,8 @@ fpu_save_lwp(struct lwp *l)
 	kpreempt_disable();
 	if (l->l_md.md_flags & MDL_FPU_IN_CPU) {
 		KASSERT((l->l_flag & LW_SYSTEM) == 0);
-
-		/*
-		 * Order is important, in case we are interrupted and
-		 * the interrupt calls fpu_kern_enter, triggering
-		 * reentry of fpu_save_lwp:
-		 *
-		 * 1. Save FPU state.
-		 * 2. Note FPU state has been saved.
-		 * 3. Disable FPU access so the kernel doesn't
-		 *accidentally use it.
-		 *
-		 * Steps (1) and (2) are both idempotent until step
-		 * (3), after which point attempting to save the FPU
-		 * state will trigger #NM/fpudna fault.
-		 */
-		fpu_area_do_save(area, x86_xsave_features);
+		fpu_area_save(area, x86_xsave_features);
 		l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
-		stts();
 	}
 	kpreempt_enable();
 }
@@ -263,8 +245,8 @@ fpu_errata_amd(void)
 	fldummy();
 }
 
-static void
-fpu_area_do_save(void *area, uint64_t xsave_features)
+void
+fpu_area_save(void *area, uint64_t xsave_features)
 {
 	switch (x86_fpu_save) {
 	case FPU_SAVE_FSAVE:
@@ -280,13 +262,7 @@ fpu_area_do_save(void *area, uint64_t xs
 		xsaveopt(area, xsave_features);
 		break;
 	}
-}
-
-void
-fpu_area_save(void *area, uint64_t xsave_features)
-{
 
-	fpu_area_do_save(area, xsave_features);
 	stts();
 }
 



CVS commit: src/sys/arch/x86/x86

2020-07-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul 20 16:37:35 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Revert 1.67 "Restore the lwp's fpu state, not zeros, and leave with fpu 
enabled."

This didn't actually avoid double-restore, and it doesn't solve the
problem anyway, and made it harder to detect in-kernel fpu abuse.


To generate a diff of this commit:
cvs rdiff -u -r1.68 -r1.69 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.68 src/sys/arch/x86/x86/fpu.c:1.69
--- src/sys/arch/x86/x86/fpu.c:1.68	Mon Jul 13 16:51:51 2020
+++ src/sys/arch/x86/x86/fpu.c	Mon Jul 20 16:37:34 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.68 2020/07/13 16:51:51 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.69 2020/07/20 16:37:34 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.68 2020/07/13 16:51:51 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.69 2020/07/20 16:37:34 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -421,9 +421,6 @@ void
 fpu_kern_leave(void)
 {
 	static const union savefpu zero_fpu __aligned(64);
-	const union savefpu *savefpu;
-	struct lwp *l = curlwp;
-	struct pcb *pcb;
 	struct cpu_info *ci = curcpu();
 	int s;
 
@@ -431,18 +428,17 @@ fpu_kern_leave(void)
 	KASSERT(ci->ci_kfpu_spl != -1);
 
 	/*
-	 * Restore the FPU state immediately to avoid leaking any
-	 * kernel secrets, or zero it if this is a kthread.
+	 * Zero the fpu registers; otherwise we might leak secrets
+	 * through Spectre-class attacks to userland, even if there are
+	 * no bugs in fpu state management.
 	 */
-	if ((l->l_pflag & LP_INTR) && (l->l_switchto != NULL))
-		l = l->l_switchto;
-	if (l->l_flag & LW_SYSTEM) {
-		savefpu = _fpu;
-	} else {
-		pcb = lwp_getpcb(l);
-		savefpu = >pcb_savefpu;
-	}
-	fpu_area_restore(savefpu, x86_xsave_features);
+	fpu_area_restore(_fpu, x86_xsave_features);
+
+	/*
+	 * Set CR0_TS again so that the kernel can't accidentally use
+	 * the FPU.
+	 */
+	stts();
 
 	s = ci->ci_kfpu_spl;
 	ci->ci_kfpu_spl = -1;



CVS commit: src/sys/arch/x86/x86

2020-07-20 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul 20 15:11:29 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
Disable x86 in-kernel AES temporarily.

There's a bug in the FPU state handling that it triggers -- likely
limited to the softint path since I've only ever seen it on a system
using wifi configured with WPA2 and CCMP, which uses AES heavily in
softint.

This is to be reverted once we diagnose the bug.  (There is also a
performance regression on wifi with WPA2 and CCMP, which I plan to
fix too once we figure out the FPU state handling bug.)


To generate a diff of this commit:
cvs rdiff -u -r1.111 -r1.112 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.111 src/sys/arch/x86/x86/identcpu.c:1.112
--- src/sys/arch/x86/x86/identcpu.c:1.111	Mon Jun 29 23:51:35 2020
+++ src/sys/arch/x86/x86/identcpu.c	Mon Jul 20 15:11:29 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.111 2020/06/29 23:51:35 riastradh Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.112 2020/07/20 15:11:29 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.111 2020/06/29 23:51:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.112 2020/07/20 15:11:29 riastradh Exp $");
 
 #include "opt_xen.h"
 
@@ -1000,18 +1000,24 @@ cpu_probe(struct cpu_info *ci)
 		/* Early patch of text segment. */
 		x86_patch(true);
 #endif
+		/*
+		 * XXX There is a bug with FPU in kernel that we
+		 * haven't been able to track down yet, and all of the
+		 * accelerated AES code relies on that, so it is
+		 * disabled temporarily while we diagnose the bug.
+		 */
 #ifdef __x86_64__	/* not yet implemented on i386 */
-		if (cpu_feature[1] & CPUID2_AES)
+		if (0 & cpu_feature[1] & CPUID2_AES)
 			aes_md_init(_ni_impl);
 		else
 #endif
-		if (cpu_feature[4] & CPUID_VIA_HAS_ACE)
+		if (0 & cpu_feature[4] & CPUID_VIA_HAS_ACE)
 			aes_md_init(_via_impl);
-		else if (i386_has_sse && i386_has_sse2 &&
+		else if (0 & i386_has_sse && i386_has_sse2 &&
 		(cpu_feature[1] & CPUID2_SSE3) &&
 		(cpu_feature[1] & CPUID2_SSSE3))
 			aes_md_init(_ssse3_impl);
-		else if (i386_has_sse && i386_has_sse2)
+		else if (0 & i386_has_sse && i386_has_sse2)
 			aes_md_init(_sse2_impl);
 	} else {
 		/*



CVS commit: src/sys/arch/x86/x86

2020-07-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jul 19 13:58:27 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
we're already in an #ifdef USER_LDT block, so no need to #ifdef again


To generate a diff of this commit:
cvs rdiff -u -r1.400 -r1.401 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.400 src/sys/arch/x86/x86/pmap.c:1.401
--- src/sys/arch/x86/x86/pmap.c:1.400	Tue Jul 14 00:45:53 2020
+++ src/sys/arch/x86/x86/pmap.c	Sun Jul 19 13:58:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.400 2020/07/14 00:45:53 yamaguchi Exp $	*/
+/*	$NetBSD: pmap.c,v 1.401 2020/07/19 13:58:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.400 2020/07/14 00:45:53 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.401 2020/07/19 13:58:26 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3326,7 +3326,7 @@ pmap_ldt_xcall(void *arg1, void *arg2)
 	kpreempt_disable();
 	pm = arg1;
 	if (curcpu()->ci_pmap == pm) {
-#if defined(SVS) && defined(USER_LDT)
+#if defined(SVS)
 		if (svs_enabled) {
 			svs_ldt_sync(pm);
 		} else



CVS commit: src/sys/arch/x86/x86

2020-07-14 Thread Lars Reichardt
Module Name:src
Committed By:   para
Date:   Tue Jul 14 15:59:21 UTC 2020

Modified Files:
src/sys/arch/x86/x86: idt.c

Log Message:
mark diagused variable as such

fixing non DIAGNOSTIC builds


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/x86/x86/idt.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/idt.c
diff -u src/sys/arch/x86/x86/idt.c:1.13 src/sys/arch/x86/x86/idt.c:1.14
--- src/sys/arch/x86/x86/idt.c:1.13	Tue Jul 14 00:45:53 2020
+++ src/sys/arch/x86/x86/idt.c	Tue Jul 14 15:59:21 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: idt.c,v 1.13 2020/07/14 00:45:53 yamaguchi Exp $	*/
+/*	$NetBSD: idt.c,v 1.14 2020/07/14 15:59:21 para Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2009 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.13 2020/07/14 00:45:53 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.14 2020/07/14 15:59:21 para Exp $");
 
 #include "opt_pcpu_idt.h"
 
@@ -198,7 +198,7 @@ void
 idt_vec_set(struct idt_vec *iv, int vec, void (*function)(void))
 {
 	idt_descriptor_t *idt;
-	char *idt_allocmap = iv->iv_allocmap;
+	char *idt_allocmap __diagused = iv->iv_allocmap;
 
 	KASSERT(idt_allocmap[vec] == 1);
 	idt = iv->iv_idt;



CVS commit: src/sys/arch/x86/x86

2020-07-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul 13 16:51:51 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Limit x86 fpu_kern_enter/leave to IPL_VM or below.

There are no users of crypto at IPL_SCHED or IPL_HIGH as far as I
know, and although we generally limit the amount of time spent in any
one crypto operation -- e.g., cgd is usually limited to processing
512 or 4096 bytes at a time -- it's better not to block IPL_SCHED and
IPL_HIGH interrupts at all.  This should make ddb a little more
accessible during crypto-heavy workloads.

This means the aes_* API cannot be used at IPL_SCHED or IPL_HIGH; the
same will go for any new crypto subsystems, like the ChaCha and
Poly1305 ones I'm drafting.  It might be better to prohibit them
altogether in hard interrupt context, but right now cprng_fast and
cprng_strong are both technically allowed at IPL_VM and are sometimes
used there (e.g., for opencrypto CBC IV generation).

KASSERT the ilevel to detect violation of this constraint in case I'm
wrong.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.67 src/sys/arch/x86/x86/fpu.c:1.68
--- src/sys/arch/x86/x86/fpu.c:1.67	Mon Jul  6 18:30:48 2020
+++ src/sys/arch/x86/x86/fpu.c	Mon Jul 13 16:51:51 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.67 2020/07/06 18:30:48 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.68 2020/07/13 16:51:51 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.67 2020/07/06 18:30:48 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.68 2020/07/13 16:51:51 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -370,11 +370,14 @@ fpu_lwp_abandon(struct lwp *l)
 /*
  * fpu_kern_enter()
  *
- *	Begin using the FPU.  Raises to splhigh, disabling all
+ *	Begin using the FPU.  Raises to splvm, disabling most
  *	interrupts and rendering the thread non-preemptible; caller
  *	should not use this for long periods of time, and must call
  *	fpu_kern_leave() afterward.  Non-recursive -- you cannot call
  *	fpu_kern_enter() again without calling fpu_kern_leave() first.
+ *
+ *	Must be used only at IPL_VM or below -- never in IPL_SCHED or
+ *	IPL_HIGH interrupt handlers.
  */
 void
 fpu_kern_enter(void)
@@ -383,9 +386,10 @@ fpu_kern_enter(void)
 	struct cpu_info *ci;
 	int s;
 
-	s = splhigh();
+	s = splvm();
 
 	ci = curcpu();
+	KASSERTMSG(ci->ci_ilevel <= IPL_VM, "ilevel=%d", ci->ci_ilevel);
 	KASSERT(ci->ci_kfpu_spl == -1);
 	ci->ci_kfpu_spl = s;
 
@@ -423,7 +427,7 @@ fpu_kern_leave(void)
 	struct cpu_info *ci = curcpu();
 	int s;
 
-	KASSERT(ci->ci_ilevel == IPL_HIGH);
+	KASSERT(ci->ci_ilevel == IPL_VM);
 	KASSERT(ci->ci_kfpu_spl != -1);
 
 	/*



CVS commit: src/sys/arch/x86/x86

2020-07-10 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Fri Jul 10 06:15:23 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu_subr.c

Log Message:
Add missing NetBSD RCS Id.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/x86/x86/identcpu_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu_subr.c
diff -u src/sys/arch/x86/x86/identcpu_subr.c:1.6 src/sys/arch/x86/x86/identcpu_subr.c:1.7
--- src/sys/arch/x86/x86/identcpu_subr.c:1.6	Tue Jun  9 05:07:13 2020
+++ src/sys/arch/x86/x86/identcpu_subr.c	Fri Jul 10 06:15:23 2020
@@ -1,3 +1,5 @@
+/* $NetBSD: identcpu_subr.c,v 1.7 2020/07/10 06:15:23 msaitoh Exp $ */
+
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -33,7 +35,7 @@
  * See src/usr.sbin/cpuctl/{Makefile, arch/i386.c}).
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.6 2020/06/09 05:07:13 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.7 2020/07/10 06:15:23 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "lapic.h"



CVS commit: src/sys/arch/x86/x86

2020-07-09 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Thu Jul  9 22:45:54 UTC 2020

Modified Files:
src/sys/arch/x86/x86: x86_autoconf.c

Log Message:
Adapt to proplib api changes


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 src/sys/arch/x86/x86/x86_autoconf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/x86_autoconf.c
diff -u src/sys/arch/x86/x86/x86_autoconf.c:1.83 src/sys/arch/x86/x86/x86_autoconf.c:1.84
--- src/sys/arch/x86/x86/x86_autoconf.c:1.83	Tue Jul  7 16:14:23 2020
+++ src/sys/arch/x86/x86/x86_autoconf.c	Thu Jul  9 22:45:54 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_autoconf.c,v 1.83 2020/07/07 16:14:23 thorpej Exp $	*/
+/*	$NetBSD: x86_autoconf.c,v 1.84 2020/07/09 22:45:54 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -35,7 +35,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_autoconf.c,v 1.83 2020/07/07 16:14:23 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_autoconf.c,v 1.84 2020/07/09 22:45:54 jdolecek Exp $");
 
 #include 
 #include 
@@ -579,7 +579,7 @@ device_register(device_t dev, void *aux)
 		int i;
 
 		for (i = 0; imcsmb_device_permitlist[i] != NULL; i++) {
-			prop_string_t pstr = prop_string_create_cstring_nocopy(
+			prop_string_t pstr = prop_string_create_nocopy(
 			imcsmb_device_permitlist[i]);
 			(void) prop_array_add(permitlist, pstr);
 			prop_object_release(pstr);
@@ -587,7 +587,7 @@ device_register(device_t dev, void *aux)
 		(void) prop_dictionary_set(props,
 	   I2C_PROP_INDIRECT_DEVICE_PERMITLIST,
 	   permitlist);
-		(void) prop_dictionary_set_cstring_nocopy(props,
+		(void) prop_dictionary_set_string_nocopy(props,
 	   I2C_PROP_INDIRECT_PROBE_STRATEGY,
 	   I2C_PROBE_STRATEGY_NONE);
 	}



CVS commit: src/sys/arch/x86/x86

2020-07-06 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul  6 18:30:48 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Restore the lwp's fpu state, not zeros, and leave with fpu enabled.

We need to clear the fpu state anyway because it is likely to contain
secrets at this point.  Previously we set it to zeros, and then issued
stts to disable the fpu in order to detect the mistake of further use
of the fpu in kernel.  But there must be some path I haven't identified
yet that doesn't do fpu_handle_deferred, leading to fpudna panics.

In any case, there's no benefit to restoring the fpu state twice
(once with zeros and once with the real data).  The downside is,
although this avoids spurious fpudna traps, using fpu_kern_enter in a
softint has the side effect that -- until the next userland context
switch triggering stts -- we no longer detect misuse of fpu in the
kernel in that lwp.  This will serve for now, but we should find
another way to issue clts/stts judiciously to detect such misuse.

May improve the continued symptoms of
https://mail-index.netbsd.org/current-users/2020/07/02/msg039051.html
although may not fix everything.


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.67 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.66 src/sys/arch/x86/x86/fpu.c:1.67
--- src/sys/arch/x86/x86/fpu.c:1.66	Mon Jul  6 01:08:15 2020
+++ src/sys/arch/x86/x86/fpu.c	Mon Jul  6 18:30:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.66 2020/07/06 01:08:15 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.67 2020/07/06 18:30:48 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.66 2020/07/06 01:08:15 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.67 2020/07/06 18:30:48 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -417,6 +417,9 @@ void
 fpu_kern_leave(void)
 {
 	static const union savefpu zero_fpu __aligned(64);
+	const union savefpu *savefpu;
+	struct lwp *l = curlwp;
+	struct pcb *pcb;
 	struct cpu_info *ci = curcpu();
 	int s;
 
@@ -424,17 +427,18 @@ fpu_kern_leave(void)
 	KASSERT(ci->ci_kfpu_spl != -1);
 
 	/*
-	 * Zero the fpu registers; otherwise we might leak secrets
-	 * through Spectre-class attacks to userland, even if there are
-	 * no bugs in fpu state management.
-	 */
-	fpu_area_restore(_fpu, x86_xsave_features);
-
-	/*
-	 * Set CR0_TS again so that the kernel can't accidentally use
-	 * the FPU.
+	 * Restore the FPU state immediately to avoid leaking any
+	 * kernel secrets, or zero it if this is a kthread.
 	 */
-	stts();
+	if ((l->l_pflag & LP_INTR) && (l->l_switchto != NULL))
+		l = l->l_switchto;
+	if (l->l_flag & LW_SYSTEM) {
+		savefpu = _fpu;
+	} else {
+		pcb = lwp_getpcb(l);
+		savefpu = >pcb_savefpu;
+	}
+	fpu_area_restore(savefpu, x86_xsave_features);
 
 	s = ci->ci_kfpu_spl;
 	ci->ci_kfpu_spl = -1;



CVS commit: src/sys/arch/x86/x86

2020-07-05 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jul  6 01:08:15 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Fix race in fpu save with fpu_kern_enter in softint.

Likely source of:

https://mail-index.netbsd.org/current-users/2020/07/02/msg039051.html


To generate a diff of this commit:
cvs rdiff -u -r1.65 -r1.66 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.65 src/sys/arch/x86/x86/fpu.c:1.66
--- src/sys/arch/x86/x86/fpu.c:1.65	Sun Jun 14 16:12:05 2020
+++ src/sys/arch/x86/x86/fpu.c	Mon Jul  6 01:08:15 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.65 2020/06/14 16:12:05 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.66 2020/07/06 01:08:15 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.65 2020/06/14 16:12:05 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.66 2020/07/06 01:08:15 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -126,6 +126,8 @@ __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.65
 #define stts() HYPERVISOR_fpu_taskswitch(1)
 #endif
 
+static void fpu_area_do_save(void *, uint64_t);
+
 void fpu_handle_deferred(void);
 void fpu_switch(struct lwp *, struct lwp *);
 
@@ -155,8 +157,24 @@ fpu_save_lwp(struct lwp *l)
 	kpreempt_disable();
 	if (l->l_md.md_flags & MDL_FPU_IN_CPU) {
 		KASSERT((l->l_flag & LW_SYSTEM) == 0);
-		fpu_area_save(area, x86_xsave_features);
+
+		/*
+		 * Order is important, in case we are interrupted and
+		 * the interrupt calls fpu_kern_enter, triggering
+		 * reentry of fpu_save_lwp:
+		 *
+		 * 1. Save FPU state.
+		 * 2. Note FPU state has been saved.
+		 * 3. Disable FPU access so the kernel doesn't
+		 *accidentally use it.
+		 *
+		 * Steps (1) and (2) are both idempotent until step
+		 * (3), after which point attempting to save the FPU
+		 * state will trigger #NM/fpudna fault.
+		 */
+		fpu_area_do_save(area, x86_xsave_features);
 		l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
+		stts();
 	}
 	kpreempt_enable();
 }
@@ -245,8 +263,8 @@ fpu_errata_amd(void)
 	fldummy();
 }
 
-void
-fpu_area_save(void *area, uint64_t xsave_features)
+static void
+fpu_area_do_save(void *area, uint64_t xsave_features)
 {
 	switch (x86_fpu_save) {
 	case FPU_SAVE_FSAVE:
@@ -262,7 +280,13 @@ fpu_area_save(void *area, uint64_t xsave
 		xsaveopt(area, xsave_features);
 		break;
 	}
+}
+
+void
+fpu_area_save(void *area, uint64_t xsave_features)
+{
 
+	fpu_area_do_save(area, xsave_features);
 	stts();
 }
 



CVS commit: src/sys/arch/x86/x86

2020-07-04 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Sat Jul  4 09:03:55 UTC 2020

Modified Files:
src/sys/arch/x86/x86: idt.c

Log Message:
Fix unset_idtgate() for XENPV, pointed out by yamaguchi@


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/x86/x86/idt.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/idt.c
diff -u src/sys/arch/x86/x86/idt.c:1.11 src/sys/arch/x86/x86/idt.c:1.12
--- src/sys/arch/x86/x86/idt.c:1.11	Mon Jun 17 06:38:30 2019
+++ src/sys/arch/x86/x86/idt.c	Sat Jul  4 09:03:54 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: idt.c,v 1.11 2019/06/17 06:38:30 msaitoh Exp $	*/
+/*	$NetBSD: idt.c,v 1.12 2020/07/04 09:03:54 bouyer Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2009 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.11 2019/06/17 06:38:30 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.12 2020/07/04 09:03:54 bouyer Exp $");
 
 #include 
 #include 
@@ -132,7 +132,7 @@ void
 unset_idtgate(struct trap_info *xen_idd)
 {
 #if defined(__x86_64__)
-	vaddr_t xen_idt_vaddr = ((vaddr_t) xen_idd) & PAGE_MASK;
+	vaddr_t xen_idt_vaddr = ((vaddr_t) xen_idd) & ~PAGE_MASK;
 
 	/* Make it writeable, so we can update the values. */
 	pmap_changeprot_local(xen_idt_vaddr, VM_PROT_READ | VM_PROT_WRITE);



CVS commit: src/sys/arch/x86/x86

2020-07-03 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Jul  3 16:17:25 UTC 2020

Modified Files:
src/sys/arch/x86/x86: vm_machdep.c

Log Message:
In cpu_uarea_{alloc,free}:

 - My previous change in this file was not correct, kremove does not free
   the underlying PA, which caused a very slow leak under memory pressure.
   Rework to correctly free the PA.
 - Add a second redzone, this time after the stack, to catch several stack
   overflows. The main concern is read overflows which leak the heap that
   follows the stack.
 - UVM_KMF_WAITVA doesn't fail, so remove error check.
 - Add KASSERTs.


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/sys/arch/x86/x86/vm_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/vm_machdep.c
diff -u src/sys/arch/x86/x86/vm_machdep.c:1.42 src/sys/arch/x86/x86/vm_machdep.c:1.43
--- src/sys/arch/x86/x86/vm_machdep.c:1.42	Tue Mar 17 17:18:49 2020
+++ src/sys/arch/x86/x86/vm_machdep.c	Fri Jul  3 16:17:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: vm_machdep.c,v 1.42 2020/03/17 17:18:49 maxv Exp $	*/
+/*	$NetBSD: vm_machdep.c,v 1.43 2020/07/03 16:17:24 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986 The Regents of the University of California.
@@ -80,7 +80,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.42 2020/03/17 17:18:49 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.43 2020/07/03 16:17:24 maxv Exp $");
 
 #include "opt_mtrr.h"
 
@@ -349,29 +349,56 @@ vunmapbuf(struct buf *bp, vsize_t len)
 }
 
 #ifdef __HAVE_CPU_UAREA_ROUTINES
+/*
+ * Layout of the uarea:
+ *Page[0]= PCB
+ *Page[1]= RedZone
+ *Page[2]= Stack
+ *Page[...]  = Stack
+ *Page[UPAGES-1] = Stack
+ *Page[UPAGES]   = RedZone
+ * There is a redzone at the beginning of the stack, and another one at the
+ * end. The former is to protect against deep recursions that could corrupt
+ * the PCB, the latter to protect against severe stack overflows.
+ */
 void *
 cpu_uarea_alloc(bool system)
 {
-	vaddr_t va;
+	vaddr_t base, va;
+	paddr_t pa;
 
-	va = uvm_km_alloc(kernel_map, USPACE, 0, UVM_KMF_WIRED|UVM_KMF_WAITVA);
-	if (va == 0)
-		return NULL;
+	base = uvm_km_alloc(kernel_map, USPACE + PAGE_SIZE, 0,
+	UVM_KMF_WIRED|UVM_KMF_WAITVA);
+
+	/* Page[1] = RedZone */
+	va = base + PAGE_SIZE;
+	if (!pmap_extract(pmap_kernel(), va, )) {
+		panic("%s: impossible, Page[1] unmapped", __func__);
+	}
+	pmap_kremove(va, PAGE_SIZE);
+	uvm_pagefree(PHYS_TO_VM_PAGE(pa));
+
+	/* Page[UPAGES] = RedZone */
+	va = base + USPACE;
+	if (!pmap_extract(pmap_kernel(), va, )) {
+		panic("%s: impossible, Page[UPAGES] unmapped", __func__);
+	}
+	pmap_kremove(va, PAGE_SIZE);
+	uvm_pagefree(PHYS_TO_VM_PAGE(pa));
 
-	/*
-	 * The second page is unmapped, and acts as a guard page between the
-	 * PCB (first page) and the stack (rest of the pages).
-	 */
-	pmap_kremove(va + PAGE_SIZE, PAGE_SIZE);
 	pmap_update(pmap_kernel());
 
-	return (void *)va;
+	return (void *)base;
 }
 
 bool
 cpu_uarea_free(void *addr)
 {
-	uvm_km_free(kernel_map, (vaddr_t)addr, USPACE, UVM_KMF_WIRED);
+	vaddr_t base = (vaddr_t)addr;
+
+	KASSERT(!pmap_extract(pmap_kernel(), base + PAGE_SIZE, NULL));
+	KASSERT(!pmap_extract(pmap_kernel(), base + USPACE, NULL));
+	uvm_km_free(kernel_map, base, USPACE + PAGE_SIZE, UVM_KMF_WIRED);
 	return true;
 }
 #endif /* __HAVE_CPU_UAREA_ROUTINES */



CVS commit: src/sys/arch/x86/x86

2020-06-25 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Thu Jun 25 17:24:31 UTC 2020

Modified Files:
src/sys/arch/x86/x86: multiboot2.c

Log Message:
rearrange code to remove need for the scratch space variable, simply put
the data to destination bootinfo buffer directly

XXX compile tested only, needs confirmation that it still works


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/x86/x86/multiboot2.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/multiboot2.c
diff -u src/sys/arch/x86/x86/multiboot2.c:1.5 src/sys/arch/x86/x86/multiboot2.c:1.6
--- src/sys/arch/x86/x86/multiboot2.c:1.5	Wed Jun 24 22:28:07 2020
+++ src/sys/arch/x86/x86/multiboot2.c	Thu Jun 25 17:24:31 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: multiboot2.c,v 1.5 2020/06/24 22:28:07 jdolecek Exp $	*/
+/*	$NetBSD: multiboot2.c,v 1.6 2020/06/25 17:24:31 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: multiboot2.c,v 1.5 2020/06/24 22:28:07 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: multiboot2.c,v 1.6 2020/06/25 17:24:31 jdolecek Exp $");
 
 #include "opt_multiboot.h"
 
@@ -115,7 +115,6 @@ char multiboot_info[16384] = "\0\0\0\0";
 bool multiboot2_enabled = false;
 bool has_syms = false;
 struct multiboot_symbols Multiboot_Symbols;
-static char bimbuf[16384];
 
 #define RELOC(type, x) ((type)((vaddr_t)(x) - KERNBASE))
 
@@ -346,16 +345,13 @@ multiboot2_pre_reloc(char *mbi)
 	return;
 }
 
-static void
-bootinfo_add(struct btinfo_common *item, int type, int len)
+static struct btinfo_common *
+bootinfo_init(int type, int len)
 {
 	int i;  
 	struct bootinfo *bip = (struct bootinfo *)
 	vaddr_t data;
 
-	item->type = type;
-	item->len = len;
-
 	data = (vaddr_t)>bi_data;
 	for (i = 0; i < bip->bi_nentries; i++) {
 		struct btinfo_common *tmp;
@@ -364,12 +360,29 @@ bootinfo_add(struct btinfo_common *item,
 		data += tmp->len;
 	}
 	if (data + len < (vaddr_t)>bi_data + sizeof(bip->bi_data)) {
-		memcpy((void *)data, item, len);
+		/* Initialize the common part */
+		struct btinfo_common *item = (struct btinfo_common *)data;
+		item->type = type;
+		item->len = len;
 		bip->bi_nentries++;
+		return item;
+	} else {
+		return NULL;
 	}
 }
 
 static void
+bootinfo_add(struct btinfo_common *item, int type, int len)
+{
+	struct btinfo_common *bip = bootinfo_init(type, len);
+	if (bip == NULL)
+		return;
+
+	/* Copy the data after the common part over */
+	memcpy([1], [1], len - sizeof(*item));
+}
+
+static void
 mbi_cmdline(struct multiboot_tag_string *mbt)
 {
 	char *cmdline = mbt->string;
@@ -450,11 +463,11 @@ mbi_modules(char *mbi, uint32_t mbi_size
 	struct btinfo_modulelist *bim;
 
 	bim_len = sizeof(*bim) + (module_count * sizeof(*bie));
-	if (bim_len > sizeof(bimbuf))
+	bim = (struct btinfo_modulelist *)bootinfo_init(BTINFO_MODULELIST,
+	bim_len);
+	if (bim == NULL)
 		return;
 
-	bim = (struct btinfo_modulelist *)bimbuf;
-
 	bim->num = module_count;
 	bim->endpa = end;
 
@@ -474,11 +487,6 @@ mbi_modules(char *mbi, uint32_t mbi_size
 
 		bie++;
 	}
-
-	bootinfo_add((struct btinfo_common *),
-   BTINFO_MODULELIST, bim_len);
-
-	return;
 }
 
 static void
@@ -518,12 +526,24 @@ static void
 mbi_mmap(struct multiboot_tag_mmap *mbt)
 {
 	struct btinfo_memmap *bim;
+	int num;
 	char *cp;
 
 	if (mbt->entry_version != 0)
 		return;
 
-	bim = (struct btinfo_memmap *)bimbuf;
+	/* Determine size */
+	num = 0;
+	for (cp = (char *)(mbt + 1);
+	 cp - (char *)mbt < mbt->size;
+	 cp += mbt->entry_size) {
+		num++;
+	}
+
+	bim = (struct btinfo_memmap *)bootinfo_init(BTINFO_MEMMAP,
+	sizeof(num) + num * sizeof(struct bi_memmap_entry));
+	if (bim == NULL)
+		return;
 	bim->num = 0;
 
 	for (cp = (char *)(mbt + 1);
@@ -557,15 +577,9 @@ mbi_mmap(struct multiboot_tag_mmap *mbt)
 		}
 
 		bim->num++;
-
-		if ((char*)>entry[bim->num] - (char *)bim > sizeof(bimbuf))
-			break;
 	}
 
-	bootinfo_add((struct btinfo_common *)bim, BTINFO_MEMMAP,
-	(char*)>entry[bim->num] - (char *)bim);
-
-	return;
+	KASSERT(bim->num == num);
 }
 
 static void
@@ -614,26 +628,22 @@ mbi_efi64(struct multiboot_tag_efi64 *mb
 static void
 mbi_efi_mmap(struct multiboot_tag_efi_mmap *mbt)
 {
-	struct btinfo_efimemmap *bie = (struct btinfo_efimemmap *)bimbuf;
+	struct btinfo_efimemmap *bie;
 	size_t bie_len;
 
 	if (mbt->descr_vers != 0)
-		goto out;
+		return;
 
 	bie_len = sizeof(*bie) + mbt->size - sizeof(*mbt);
-	if (bie_len > sizeof(bimbuf))
-		goto out;
+	bie = (struct btinfo_efimemmap *)bootinfo_init(BTINFO_EFIMEMMAP,
+	bie_len);
+	if (bie == NULL)
+		return;
 
 	bie->num = (mbt->size - sizeof(*mbt)) / mbt->descr_size;
 	bie->version = mbt->descr_vers;
 	bie->size = mbt->descr_size;
 	memcpy(bie->memmap, mbt + 1, mbt->size - sizeof(*mbt));
-
-	bootinfo_add((struct btinfo_common *)bie,
-   BTINFO_EFIMEMMAP, 

CVS commit: src/sys/arch/x86/x86

2020-06-24 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Wed Jun 24 22:28:08 UTC 2020

Modified Files:
src/sys/arch/x86/x86: multiboot2.c

Log Message:
don't try allocating 16KB of scratch space on stack

it's too early for kmem_alloc(), so use static variable in BSS; it's used
post reloc, so don't need to use the RELOC() macros

XXX compile-tested only on i386


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/x86/multiboot2.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/multiboot2.c
diff -u src/sys/arch/x86/x86/multiboot2.c:1.4 src/sys/arch/x86/x86/multiboot2.c:1.5
--- src/sys/arch/x86/x86/multiboot2.c:1.4	Thu Jan 30 01:49:44 2020
+++ src/sys/arch/x86/x86/multiboot2.c	Wed Jun 24 22:28:07 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: multiboot2.c,v 1.4 2020/01/30 01:49:44 manu Exp $	*/
+/*	$NetBSD: multiboot2.c,v 1.5 2020/06/24 22:28:07 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: multiboot2.c,v 1.4 2020/01/30 01:49:44 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: multiboot2.c,v 1.5 2020/06/24 22:28:07 jdolecek Exp $");
 
 #include "opt_multiboot.h"
 
@@ -115,7 +115,7 @@ char multiboot_info[16384] = "\0\0\0\0";
 bool multiboot2_enabled = false;
 bool has_syms = false;
 struct multiboot_symbols Multiboot_Symbols;
-
+static char bimbuf[16384];
 
 #define RELOC(type, x) ((type)((vaddr_t)(x) - KERNBASE))
 
@@ -448,7 +448,6 @@ mbi_modules(char *mbi, uint32_t mbi_size
 	size_t bim_len;
 	struct bi_modulelist_entry *bie;
 	struct btinfo_modulelist *bim;
-	char bimbuf[16384];
 
 	bim_len = sizeof(*bim) + (module_count * sizeof(*bie));
 	if (bim_len > sizeof(bimbuf))
@@ -518,7 +517,6 @@ mbi_bootdev(struct multiboot_tag_bootdev
 static void
 mbi_mmap(struct multiboot_tag_mmap *mbt)
 {
-	char bimbuf[16384];
 	struct btinfo_memmap *bim;
 	char *cp;
 
@@ -616,15 +614,14 @@ mbi_efi64(struct multiboot_tag_efi64 *mb
 static void
 mbi_efi_mmap(struct multiboot_tag_efi_mmap *mbt)
 {
-	char biebuf[16384];
-	struct btinfo_efimemmap *bie = (struct btinfo_efimemmap *)biebuf;
+	struct btinfo_efimemmap *bie = (struct btinfo_efimemmap *)bimbuf;
 	size_t bie_len;
 
 	if (mbt->descr_vers != 0)
 		goto out;
 
 	bie_len = sizeof(*bie) + mbt->size - sizeof(*mbt);
-	if (bie_len > sizeof(biebuf))
+	if (bie_len > sizeof(bimbuf))
 		goto out;
 
 	bie->num = (mbt->size - sizeof(*mbt)) / mbt->descr_size;



CVS commit: src/sys/arch/x86/x86

2020-06-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jun 15 01:24:21 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu_rng.c

Log Message:
Count down bits of entropy, not bits of data, in x86 cpu_rng.

Fixes logic in this loop for XSTORERNG on VIA CPUs, which are deemed
to have half the entropy per bit of data as RDSEED on Intel CPUs, so
that it gathers enough entropy on the first request, not on the
second request.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/x86/x86/cpu_rng.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.16 src/sys/arch/x86/x86/cpu_rng.c:1.17
--- src/sys/arch/x86/x86/cpu_rng.c:1.16	Mon Jun 15 01:23:44 2020
+++ src/sys/arch/x86/x86/cpu_rng.c	Mon Jun 15 01:24:20 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.16 2020/06/15 01:23:44 riastradh Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.17 2020/06/15 01:24:20 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -254,7 +254,7 @@ cpu_rng_get(size_t nbytes, void *cookie)
 	uint64_t buf[2*N];
 	unsigned i, nbits = 0;
 
-	for (; nbytes; nbytes -= MIN(nbytes, sizeof buf)) {
+	while (nbytes) {
 		/*
 		 * The fraction of outputs this rejects in correct
 		 * operation is 1/2^256, which is close enough to zero
@@ -269,6 +269,7 @@ cpu_rng_get(size_t nbytes, void *cookie)
 			nbits = 0;
 		}
 		rnd_add_data_sync(_rng_source, buf, sizeof buf, nbits);
+		nbytes -= MIN(MIN(nbytes, sizeof buf), MAX(1, 8*nbits));
 	}
 #undef N
 }



CVS commit: src/sys/arch/x86/x86

2020-06-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jun 15 01:23:44 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu_rng.c

Log Message:
Use x86_read_psl/x86_disable_intr/x86_read_psl to defer interrupts.

Using x86_disable_intr/x86_enable_intr causes a bit of a snag when we
try it early at boot before we're ready to handle interrupts, because
it has the effect of enabling interrupts!

Fixes instant reset at boot on VIA CPUs.  The instant reset on boot
is new since the entropy rework, which initialized the x86 CPU RNG
earlier than before, but in principle this could also cause other
problems while not early at boot too.

XXX pullup


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/x86/x86/cpu_rng.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.15 src/sys/arch/x86/x86/cpu_rng.c:1.16
--- src/sys/arch/x86/x86/cpu_rng.c:1.15	Fri Jun  5 21:48:03 2020
+++ src/sys/arch/x86/x86/cpu_rng.c	Mon Jun 15 01:23:44 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.15 2020/06/05 21:48:03 kamil Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.16 2020/06/15 01:23:44 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -186,6 +186,7 @@ cpu_rng_rdseed_rdrand(uint64_t *out)
 static size_t
 cpu_rng_via(uint64_t *out)
 {
+	u_long psl;
 	uint32_t creg0, rndsts;
 
 	/*
@@ -199,9 +200,9 @@ cpu_rng_via(uint64_t *out)
 	 * even if such a fault is generated.
 	 *
 	 * XXX can this really happen if we don't use "rep xstorrng"?
-	 *
 	 */
 	kpreempt_disable();
+	psl = x86_read_psl();
 	x86_disable_intr();
 	creg0 = rcr0();
 	lcr0(creg0 & ~(CR0_EM|CR0_TS)); /* Permit access to SIMD/FPU path */
@@ -215,7 +216,7 @@ cpu_rng_via(uint64_t *out)
 	: "=a" (rndsts), "+D" (out) : "d" (0) : "memory");
 	/* Put CR0 back how it was */
 	lcr0(creg0);
-	x86_enable_intr();
+	x86_write_psl(psl);
 	kpreempt_enable();
 
 	/*



CVS commit: src/sys/arch/x86/x86

2020-06-14 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Jun 14 23:24:20 UTC 2020

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
tsc_get_timecount(): disable the "clock goes backwards" check on i386 for
the moment since it requires 64-bit store to be atomic because of nesting
via interrupt.


To generate a diff of this commit:
cvs rdiff -u -r1.49 -r1.50 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.49 src/sys/arch/x86/x86/tsc.c:1.50
--- src/sys/arch/x86/x86/tsc.c:1.49	Sat Jun 13 23:58:52 2020
+++ src/sys/arch/x86/x86/tsc.c	Sun Jun 14 23:24:20 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.49 2020/06/13 23:58:52 ad Exp $	*/
+/*	$NetBSD: tsc.c,v 1.50 2020/06/14 23:24:20 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.49 2020/06/13 23:58:52 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.50 2020/06/14 23:24:20 ad Exp $");
 
 #include 
 #include 
@@ -357,6 +357,7 @@ tsc_delay(unsigned int us)
 static u_int
 tsc_get_timecount(struct timecounter *tc)
 {
+#ifdef _LP64 /* requires atomic 64-bit store */
 	static __cpu_simple_lock_t lock = __SIMPLELOCK_UNLOCKED;
 	static int lastwarn;
 	uint64_t cur, prev;
@@ -385,4 +386,7 @@ tsc_get_timecount(struct timecounter *tc
 	}
 	l->l_md.md_tsc = cur;
 	return (uint32_t)cur;
+#else
+	return cpu_counter32();
+#endif
 }



CVS commit: src/sys/arch/x86/x86

2020-06-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jun 14 23:20:15 UTC 2020

Modified Files:
src/sys/arch/x86/x86: via_padlock.c

Log Message:
padlock(4): Don't use prev msg's last block as IV for next msg in CBC.

This violates the security contract of the CBC construction, which
requires that the IV be unpredictable in advance; an adaptive adversary
can exploit this to verify plaintext guesses.

XXX Compile-tested only.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/arch/x86/x86/via_padlock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/via_padlock.c
diff -u src/sys/arch/x86/x86/via_padlock.c:1.28 src/sys/arch/x86/x86/via_padlock.c:1.29
--- src/sys/arch/x86/x86/via_padlock.c:1.28	Sat Mar  7 13:28:45 2020
+++ src/sys/arch/x86/x86/via_padlock.c	Sun Jun 14 23:20:15 2020
@@ -1,5 +1,5 @@
 /*	$OpenBSD: via.c,v 1.8 2006/11/17 07:47:56 tom Exp $	*/
-/*	$NetBSD: via_padlock.c,v 1.28 2020/03/07 13:28:45 maya Exp $ */
+/*	$NetBSD: via_padlock.c,v 1.29 2020/06/14 23:20:15 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2003 Jason Wright
@@ -20,7 +20,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.28 2020/03/07 13:28:45 maya Exp $");
+__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.29 2020/06/14 23:20:15 riastradh Exp $");
 
 #include 
 #include 
@@ -191,7 +191,6 @@ via_padlock_crypto_newsession(void *arg,
 C3_CRYPT_CWLO_KEYGEN_SW |
 C3_CRYPT_CWLO_NORMAL;
 
-			cprng_fast(ses->ses_iv, sizeof(ses->ses_iv));
 			ses->ses_klen = c->cri_klen;
 			ses->ses_cw0 = cw0;
 
@@ -384,7 +383,7 @@ via_padlock_crypto_encdec(struct cryptop
 		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
 			memcpy(sc->op_iv, crd->crd_iv, 16);
 		else
-			memcpy(sc->op_iv, ses->ses_iv, 16);
+			cprng_fast(sc->op_iv, 16);
 
 		if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
 			if (crp->crp_flags & CRYPTO_F_IMBUF)
@@ -439,21 +438,6 @@ via_padlock_crypto_encdec(struct cryptop
 		memcpy((char *)crp->crp_buf + crd->crd_skip, sc->op_buf,
 		crd->crd_len);
 
-	/* copy out last block for use as next session IV */
-	if (crd->crd_flags & CRD_F_ENCRYPT) {
-		if (crp->crp_flags & CRYPTO_F_IMBUF)
-			m_copydata((struct mbuf *)crp->crp_buf,
-			crd->crd_skip + crd->crd_len - 16, 16,
-			ses->ses_iv);
-		else if (crp->crp_flags & CRYPTO_F_IOV)
-			cuio_copydata((struct uio *)crp->crp_buf,
-			crd->crd_skip + crd->crd_len - 16, 16,
-			ses->ses_iv);
-		else
-			memcpy(ses->ses_iv, (char *)crp->crp_buf +
-			crd->crd_skip + crd->crd_len - 16, 16);
-	}
-
 	if (sc->op_buf != NULL) {
 		memset(sc->op_buf, 0, crd->crd_len);
 		free(sc->op_buf, M_DEVBUF);



CVS commit: src/sys/arch/x86/x86

2020-06-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jun 13 19:01:11 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Add comments over fpu_kern_enter/leave.


To generate a diff of this commit:
cvs rdiff -u -r1.63 -r1.64 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.63 src/sys/arch/x86/x86/fpu.c:1.64
--- src/sys/arch/x86/x86/fpu.c:1.63	Sat Jun 13 19:00:18 2020
+++ src/sys/arch/x86/x86/fpu.c	Sat Jun 13 19:01:11 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.63 2020/06/13 19:00:18 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.64 2020/06/13 19:01:11 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.63 2020/06/13 19:00:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.64 2020/06/13 19:01:11 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -343,6 +343,15 @@ fpu_lwp_abandon(struct lwp *l)
 
 /* -- */
 
+/*
+ * fpu_kern_enter()
+ *
+ *	Begin using the FPU.  Raises to splhigh, disabling all
+ *	interrupts and rendering the thread non-preemptible; caller
+ *	should not use this for long periods of time, and must call
+ *	fpu_kern_leave() afterward.  Non-recursive -- you cannot call
+ *	fpu_kern_enter() again without calling fpu_kern_leave() first.
+ */
 void
 fpu_kern_enter(void)
 {
@@ -375,6 +384,11 @@ fpu_kern_enter(void)
 	clts();
 }
 
+/*
+ * fpu_kern_leave()
+ *
+ *	End using the FPU after fpu_kern_enter().
+ */
 void
 fpu_kern_leave(void)
 {



CVS commit: src/sys/arch/x86/x86

2020-06-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jun 13 19:00:18 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Zero the fpu registers on fpu_kern_leave.

Avoid Spectre-class attacks on any values left in them.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.62 src/sys/arch/x86/x86/fpu.c:1.63
--- src/sys/arch/x86/x86/fpu.c:1.62	Thu Jun  4 19:53:55 2020
+++ src/sys/arch/x86/x86/fpu.c	Sat Jun 13 19:00:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.62 2020/06/04 19:53:55 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.63 2020/06/13 19:00:18 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.62 2020/06/04 19:53:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.63 2020/06/13 19:00:18 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -378,6 +378,7 @@ fpu_kern_enter(void)
 void
 fpu_kern_leave(void)
 {
+	union savefpu zero_fpu __aligned(64);
 	struct cpu_info *ci = curcpu();
 	int s;
 
@@ -385,6 +386,14 @@ fpu_kern_leave(void)
 	KASSERT(ci->ci_kfpu_spl != -1);
 
 	/*
+	 * Zero the fpu registers; otherwise we might leak secrets
+	 * through Spectre-class attacks to userland, even if there are
+	 * no bugs in fpu state management.
+	 */
+	memset(_fpu, 0, sizeof(zero_fpu));
+	fpu_area_restore(_fpu, x86_xsave_features);
+
+	/*
 	 * Set CR0_TS again so that the kernel can't accidentally use
 	 * the FPU.
 	 */



CVS commit: src/sys/arch/x86/x86

2020-06-08 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Tue Jun  9 05:07:13 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu_subr.c

Log Message:
Add braces.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/x86/x86/identcpu_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu_subr.c
diff -u src/sys/arch/x86/x86/identcpu_subr.c:1.5 src/sys/arch/x86/x86/identcpu_subr.c:1.6
--- src/sys/arch/x86/x86/identcpu_subr.c:1.5	Tue Jun  9 05:06:27 2020
+++ src/sys/arch/x86/x86/identcpu_subr.c	Tue Jun  9 05:07:13 2020
@@ -33,7 +33,7 @@
  * See src/usr.sbin/cpuctl/{Makefile, arch/i386.c}).
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.5 2020/06/09 05:06:27 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.6 2020/06/09 05:07:13 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "lapic.h"
@@ -71,7 +71,7 @@ cpu_tsc_freq_cpuid(struct cpu_info *ci)
 	x86_cpuid(0x15, descs);
 	denominator = descs[0];
 	numerator = descs[1];
-	if ((denominator != 0) && numerator != 0) {
+	if ((denominator != 0) && (numerator != 0)) {
 		khz = 0;
 		if (descs[2] != 0)
 			khz = descs[2] / 1000;



CVS commit: src/sys/arch/x86/x86

2020-06-08 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Tue Jun  9 05:06:27 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu_subr.c

Log Message:
Remove debug printf.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/x86/identcpu_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu_subr.c
diff -u src/sys/arch/x86/x86/identcpu_subr.c:1.4 src/sys/arch/x86/x86/identcpu_subr.c:1.5
--- src/sys/arch/x86/x86/identcpu_subr.c:1.4	Tue May 12 06:32:05 2020
+++ src/sys/arch/x86/x86/identcpu_subr.c	Tue Jun  9 05:06:27 2020
@@ -33,7 +33,7 @@
  * See src/usr.sbin/cpuctl/{Makefile, arch/i386.c}).
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.4 2020/05/12 06:32:05 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.5 2020/06/09 05:06:27 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "lapic.h"
@@ -71,10 +71,7 @@ cpu_tsc_freq_cpuid(struct cpu_info *ci)
 	x86_cpuid(0x15, descs);
 	denominator = descs[0];
 	numerator = descs[1];
-	if ((denominator == 0) || numerator == 0) {
-		aprint_debug_dev(ci->ci_dev,
-		"TSC/core crystal clock ratio is not enumerated\n");
-	} else {
+	if ((denominator != 0) && numerator != 0) {
 		khz = 0;
 		if (descs[2] != 0)
 			khz = descs[2] / 1000;



CVS commit: src/sys/arch/x86/x86

2020-06-05 Thread Kamil Rytarowski
Module Name:src
Committed By:   kamil
Date:   Fri Jun  5 21:48:04 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu_rng.c

Log Message:
Change const unsigned to preprocessor define

Fixes GCC -O0 build with the stack protector.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/x86/x86/cpu_rng.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.14 src/sys/arch/x86/x86/cpu_rng.c:1.15
--- src/sys/arch/x86/x86/cpu_rng.c:1.14	Sun May 10 06:30:57 2020
+++ src/sys/arch/x86/x86/cpu_rng.c	Fri Jun  5 21:48:03 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.14 2020/05/10 06:30:57 maxv Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.15 2020/06/05 21:48:03 kamil Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -249,7 +249,7 @@ cpu_rng(enum cpu_rng_mode mode, uint64_t
 static void
 cpu_rng_get(size_t nbytes, void *cookie)
 {
-	const unsigned N = howmany(256, 64);
+#define N howmany(256, 64)
 	uint64_t buf[2*N];
 	unsigned i, nbits = 0;
 
@@ -269,6 +269,7 @@ cpu_rng_get(size_t nbytes, void *cookie)
 		}
 		rnd_add_data_sync(_rng_source, buf, sizeof buf, nbits);
 	}
+#undef N
 }
 
 void



CVS commit: src/sys/arch/x86/x86

2020-06-04 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Jun  4 19:53:55 UTC 2020

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
Call clts/stts in fpu_kern_enter/leave so they work.


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.61 src/sys/arch/x86/x86/fpu.c:1.62
--- src/sys/arch/x86/x86/fpu.c:1.61	Fri Jan 31 08:55:38 2020
+++ src/sys/arch/x86/x86/fpu.c	Thu Jun  4 19:53:55 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.61 2020/01/31 08:55:38 maxv Exp $	*/
+/*	$NetBSD: fpu.c,v 1.62 2020/06/04 19:53:55 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.61 2020/01/31 08:55:38 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.62 2020/06/04 19:53:55 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -365,6 +365,14 @@ fpu_kern_enter(void)
 	} else {
 		fpu_save_lwp(l);
 	}
+
+	/*
+	 * Clear CR0_TS, which fpu_save_lwp set if it saved anything --
+	 * otherwise the CPU will trap if we try to use the FPU under
+	 * the false impression that there has been a task switch since
+	 * the last FPU usage requiring that we save the FPU state.
+	 */
+	clts();
 }
 
 void
@@ -375,6 +383,13 @@ fpu_kern_leave(void)
 
 	KASSERT(ci->ci_ilevel == IPL_HIGH);
 	KASSERT(ci->ci_kfpu_spl != -1);
+
+	/*
+	 * Set CR0_TS again so that the kernel can't accidentally use
+	 * the FPU.
+	 */
+	stts();
+
 	s = ci->ci_kfpu_spl;
 	ci->ci_kfpu_spl = -1;
 	splx(s);



CVS commit: src/sys/arch/x86/x86

2020-06-02 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed Jun  3 00:27:46 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Revert most of 1.396 and go back to using memset()/memcpy().
Do not restore pageidlezero stuff though.


To generate a diff of this commit:
cvs rdiff -u -r1.397 -r1.398 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.397 src/sys/arch/x86/x86/pmap.c:1.398
--- src/sys/arch/x86/x86/pmap.c:1.397	Fri May 29 22:40:15 2020
+++ src/sys/arch/x86/x86/pmap.c	Wed Jun  3 00:27:46 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.397 2020/05/29 22:40:15 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.398 2020/06/03 00:27:46 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.397 2020/05/29 22:40:15 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.398 2020/06/03 00:27:46 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1297,8 +1297,7 @@ pmap_bootstrap(vaddr_t kva_start)
 	xen_dummy_user_pgd = xen_dummy_page - KERNBASE;
 
 	/* Zero fill it, the less checks in Xen it requires the better */
-	x86_stos((void *)(xen_dummy_user_pgd + KERNBASE), 0,
-	PAGE_SIZE / sizeof(long));
+	memset((void *)(xen_dummy_user_pgd + KERNBASE), 0, PAGE_SIZE);
 	/* Mark read-only */
 	HYPERVISOR_update_va_mapping(xen_dummy_user_pgd + KERNBASE,
 	pmap_pa2pte(xen_dummy_user_pgd) | PTE_P | pmap_pg_nx,
@@ -1547,7 +1546,7 @@ pmap_init_pcpu(void)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		x86_stos((void *)tmpva, 0, PAGE_SIZE / sizeof(long));
+		memset((void *)tmpva, 0, PAGE_SIZE);
 
 		L4_BASE[L4e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1561,7 +1560,7 @@ pmap_init_pcpu(void)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		x86_stos((void *)tmpva, 0, PAGE_SIZE / sizeof(long));
+		memset((void *)tmpva, 0, PAGE_SIZE);
 
 		L3_BASE[L3e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1576,7 +1575,7 @@ pmap_init_pcpu(void)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		x86_stos((void *)tmpva, 0, PAGE_SIZE / sizeof(long));
+		memset((void *)tmpva, 0, PAGE_SIZE);
 
 		L2_BASE[L2e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1668,7 +1667,7 @@ pmap_init_directmap(struct pmap *kpm)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		x86_stos((void *)tmpva, 0, PAGE_SIZE / sizeof(long));
+		memset((void *)tmpva, 0, PAGE_SIZE);
 
 		L4_BASE[L4e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -1682,7 +1681,7 @@ pmap_init_directmap(struct pmap *kpm)
 		pa = pmap_bootstrap_palloc(1);
 		*pte = (pa & PTE_FRAME) | pteflags;
 		pmap_update_pg(tmpva);
-		x86_stos((void *)tmpva, 0, PAGE_SIZE / sizeof(long));
+		memset((void *)tmpva, 0, PAGE_SIZE);
 
 		L3_BASE[L3e_idx+i] = pa | pteflags | PTE_A;
 	}
@@ -2632,7 +2631,7 @@ pmap_pdp_init(pd_entry_t *pdir)
 	int s;
 #endif
 
-	x86_stos(pdir, 0, PDP_SIZE * PAGE_SIZE / sizeof(long));
+	memset(pdir, 0, PDP_SIZE * PAGE_SIZE);
 
 	/*
 	 * NOTE: This is all done unlocked, but we will check afterwards
@@ -2675,8 +2674,8 @@ pmap_pdp_init(pd_entry_t *pdir)
 	/* Copy the kernel's top level PDE */
 	npde = nkptp[PTP_LEVELS - 1];
 
-	x86_movs([PDIR_SLOT_KERN], _BASE[PDIR_SLOT_KERN],
-	npde * sizeof(pd_entry_t) / sizeof(long));
+	memcpy([PDIR_SLOT_KERN], _BASE[PDIR_SLOT_KERN],
+	npde * sizeof(pd_entry_t));
 
 	if (VM_MIN_KERNEL_ADDRESS != KERNBASE) {
 		int idx = pl_i(KERNBASE, PTP_LEVELS);
@@ -3811,7 +3810,7 @@ void
 pmap_zero_page(paddr_t pa)
 {
 #if defined(__HAVE_DIRECT_MAP)
-	x86_stos((void *)PMAP_DIRECT_MAP(pa), 0, PAGE_SIZE / sizeof(long));
+	memset((void *)PMAP_DIRECT_MAP(pa), 0, PAGE_SIZE);
 #else
 #if defined(XENPV)
 	if (XEN_VERSION_SUPPORTED(3, 4))
@@ -3835,7 +3834,7 @@ pmap_zero_page(paddr_t pa)
 	pmap_pte_flush();
 	pmap_update_pg(zerova);		/* flush TLB */
 
-	x86_stos((void *)zerova, 0, PAGE_SIZE / sizeof(long));
+	memset((void *)zerova, 0, PAGE_SIZE);
 
 #if defined(DIAGNOSTIC) || defined(XENPV)
 	pmap_pte_set(zpte, 0);/* zap ! */
@@ -3853,7 +3852,7 @@ pmap_copy_page(paddr_t srcpa, paddr_t ds
 	vaddr_t srcva = PMAP_DIRECT_MAP(srcpa);
 	vaddr_t dstva = PMAP_DIRECT_MAP(dstpa);
 
-	x86_movs((void *)dstva, (void *)srcva, PAGE_SIZE / sizeof(long));
+	memcpy((void *)dstva, (void *)srcva, PAGE_SIZE);
 #else
 #if defined(XENPV)
 	if (XEN_VERSION_SUPPORTED(3, 4)) {
@@ -3883,7 +3882,7 @@ pmap_copy_page(paddr_t srcpa, paddr_t ds
 	pmap_update_pg(srcva);
 	pmap_update_pg(dstva);
 
-	x86_movs((void *)dstva, (void *)srcva, PAGE_SIZE / sizeof(long));
+	memcpy((void *)dstva, (void *)srcva, PAGE_SIZE);
 
 #if defined(DIAGNOSTIC) || defined(XENPV)
 	pmap_pte_set(srcpte, 

CVS commit: src/sys/arch/x86/x86

2020-05-29 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Fri May 29 22:40:16 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Reported-by: syzbot+fd9be59aa613bbf4e...@syzkaller.appspotmail.com
Reported-by: syzbot+15dd4dbac6ed159fa...@syzkaller.appspotmail.com
Reported-by: syzbot+38fa02d3b0e46e57c...@syzkaller.appspotmail.com

pmap_remove_all(): need to drain PV pages only after the PTEs are unmapped,
otherwise there can be a context switch with them mapped in.  XXX amd64
should use the direct map.


To generate a diff of this commit:
cvs rdiff -u -r1.396 -r1.397 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.396 src/sys/arch/x86/x86/pmap.c:1.397
--- src/sys/arch/x86/x86/pmap.c:1.396	Wed May 27 19:33:40 2020
+++ src/sys/arch/x86/x86/pmap.c	Fri May 29 22:40:15 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.396 2020/05/27 19:33:40 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.397 2020/05/29 22:40:15 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.396 2020/05/27 19:33:40 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.397 2020/05/29 22:40:15 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3145,7 +3145,6 @@ pmap_zap_ptp(struct pmap *pmap, struct v
 #ifdef DIAGNOSTIC
 	rb_tree_init(tree, _rbtree_ops);
 #endif
-	pmap_drain_pv(pmap);
 #else	/* !XENPV */
 	/*
 	 * XXXAD For XEN, it's not clear to me that we can do this, because
@@ -3216,6 +3215,7 @@ pmap_remove_all(struct pmap *pmap)
 			pmap_free_ptp(pmap, ptps[i], va, ptes, pdes);
 		}
 		pmap_unmap_ptes(pmap, pmap2);
+		pmap_drain_pv(pmap);
 		pmap_tlb_shootdown(pmap, -1L, 0, TLBSHOOT_REMOVE_ALL);
 		mutex_exit(>pm_lock);
 



CVS commit: src/sys/arch/x86/x86

2020-05-27 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed May 27 19:40:30 UTC 2020

Modified Files:
src/sys/arch/x86/x86: svs.c

Log Message:
svs_pdir_switch(): Use MOVS to copy the PTES.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/sys/arch/x86/x86/svs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/svs.c
diff -u src/sys/arch/x86/x86/svs.c:1.36 src/sys/arch/x86/x86/svs.c:1.37
--- src/sys/arch/x86/x86/svs.c:1.36	Wed May 27 19:15:08 2020
+++ src/sys/arch/x86/x86/svs.c	Wed May 27 19:40:29 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: svs.c,v 1.36 2020/05/27 19:15:08 ad Exp $	*/
+/*	$NetBSD: svs.c,v 1.37 2020/05/27 19:40:29 ad Exp $	*/
 
 /*
  * Copyright (c) 2018-2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.36 2020/05/27 19:15:08 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.37 2020/05/27 19:40:29 ad Exp $");
 
 #include "opt_svs.h"
 #include "opt_user_ldt.h"
@@ -682,28 +682,16 @@ svs_lwp_switch(struct lwp *oldlwp, struc
 	}
 }
 
-static inline pt_entry_t
-svs_pte_atomic_read(struct pmap *pmap, size_t idx)
-{
-	/*
-	 * XXX: We don't have a basic atomic_fetch_64 function?
-	 */
-	return atomic_cas_64(>pm_pdir[idx], 666, 666);
-}
-
 /*
- * We may come here with the pmap unlocked. So read its PTEs atomically. If
- * a remote CPU is updating them at the same time, it's not a problem: the
- * remote CPU will call svs_pmap_sync afterwards, and our updirpa will be
- * synchronized properly.
+ * We may come here with the pmap unlocked.  If a remote CPU is updating
+ * them at the same time, it's not a problem: the remote CPU will call
+ * svs_pmap_sync afterwards, and our updirpa will be synchronized properly.
  */
 void
 svs_pdir_switch(struct pmap *pmap)
 {
 	struct cpu_info *ci = curcpu();
 	struct svs_utls *utls;
-	pt_entry_t pte;
-	size_t i;
 
 	KASSERT(kpreempt_disabled());
 	KASSERT(pmap != pmap_kernel());
@@ -712,14 +700,9 @@ svs_pdir_switch(struct pmap *pmap)
 	utls = (struct svs_utls *)ci->ci_svs_utls;
 	utls->kpdirpa = pmap_pdirpa(pmap, 0) | svs_pcid_kcr3;
 
+	/* Copy user slots. */
 	mutex_enter(>ci_svs_mtx);
-
-	/* User slots. */
-	for (i = 0; i < PDIR_SLOT_USERLIM; i++) {
-		pte = svs_pte_atomic_read(pmap, i);
-		ci->ci_svs_updir[i] = pte;
-	}
-
+	x86_movs(ci->ci_svs_updir, pmap->pm_pdir, PDIR_SLOT_USERLIM);
 	mutex_exit(>ci_svs_mtx);
 
 	if (svs_pcid) {



CVS commit: src/sys/arch/x86/x86

2020-05-27 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed May 27 19:26:43 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Reported-by: syzbot+c1770938bb3fa7c08...@syzkaller.appspotmail.com
Reported-by: syzbot+ae26209c7d7f06e0b...@syzkaller.appspotmail.com

Can't defer freeing PV entries for the kernel's pmap until pmap_update(),
as that means taking locks and potentially recursing, and pmap_update()
for the kernel is used in all sorts of sensitive places.


To generate a diff of this commit:
cvs rdiff -u -r1.394 -r1.395 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.394 src/sys/arch/x86/x86/pmap.c:1.395
--- src/sys/arch/x86/x86/pmap.c:1.394	Tue May 26 10:10:31 2020
+++ src/sys/arch/x86/x86/pmap.c	Wed May 27 19:26:43 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.394 2020/05/26 10:10:31 bouyer Exp $	*/
+/*	$NetBSD: pmap.c,v 1.395 2020/05/27 19:26:43 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.394 2020/05/26 10:10:31 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.395 2020/05/27 19:26:43 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -494,6 +494,7 @@ static int pmap_pvp_ctor(void *, void *,
 static void pmap_pvp_dtor(void *, void *);
 static struct pv_entry *pmap_alloc_pv(struct pmap *);
 static void pmap_free_pv(struct pmap *, struct pv_entry *);
+static void pmap_drain_pv(struct pmap *);
 
 static void pmap_alloc_level(struct pmap *, vaddr_t, long *);
 
@@ -2073,6 +2074,25 @@ pmap_free_pv(struct pmap *pmap, struct p
 }
 
 /*
+ * pmap_drain_pv: free full PV pages.
+ */
+static void
+pmap_drain_pv(struct pmap *pmap)
+{
+	struct pv_page *pvp;
+
+	KASSERT(mutex_owned(>pm_lock));
+
+	while ((pvp = LIST_FIRST(>pm_pvp_full)) != NULL) {
+		LIST_REMOVE(pvp, pvp_list);
+		KASSERT(pvp->pvp_pmap == pmap);
+		KASSERT(pvp->pvp_nfree == PVE_PER_PVP);
+		pvp->pvp_pmap = NULL;
+		pool_cache_put(_pvp_cache, pvp);
+	}
+}
+
+/*
  * pmap_check_pv: verify {VA, PTP} pair is either tracked/untracked by page
  */
 static void
@@ -2940,12 +2960,13 @@ pmap_destroy(struct pmap *pmap)
 	 * handle any deferred frees.
 	 */
 
+	mutex_enter(>pm_lock);
 	if (pmap->pm_pve != NULL) {
-		mutex_enter(>pm_lock);
 		pmap_free_pv(pmap, pmap->pm_pve);
-		mutex_exit(>pm_lock);
 		pmap->pm_pve = NULL;
 	}
+	pmap_drain_pv(pmap);
+	mutex_exit(>pm_lock);
 	pmap_update(pmap);
 
 	/*
@@ -3088,7 +3109,7 @@ pmap_zap_ptp(struct pmap *pmap, struct v
 			mutex_spin_exit(>pp_lock);
 
 			/*
-			 * pve won't be touched again until pmap_update(),
+			 * pve won't be touched again until pmap_drain_pv(),
 			 * so it's still safe to traverse the tree.
 			 */
 			pmap_free_pv(pmap, pve);
@@ -3123,6 +3144,7 @@ pmap_zap_ptp(struct pmap *pmap, struct v
 #ifdef DIAGNOSTIC
 	rb_tree_init(tree, _rbtree_ops);
 #endif
+	pmap_drain_pv(pmap);
 #else	/* !XENPV */
 	/*
 	 * XXXAD For XEN, it's not clear to me that we can do this, because
@@ -4187,6 +4209,7 @@ pmap_remove_locked(struct pmap *pmap, va
 		}
 	}
 	pmap_unmap_ptes(pmap, pmap2);
+	pmap_drain_pv(pmap);
 }
 
 /*
@@ -4432,6 +4455,7 @@ pmap_pp_remove(struct pmap_page *pp, pad
 			pmap_stats_update_bypte(pmap, 0, opte);
 		}
 		pmap_tlb_shootnow();
+		pmap_drain_pv(pmap);
 		mutex_exit(>pm_lock);
 		if (ptp != NULL) {
 			pmap_destroy(pmap);
@@ -5076,6 +5100,7 @@ same_pa:
 	((opte ^ npte) & (PTE_FRAME | PTE_W)) != 0) {
 		pmap_tlb_shootdown(pmap, va, opte, TLBSHOOT_ENTER);
 	}
+	pmap_drain_pv(pmap);
 	mutex_exit(>pm_lock);
 	return 0;
 }
@@ -5322,6 +5347,7 @@ pmap_enter_gnt(struct pmap *pmap, vaddr_
 		KASSERT(pmap_treelookup_pv(pmap, ptp, tree, va) == NULL);
 	}
 
+	pmap_drain_pv(pmap);
 	mutex_exit(>pm_lock);
 	return op->status;
 }
@@ -5716,10 +5742,8 @@ pmap_dump(struct pmap *pmap, vaddr_t sva
 void
 pmap_update(struct pmap *pmap)
 {
-	struct pv_page *pvp;
 	struct pmap_page *pp;
 	struct vm_page *ptp;
-	uintptr_t sum;
 
 	/*
 	 * Initiate any pending TLB shootdowns.  Wait for them to
@@ -5733,18 +5757,12 @@ pmap_update(struct pmap *pmap)
 	 * Now that shootdowns are complete, process deferred frees.  This
 	 * is an unlocked check, but is safe as we're only interested in
 	 * work done in this LWP - we won't get a false negative.
-	 *
-	 * If pmap_kernel(), this can be called from interrupt context or
-	 * while holding a spinlock so we can't wait on the pmap lock.  No
-	 * big deal as we'll catch up eventually (even for user pmaps, in
-	 * pmap_destroy() when there's never contention on the lock).
-	 */
-	sum = (uintptr_t)atomic_load_relaxed(>pm_gc_ptp.lh_first);
-	sum |= (uintptr_t)atomic_load_relaxed(>pm_pvp_full.lh_first);
-	if (__predict_true(sum == 0 || cpu_intr_p() ||
-	!mutex_tryenter(>pm_lock))) {
+	 */
+	if 

CVS commit: src/sys/arch/x86/x86

2020-05-27 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed May 27 19:15:08 UTC 2020

Modified Files:
src/sys/arch/x86/x86: svs.c

Log Message:
svs_pmap_sync(): Fast-path the curcpu case.  Could be improved further
with a kcpuset iterator thing.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/arch/x86/x86/svs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/svs.c
diff -u src/sys/arch/x86/x86/svs.c:1.35 src/sys/arch/x86/x86/svs.c:1.36
--- src/sys/arch/x86/x86/svs.c:1.35	Sat May  2 11:37:17 2020
+++ src/sys/arch/x86/x86/svs.c	Wed May 27 19:15:08 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: svs.c,v 1.35 2020/05/02 11:37:17 maxv Exp $	*/
+/*	$NetBSD: svs.c,v 1.36 2020/05/27 19:15:08 ad Exp $	*/
 
 /*
- * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
+ * Copyright (c) 2018-2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.35 2020/05/02 11:37:17 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.36 2020/05/27 19:15:08 ad Exp $");
 
 #include "opt_svs.h"
 #include "opt_user_ldt.h"
@@ -575,6 +575,18 @@ svs_pmap_sync(struct pmap *pmap, int ind
 	KASSERT(kpreempt_disabled());
 	KASSERT(index < PDIR_SLOT_USERLIM);
 
+	ci = curcpu();
+	cid = cpu_index(ci);
+
+	mutex_enter(>ci_svs_mtx);
+	KASSERT(kcpuset_isset(pmap->pm_kernel_cpus, cid));
+	ci->ci_svs_updir[index] = pmap->pm_pdir[index];
+	mutex_exit(>ci_svs_mtx);
+
+	if (!kcpuset_isotherset(pmap->pm_kernel_cpus, cid)) {
+		return;
+	}
+
 	for (CPU_INFO_FOREACH(cii, ci)) {
 		cid = cpu_index(ci);
 



CVS commit: src/sys/arch/x86/x86

2020-05-27 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed May 27 18:46:15 UTC 2020

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
tsc_delay(): use tsc_freq in preference to cpu_frequency().


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.47 src/sys/arch/x86/x86/tsc.c:1.48
--- src/sys/arch/x86/x86/tsc.c:1.47	Wed May 20 20:19:02 2020
+++ src/sys/arch/x86/x86/tsc.c	Wed May 27 18:46:15 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.47 2020/05/20 20:19:02 ad Exp $	*/
+/*	$NetBSD: tsc.c,v 1.48 2020/05/27 18:46:15 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.47 2020/05/20 20:19:02 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.48 2020/05/27 18:46:15 ad Exp $");
 
 #include 
 #include 
@@ -55,7 +55,7 @@ u_int	tsc_get_timecount(struct timecount
 static void	tsc_delay(unsigned int);
 
 static uint64_t	tsc_dummy_cacheline __cacheline_aligned;
-uint64_t	tsc_freq; /* exported for sysctl */
+uint64_t	tsc_freq __read_mostly;	/* exported for sysctl */
 static int64_t	tsc_drift_max = 1000;	/* max cycles */
 static int64_t	tsc_drift_observed;
 
@@ -346,7 +346,7 @@ tsc_delay(unsigned int us)
 	uint64_t start, delta;
 
 	start = cpu_counter();
-	delta = (uint64_t)us * cpu_frequency(_info_primary) / 100;
+	delta = (uint64_t)us * tsc_freq / 100;
 
 	while ((cpu_counter() - start) < delta) {
 		x86_pause();



CVS commit: src/sys/arch/x86/x86

2020-05-21 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Thu May 21 22:01:42 UTC 2020

Modified Files:
src/sys/arch/x86/x86: lapic.c

Log Message:
Fix merge error


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/arch/x86/x86/lapic.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/lapic.c
diff -u src/sys/arch/x86/x86/lapic.c:1.81 src/sys/arch/x86/x86/lapic.c:1.82
--- src/sys/arch/x86/x86/lapic.c:1.81	Thu May 21 21:12:30 2020
+++ src/sys/arch/x86/x86/lapic.c	Thu May 21 22:01:42 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: lapic.c,v 1.81 2020/05/21 21:12:30 ad Exp $	*/
+/*	$NetBSD: lapic.c,v 1.82 2020/05/21 22:01:42 ad Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2008, 2020 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.81 2020/05/21 21:12:30 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.82 2020/05/21 22:01:42 ad Exp $");
 
 #include "acpica.h"
 #include "ioapic.h"
@@ -781,7 +781,7 @@ lapic_delay(unsigned int usec)
 	while (deltat > 0) {
 		xtick = lapic_gettick();
 		if (lapic_broken_periodic && xtick == 0 && otick == 0) {
-			lapic_initclocks();
+			lapic_reset();
 			xtick = lapic_gettick();
 			if (xtick == 0)
 panic("lapic timer stopped ticking");



CVS commit: src/sys/arch/x86/x86

2020-05-20 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed May 20 20:19:02 UTC 2020

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
The boot CPU suffers a cache miss during TSC sync, before RDTSC.  Make the
secondary CPU take a miss as well to try and delay it an equal amount.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.46 src/sys/arch/x86/x86/tsc.c:1.47
--- src/sys/arch/x86/x86/tsc.c:1.46	Tue May 19 21:56:51 2020
+++ src/sys/arch/x86/x86/tsc.c	Wed May 20 20:19:02 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.46 2020/05/19 21:56:51 ad Exp $	*/
+/*	$NetBSD: tsc.c,v 1.47 2020/05/20 20:19:02 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.46 2020/05/19 21:56:51 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.47 2020/05/20 20:19:02 ad Exp $");
 
 #include 
 #include 
@@ -54,6 +54,7 @@ u_int	tsc_get_timecount(struct timecount
 
 static void	tsc_delay(unsigned int);
 
+static uint64_t	tsc_dummy_cacheline __cacheline_aligned;
 uint64_t	tsc_freq; /* exported for sysctl */
 static int64_t	tsc_drift_max = 1000;	/* max cycles */
 static int64_t	tsc_drift_observed;
@@ -200,7 +201,7 @@ tsc_sync_drift(int64_t drift)
  * Called during startup of APs, by the boot processor.  Interrupts
  * are disabled on entry.
  */
-static void
+static void __noinline
 tsc_read_bp(struct cpu_info *ci, uint64_t *bptscp, uint64_t *aptscp)
 {
 	uint64_t bptsc;
@@ -209,10 +210,13 @@ tsc_read_bp(struct cpu_info *ci, uint64_
 		panic("tsc_sync_bp: 1");
 	}
 
-	/* Flag it and read our TSC. */
+	/* Prepare a cache miss for the other side. */
+	(void)atomic_swap_uint((void *)_dummy_cacheline, 0);
+
+	/* Flag our readiness. */
 	atomic_or_uint(>ci_flags, CPUF_SYNCTSC);
 
-	/* Wait for remote to complete, and read ours again. */
+	/* Wait for other side then read our TSC. */
 	while ((ci->ci_flags & CPUF_SYNCTSC) != 0) {
 		__insn_barrier();
 	}
@@ -254,7 +258,7 @@ tsc_sync_bp(struct cpu_info *ci)
  * Called during startup of AP, by the AP itself.  Interrupts are
  * disabled on entry.
  */
-static void
+static void __noinline
 tsc_post_ap(struct cpu_info *ci)
 {
 	uint64_t tsc;
@@ -266,7 +270,12 @@ tsc_post_ap(struct cpu_info *ci)
 
 	/* Instruct primary to read its counter. */
 	atomic_and_uint(>ci_flags, ~CPUF_SYNCTSC);
-	tsc = rdtsc();
+
+	/* Suffer a cache miss, then read TSC. */
+	__insn_barrier();
+	tsc = tsc_dummy_cacheline;
+	__insn_barrier();
+	tsc += rdtsc();
 
 	/* Post result.  Ensure the whole value goes out atomically. */
 	(void)atomic_swap_64(_sync_val, tsc);



CVS commit: src/sys/arch/x86/x86

2020-05-19 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Wed May 20 02:23:35 UTC 2020

Modified Files:
src/sys/arch/x86/x86: lapic.c

Log Message:
 Temporary back to lapic_initclocks() from lapci_reset() to avoid compile
error.


To generate a diff of this commit:
cvs rdiff -u -r1.79 -r1.80 src/sys/arch/x86/x86/lapic.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/lapic.c
diff -u src/sys/arch/x86/x86/lapic.c:1.79 src/sys/arch/x86/x86/lapic.c:1.80
--- src/sys/arch/x86/x86/lapic.c:1.79	Tue May 19 21:39:11 2020
+++ src/sys/arch/x86/x86/lapic.c	Wed May 20 02:23:35 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: lapic.c,v 1.79 2020/05/19 21:39:11 ad Exp $	*/
+/*	$NetBSD: lapic.c,v 1.80 2020/05/20 02:23:35 msaitoh Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2008, 2020 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.79 2020/05/19 21:39:11 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.80 2020/05/20 02:23:35 msaitoh Exp $");
 
 #include "acpica.h"
 #include "ioapic.h"
@@ -746,7 +746,7 @@ lapic_delay(unsigned int usec)
 	while (deltat > 0) {
 		xtick = lapic_gettick();
 		if (lapic_broken_periodic && xtick == 0 && otick == 0) {
-			lapic_reset();
+			lapic_initclocks();
 			xtick = lapic_gettick();
 			if (xtick == 0)
 panic("lapic timer stopped ticking");



CVS commit: src/sys/arch/x86/x86

2020-05-19 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue May 19 21:56:52 UTC 2020

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
Ignore x86_delay, for xen


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.45 src/sys/arch/x86/x86/tsc.c:1.46
--- src/sys/arch/x86/x86/tsc.c:1.45	Tue May 19 21:43:36 2020
+++ src/sys/arch/x86/x86/tsc.c	Tue May 19 21:56:51 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.45 2020/05/19 21:43:36 ad Exp $	*/
+/*	$NetBSD: tsc.c,v 1.46 2020/05/19 21:56:51 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.45 2020/05/19 21:43:36 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.46 2020/05/19 21:56:51 ad Exp $");
 
 #include 
 #include 
@@ -175,7 +175,6 @@ tsc_tc_init(void)
 		invariant = false;
 	} else if (vm_guest == VM_GUEST_NO) {
 		delay_func = tsc_delay;
-		x86_delay = tsc_delay;
 	}
 
 	if (tsc_freq != 0) {



CVS commit: src/sys/arch/x86/x86

2020-05-19 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue May 19 21:43:36 UTC 2020

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
If the the TSC timecounter is good then use the TSC for DELAY() too.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.44 src/sys/arch/x86/x86/tsc.c:1.45
--- src/sys/arch/x86/x86/tsc.c:1.44	Fri May  8 22:01:55 2020
+++ src/sys/arch/x86/x86/tsc.c	Tue May 19 21:43:36 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.44 2020/05/08 22:01:55 ad Exp $	*/
+/*	$NetBSD: tsc.c,v 1.45 2020/05/19 21:43:36 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.44 2020/05/08 22:01:55 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.45 2020/05/19 21:43:36 ad Exp $");
 
 #include 
 #include 
@@ -52,6 +52,8 @@ __KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.44
 
 u_int	tsc_get_timecount(struct timecounter *);
 
+static void	tsc_delay(unsigned int);
+
 uint64_t	tsc_freq; /* exported for sysctl */
 static int64_t	tsc_drift_max = 1000;	/* max cycles */
 static int64_t	tsc_drift_observed;
@@ -145,9 +147,11 @@ tsc_is_invariant(void)
 }
 
 /*
- * Initialize timecounter(9) of TSC.
- * This function is called after all secondary processors were up and
- * calculated the drift.
+ * Initialize timecounter(9) and DELAY() function of TSC.
+ *
+ * This function is called after all secondary processors were brought up
+ * and drift has been measured, and after any other potential delay funcs
+ * have been installed (e.g. lapic_delay()).
  */
 void
 tsc_tc_init(void)
@@ -169,6 +173,9 @@ tsc_tc_init(void)
 		(long long)tsc_drift_observed);
 		tsc_timecounter.tc_quality = -100;
 		invariant = false;
+	} else if (vm_guest == VM_GUEST_NO) {
+		delay_func = tsc_delay;
+		x86_delay = tsc_delay;
 	}
 
 	if (tsc_freq != 0) {
@@ -324,3 +331,16 @@ cpu_hascounter(void)
 
 	return cpu_feature[0] & CPUID_TSC;
 }
+
+static void
+tsc_delay(unsigned int us)
+{
+	uint64_t start, delta;
+
+	start = cpu_counter();
+	delta = (uint64_t)us * cpu_frequency(_info_primary) / 100;
+
+	while ((cpu_counter() - start) < delta) {
+		x86_pause();
+	}
+}



CVS commit: src/sys/arch/x86/x86

2020-05-19 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue May 19 21:39:11 UTC 2020

Modified Files:
src/sys/arch/x86/x86: lapic.c

Log Message:
lapic_delay() disable preemption since the state is very CPU dependent.


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.79 src/sys/arch/x86/x86/lapic.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/lapic.c
diff -u src/sys/arch/x86/x86/lapic.c:1.78 src/sys/arch/x86/x86/lapic.c:1.79
--- src/sys/arch/x86/x86/lapic.c:1.78	Sat May  2 16:44:36 2020
+++ src/sys/arch/x86/x86/lapic.c	Tue May 19 21:39:11 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: lapic.c,v 1.78 2020/05/02 16:44:36 bouyer Exp $	*/
+/*	$NetBSD: lapic.c,v 1.79 2020/05/19 21:39:11 ad Exp $	*/
 
 /*-
- * Copyright (c) 2000, 2008 The NetBSD Foundation, Inc.
+ * Copyright (c) 2000, 2008, 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.78 2020/05/02 16:44:36 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.79 2020/05/19 21:39:11 ad Exp $");
 
 #include "acpica.h"
 #include "ioapic.h"
@@ -727,12 +727,17 @@ static void
 lapic_delay(unsigned int usec)
 {
 	int32_t xtick, otick;
-	int64_t deltat;		/* XXX may want to be 64bit */
+	int64_t deltat;
 
+	/* XXX Bad to disable preemption, but it's tied to the cpu. */
+	kpreempt_disable();
 	otick = lapic_gettick();
 
-	if (usec <= 0)
+	if (usec <= 0) {
+		kpreempt_enable();
 		return;
+	}
+
 	if (usec <= 25)
 		deltat = lapic_delaytab[usec];
 	else
@@ -741,7 +746,7 @@ lapic_delay(unsigned int usec)
 	while (deltat > 0) {
 		xtick = lapic_gettick();
 		if (lapic_broken_periodic && xtick == 0 && otick == 0) {
-			lapic_initclocks();
+			lapic_reset();
 			xtick = lapic_gettick();
 			if (xtick == 0)
 panic("lapic timer stopped ticking");
@@ -754,6 +759,7 @@ lapic_delay(unsigned int usec)
 
 		x86_pause();
 	}
+	kpreempt_enable();
 }
 
 /*



CVS commit: src/sys/arch/x86/x86

2020-05-19 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue May 19 21:14:20 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Comment


To generate a diff of this commit:
cvs rdiff -u -r1.392 -r1.393 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.392 src/sys/arch/x86/x86/pmap.c:1.393
--- src/sys/arch/x86/x86/pmap.c:1.392	Fri May 15 22:22:44 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue May 19 21:14:20 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.392 2020/05/15 22:22:44 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.393 2020/05/19 21:14:20 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.392 2020/05/15 22:22:44 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.393 2020/05/19 21:14:20 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3025,8 +3025,7 @@ pmap_zap_ptp(struct pmap *pmap, struct v
 
 	/*
 	 * Start at the lowest entered VA, and scan until there are no more
-	 * PTEs in the PTPs.  The goal is to disconnect PV entries and patch
-	 * up the pmap's stats.  No PTEs will be modified.
+	 * PTEs in the PTPs.
 	 */
 	tree = _PAGE_TO_PP(ptp)->pp_rb;
 	pve = RB_TREE_MIN(tree);



CVS commit: src/sys/arch/x86/x86

2020-05-17 Thread NONAKA Kimihiro
Module Name:src
Committed By:   nonaka
Date:   Sun May 17 11:54:40 UTC 2020

Modified Files:
src/sys/arch/x86/x86: hyperv.c

Log Message:
Fixed a problem that caused a page fault when attaching vmbus(4).

Dynamically allocate a page of memory with uvm_km_alloc(kernel_map, ...)
for Hyper-V hypercall.  However, this method can no longer be used to
make an executable page.
So we prevent it by using statically allocated memory for text segment.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/x86/x86/hyperv.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/hyperv.c
diff -u src/sys/arch/x86/x86/hyperv.c:1.8 src/sys/arch/x86/x86/hyperv.c:1.9
--- src/sys/arch/x86/x86/hyperv.c:1.8	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/hyperv.c	Sun May 17 11:54:39 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: hyperv.c,v 1.8 2020/04/25 15:26:18 bouyer Exp $	*/
+/*	$NetBSD: hyperv.c,v 1.9 2020/05/17 11:54:39 nonaka Exp $	*/
 
 /*-
  * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
@@ -33,7 +33,7 @@
  */
 #include 
 #ifdef __KERNEL_RCSID
-__KERNEL_RCSID(0, "$NetBSD: hyperv.c,v 1.8 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: hyperv.c,v 1.9 2020/05/17 11:54:39 nonaka Exp $");
 #endif
 #ifdef __FBSDID
 __FBSDID("$FreeBSD: head/sys/dev/hyperv/vmbus/hyperv.c 331757 2018-03-30 02:25:12Z emaste $");
@@ -97,6 +97,9 @@ struct hyperv_hypercall_ctx {
 
 static struct hyperv_hypercall_ctx hyperv_hypercall_ctx;
 
+static char hyperv_hypercall_page[PAGE_SIZE]
+__section(".text") __aligned(PAGE_SIZE) = { 0xcc };
+
 static u_int	hyperv_get_timecount(struct timecounter *);
 
 static u_int hyperv_ver_major;
@@ -703,11 +706,7 @@ static void
 hyperv_hypercall_memfree(void)
 {
 
-	if (hyperv_hypercall_ctx.hc_addr != NULL) {
-		uvm_km_free(kernel_map, (vaddr_t)hyperv_hypercall_ctx.hc_addr,
-		PAGE_SIZE, UVM_KMF_WIRED);
-		hyperv_hypercall_ctx.hc_addr = NULL;
-	}
+	hyperv_hypercall_ctx.hc_addr = NULL;
 }
 
 static bool
@@ -715,30 +714,9 @@ hyperv_init_hypercall(void)
 {
 	uint64_t hc, hc_orig;
 
-	hyperv_hypercall_ctx.hc_addr = (void *)uvm_km_alloc(kernel_map,
-	PAGE_SIZE, PAGE_SIZE,
-	UVM_KMF_WIRED | UVM_KMF_EXEC | (cold ? UVM_KMF_NOWAIT : 0));
-	if (hyperv_hypercall_ctx.hc_addr == NULL) {
-		aprint_error("Hyper-V: Hypercall page allocation failed\n");
-		return false;
-	}
-
-	memset(hyperv_hypercall_ctx.hc_addr, 0xcc, PAGE_SIZE);
-	wbinvd();
-	x86_flush();
-
-	/* The hypercall page must be both readable and executable */
-	uvm_km_protect(kernel_map, (vaddr_t)hyperv_hypercall_ctx.hc_addr,
-	PAGE_SIZE, VM_PROT_READ | VM_PROT_EXECUTE);
-
-	if (!pmap_extract(pmap_kernel(), (vaddr_t)hyperv_hypercall_ctx.hc_addr,
-	_hypercall_ctx.hc_paddr)) {
-		aprint_error("Hyper-V: Hypercall page setup failed\n");
-		hyperv_hypercall_memfree();
-		/* Can't perform any Hyper-V specific actions */
-		vm_guest = VM_GUEST_VM;
-		return false;
-	}
+	hyperv_hypercall_ctx.hc_addr = hyperv_hypercall_page;
+	hyperv_hypercall_ctx.hc_paddr = vtophys((vaddr_t)hyperv_hypercall_page);
+	KASSERT(hyperv_hypercall_ctx.hc_paddr != 0);
 
 	/* Get the 'reserved' bits, which requires preservation. */
 	hc_orig = rdmsr(MSR_HV_HYPERCALL);



CVS commit: src/sys/arch/x86/x86

2020-05-15 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Fri May 15 22:22:44 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
PTP pages are zeroed before free again.


To generate a diff of this commit:
cvs rdiff -u -r1.391 -r1.392 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.391 src/sys/arch/x86/x86/pmap.c:1.392
--- src/sys/arch/x86/x86/pmap.c:1.391	Fri May 15 22:22:06 2020
+++ src/sys/arch/x86/x86/pmap.c	Fri May 15 22:22:44 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.391 2020/05/15 22:22:06 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.392 2020/05/15 22:22:44 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.391 2020/05/15 22:22:06 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.392 2020/05/15 22:22:44 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -5433,10 +5433,7 @@ pmap_update(struct pmap *pmap)
 		uvm_pagerealloc(ptp, NULL, 0);
 		PMAP_DUMMY_UNLOCK(pmap);
 
-		/*
-		 * XXX for PTPs freed by pmap_remove_ptes() but not
-		 * pmap_zap_ptp(), we could mark them PG_ZERO.
-		 */
+		ptp->flags |= PG_ZERO;
 		uvm_pagefree(ptp);
 	}
 	while ((pvp = LIST_FIRST(>pm_pvp_full)) != NULL) {



CVS commit: src/sys/arch/x86/x86

2020-05-15 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Fri May 15 22:22:06 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Reported-by: syzbot+0f38e4aed17c14cf0...@syzkaller.appspotmail.com
Reported-by: syzbot+c1770938bb3fa7c08...@syzkaller.appspotmail.com
Reported-by: syzbot+92ca248f1137c4b34...@syzkaller.appspotmail.com
Reported-by: syzbot+acfd688740461f7ed...@syzkaller.appspotmail.com

Be careful with pmap_lock in pmap_update().  It can happen that pmap_kernel
has work pending that gets noticed in interrupt context, before process
context has a chance to deal with it.


To generate a diff of this commit:
cvs rdiff -u -r1.390 -r1.391 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.390 src/sys/arch/x86/x86/pmap.c:1.391
--- src/sys/arch/x86/x86/pmap.c:1.390	Fri May 15 22:19:01 2020
+++ src/sys/arch/x86/x86/pmap.c	Fri May 15 22:22:06 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.390 2020/05/15 22:19:01 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.391 2020/05/15 22:22:06 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.390 2020/05/15 22:19:01 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.391 2020/05/15 22:22:06 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3035,10 +3035,17 @@ pmap_zap_ptp(struct pmap *pmap, struct v
 	pte += ((va - startva) >> PAGE_SHIFT);
 
 	for (cnt = ptp->wire_count; cnt > 1; pte++, va += PAGE_SIZE) {
+		/*
+		 * No need for an atomic to clear the PTE.  Nothing else can
+		 * see the address space any more and speculative access (if
+		 * possible) won't modify.  Therefore there's no need to
+		 * track the accessed/dirty bits.
+		 */
 		opte = *pte;
 		if (!pmap_valid_entry(opte)) {
 			continue;
 		}
+		pmap_pte_set(pte, 0);
 
 		/*
 		 * Count the PTE.  If it's not for a managed mapping
@@ -5380,6 +5387,7 @@ pmap_update(struct pmap *pmap)
 	struct pv_page *pvp;
 	struct pmap_page *pp;
 	struct vm_page *ptp;
+	uintptr_t sum;
 
 	/*
 	 * Initiate any pending TLB shootdowns.  Wait for them to
@@ -5393,45 +5401,52 @@ pmap_update(struct pmap *pmap)
 	 * Now that shootdowns are complete, process deferred frees.  This
 	 * is an unlocked check, but is safe as we're only interested in
 	 * work done in this LWP - we won't get a false negative.
-	 */
-	if (__predict_false(!LIST_EMPTY(>pm_gc_ptp) ||
-	!LIST_EMPTY(>pm_pvp_full))) {
-		mutex_enter(>pm_lock);
-		while ((ptp = LIST_FIRST(>pm_gc_ptp)) != NULL) {
-			KASSERT(ptp->wire_count == 0);
-			KASSERT(ptp->uanon == NULL);
-			LIST_REMOVE(ptp, mdpage.mp_pp.pp_link);
-			pp = VM_PAGE_TO_PP(ptp);
-			LIST_INIT(>pp_pvlist);
-			pp->pp_attrs = 0;
-			pp->pp_pte.pte_ptp = NULL;
-			pp->pp_pte.pte_va = 0;
-			PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
+	 *
+	 * If pmap_kernel(), this can be called from interrupt context or
+	 * while holding a spinlock so we can't wait on the pmap lock.  No
+	 * big deal as we'll catch up eventually (even for user pmaps, in
+	 * pmap_destroy() when there's never contention on the lock).
+	 */
+	sum = (uintptr_t)atomic_load_relaxed(>pm_gc_ptp.lh_first);
+	sum |= (uintptr_t)atomic_load_relaxed(>pm_pvp_full.lh_first);
+	if (__predict_true(sum == 0 || cpu_intr_p() ||
+	!mutex_tryenter(>pm_lock))) {
+		return;
+	}	
+	while ((ptp = LIST_FIRST(>pm_gc_ptp)) != NULL) {
+		KASSERT(ptp->wire_count == 0);
+		KASSERT(ptp->uanon == NULL);
+		LIST_REMOVE(ptp, mdpage.mp_pp.pp_link);
+		pp = VM_PAGE_TO_PP(ptp);
+		LIST_INIT(>pp_pvlist);
+		pp->pp_attrs = 0;
+		pp->pp_pte.pte_ptp = NULL;
+		pp->pp_pte.pte_va = 0;
+		PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
 
-			/*
-			 * XXX Hack to avoid extra locking, and lock
-			 * assertions in uvm_pagefree().  Despite uobject
-			 * being set, this isn't a managed page.
-			 */
-			PMAP_DUMMY_LOCK(pmap);
-			uvm_pagerealloc(ptp, NULL, 0);
-			PMAP_DUMMY_UNLOCK(pmap);
+		/*
+		 * XXX Hack to avoid extra locking, and lock
+		 * assertions in uvm_pagefree().  Despite uobject
+		 * being set, this isn't a managed page.
+		 */
+		PMAP_DUMMY_LOCK(pmap);
+		uvm_pagerealloc(ptp, NULL, 0);
+		PMAP_DUMMY_UNLOCK(pmap);
 
-			/*
-			 * XXX for PTPs freed by pmap_remove_ptes() but not
-			 * pmap_zap_ptp(), we could mark them PG_ZERO.
-			 */
-			uvm_pagefree(ptp);
-		}
-		while ((pvp = LIST_FIRST(>pm_pvp_full)) != NULL) {
-			LIST_REMOVE(pvp, pvp_list);
-			KASSERT(pvp->pvp_pmap == pmap);
-			KASSERT(pvp->pvp_nfree == PVE_PER_PVP);
-			pvp->pvp_pmap = NULL;
-			pool_cache_put(_pvp_cache, pvp);
-		}
-		mutex_exit(>pm_lock);
+		/*
+		 * XXX for PTPs freed by pmap_remove_ptes() but not
+		 * pmap_zap_ptp(), we could mark them PG_ZERO.
+		 */
+		uvm_pagefree(ptp);
 	}
+	while ((pvp = LIST_FIRST(>pm_pvp_full)) != NULL) {
+		LIST_REMOVE(pvp, pvp_list);
+		

CVS commit: src/sys/arch/x86/x86

2020-05-15 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Fri May 15 22:19:01 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
PR kern/55268: tmpfs is slow

pmap_clear_attrs(): if a brand new page with no mappings just zap pp_attrs.


To generate a diff of this commit:
cvs rdiff -u -r1.389 -r1.390 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.389 src/sys/arch/x86/x86/pmap.c:1.390
--- src/sys/arch/x86/x86/pmap.c:1.389	Fri May  8 00:49:43 2020
+++ src/sys/arch/x86/x86/pmap.c	Fri May 15 22:19:01 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.389 2020/05/08 00:49:43 riastradh Exp $	*/
+/*	$NetBSD: pmap.c,v 1.390 2020/05/15 22:19:01 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.389 2020/05/08 00:49:43 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.390 2020/05/15 22:19:01 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -4567,7 +4567,19 @@ pmap_clear_attrs(struct vm_page *pg, uns
 	pp = VM_PAGE_TO_PP(pg);
 	pa = VM_PAGE_TO_PHYS(pg);
 
-	return pmap_pp_clear_attrs(pp, pa, clearbits);
+	/*
+	 * If this is a new page, assert it has no mappings and simply zap
+	 * the stored attributes without taking any locks.
+	 */
+	if ((pg->flags & PG_FAKE) != 0) {
+		KASSERT(atomic_load_relaxed(>pp_pte.pte_va) == 0);
+		KASSERT(atomic_load_relaxed(>pp_pte.pte_ptp) == NULL);
+		KASSERT(atomic_load_relaxed(>pp_pvlist.lh_first) == NULL);
+		atomic_store_relaxed(>pp_attrs, 0);
+		return false;
+	} else {
+		return pmap_pp_clear_attrs(pp, pa, clearbits);
+	}
 }
 
 /*



CVS commit: src/sys/arch/x86/x86

2020-05-12 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Tue May 12 06:32:05 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu.c identcpu_subr.c

Log Message:
Don't use TSC freq value from CPUID if calibration works.

 - When it's the first call of cpu_get_tsc_freq() the HPET is not initialized,
   so try to use CPUID to get TSC freq.
 - If it's the 2nd call, don't use CPUID. Instead, print the difference
   between the calibrated value and CPUID's value if the verbose mode is set.


To generate a diff of this commit:
cvs rdiff -u -r1.190 -r1.191 src/sys/arch/x86/x86/cpu.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/x86/x86/identcpu_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.190 src/sys/arch/x86/x86/cpu.c:1.191
--- src/sys/arch/x86/x86/cpu.c:1.190	Fri May  8 22:01:55 2020
+++ src/sys/arch/x86/x86/cpu.c	Tue May 12 06:32:05 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.190 2020/05/08 22:01:55 ad Exp $	*/
+/*	$NetBSD: cpu.c,v 1.191 2020/05/12 06:32:05 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.190 2020/05/08 22:01:55 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.191 2020/05/12 06:32:05 msaitoh Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -1320,11 +1320,19 @@ cpu_shutdown(device_t dv, int how)
 void
 cpu_get_tsc_freq(struct cpu_info *ci)
 {
-	uint64_t freq = 0, t0, t1;
+	uint64_t freq = 0, freq_from_cpuid, t0, t1;
 	int64_t overhead;
 
 	if ((ci->ci_flags & CPUF_PRIMARY) != 0 && cpu_hascounter()) {
-		freq = cpu_tsc_freq_cpuid(ci);
+		/*
+		 * If it's the first call of this function, try to get TSC
+		 * freq from CPUID by calling cpu_tsc_freq_cpuid().
+		 * The function also set lapic_per_second variable if it's
+		 * known. This is required for Intel's Comet Lake and newer
+		 * processors to set LAPIC timer correctly.
+		 */
+		if (ci->ci_data.cpu_cc_freq == 0)
+			freq = freq_from_cpuid = cpu_tsc_freq_cpuid(ci);
 #if NHPET > 0
 		if (freq == 0)
 			freq = hpet_tsc_freq();
@@ -1352,6 +1360,13 @@ cpu_get_tsc_freq(struct cpu_info *ci)
 			t1 = cpu_counter();
 			freq = (t1 - t0 - overhead) * 10;
 		}
+		if (ci->ci_data.cpu_cc_freq != 0) {
+			freq_from_cpuid = cpu_tsc_freq_cpuid(ci);
+			if ((freq_from_cpuid != 0)
+			&& (freq != freq_from_cpuid))
+aprint_verbose_dev(ci->ci_dev, "TSC freq "
+"calibrated %" PRIu64 " Hz\n", freq);
+		}
 	} else {
 		freq = cpu_info_primary.ci_data.cpu_cc_freq;
 	}

Index: src/sys/arch/x86/x86/identcpu_subr.c
diff -u src/sys/arch/x86/x86/identcpu_subr.c:1.3 src/sys/arch/x86/x86/identcpu_subr.c:1.4
--- src/sys/arch/x86/x86/identcpu_subr.c:1.3	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/identcpu_subr.c	Tue May 12 06:32:05 2020
@@ -33,7 +33,7 @@
  * See src/usr.sbin/cpuctl/{Makefile, arch/i386.c}).
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.3 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.4 2020/05/12 06:32:05 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "lapic.h"
@@ -139,8 +139,8 @@ cpu_tsc_freq_cpuid(struct cpu_info *ci)
 #endif
 	}
 	if (freq != 0)
-		aprint_verbose_dev(ci->ci_dev, "TSC freq %" PRIu64 " Hz\n",
-		freq);
+		aprint_verbose_dev(ci->ci_dev, "TSC freq CPUID %" PRIu64
+		" Hz\n", freq);
 
 	return freq;
 }



CVS commit: src/sys/arch/x86/x86

2020-05-08 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Fri May  8 21:43:54 UTC 2020

Modified Files:
src/sys/arch/x86/x86: x86_softintr.c

Log Message:
KNF


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/x86/x86/x86_softintr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/x86_softintr.c
diff -u src/sys/arch/x86/x86/x86_softintr.c:1.2 src/sys/arch/x86/x86/x86_softintr.c:1.3
--- src/sys/arch/x86/x86/x86_softintr.c:1.2	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/x86_softintr.c	Fri May  8 21:43:54 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_softintr.c,v 1.2 2020/04/25 15:26:18 bouyer Exp $	*/
+/*	$NetBSD: x86_softintr.c,v 1.3 2020/05/08 21:43:54 ad Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_softintr.c,v 1.2 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_softintr.c,v 1.3 2020/05/08 21:43:54 ad Exp $");
 
 #include 
 #include 
@@ -223,6 +223,7 @@ void
 x86_init_preempt(struct cpu_info *ci)
 {
 	struct intrsource *isp;
+
 	isp = kmem_zalloc(sizeof(*isp), KM_SLEEP);
 	isp->is_recurse = Xrecurse_preempt;
 	isp->is_resume = Xresume_preempt;



CVS commit: src/sys/arch/x86/x86

2020-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May  7 18:13:05 UTC 2020

Modified Files:
src/sys/arch/x86/x86: patch.c

Log Message:
Fix LOCKDEBUG compilation on i386.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/sys/arch/x86/x86/patch.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/patch.c
diff -u src/sys/arch/x86/x86/patch.c:1.48 src/sys/arch/x86/x86/patch.c:1.49
--- src/sys/arch/x86/x86/patch.c:1.48	Sat May  2 16:25:47 2020
+++ src/sys/arch/x86/x86/patch.c	Thu May  7 18:13:05 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: patch.c,v 1.48 2020/05/02 16:25:47 maxv Exp $	*/
+/*	$NetBSD: patch.c,v 1.49 2020/05/07 18:13:05 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.48 2020/05/02 16:25:47 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.49 2020/05/07 18:13:05 maxv Exp $");
 
 #include "opt_lockdebug.h"
 #ifdef i386
@@ -171,6 +171,7 @@ static const struct x86_hotpatch_descrip
 __link_set_add_rodata(x86_hotpatch_descriptors, hp_cx8_spllower_desc);
 
 /* MUTEX_EXIT. */
+#ifndef LOCKDEBUG
 extern uint8_t i686_mutex_spin_exit, i686_mutex_spin_exit_end;
 static const struct x86_hotpatch_source hp_i686_mutex_spin_exit_source = {
 	.saddr = _mutex_spin_exit,
@@ -183,6 +184,7 @@ static const struct x86_hotpatch_descrip
 };
 __link_set_add_rodata(x86_hotpatch_descriptors, hp_i686_mutex_spin_exit_desc);
 #endif
+#endif
 
 /* -- */
 



CVS commit: src/sys/arch/x86/x86

2020-05-03 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Sun May  3 17:22:03 UTC 2020

Modified Files:
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
If hvm_start_info has no memmap_entries, fall back to XENMEM_memory_map
hypercall.


To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 src/sys/arch/x86/x86/x86_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.141 src/sys/arch/x86/x86/x86_machdep.c:1.142
--- src/sys/arch/x86/x86/x86_machdep.c:1.141	Sat May  2 16:44:36 2020
+++ src/sys/arch/x86/x86/x86_machdep.c	Sun May  3 17:22:03 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.141 2020/05/02 16:44:36 bouyer Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.142 2020/05/03 17:22:03 bouyer Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.141 2020/05/02 16:44:36 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.142 2020/05/03 17:22:03 bouyer Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -846,22 +846,11 @@ x86_load_region(uint64_t seg_start, uint
 	}
 }
 
-/*
- * init_x86_clusters: retrieve the memory clusters provided by the BIOS, and
- * initialize mem_clusters.
- */
-void
-init_x86_clusters(void)
-{
-	struct btinfo_memmap *bim;
-	struct btinfo_efimemmap *biem;
-
-	/*
-	 * Check to see if we have a memory map from the BIOS (passed to us by
-	 * the boot program).
-	 */
 #ifdef XEN
-	if (vm_guest == VM_GUEST_XENPVH) {
+static void
+x86_add_xen_clusters(void)
+{
+	if (hvm_start_info->memmap_entries > 0) {
 		struct hvm_memmap_table_entry *map_entry;
 		map_entry = (void *)((uintptr_t)hvm_start_info->memmap_paddr + KERNBASE);
 		for (int i = 0; i < hvm_start_info->memmap_entries; i++) {
@@ -878,6 +867,41 @@ init_x86_clusters(void)
 break;
 			}
 		}
+	} else {
+		struct xen_memory_map memmap;
+		static struct _xen_mmap {
+			struct btinfo_memmap bim;
+			struct bi_memmap_entry map[128]; /* same as FreeBSD */
+		} __packed xen_mmap;
+		int err;
+
+		memmap.nr_entries = 128;
+		set_xen_guest_handle(memmap.buffer, _mmap.bim.entry[0]);
+		if ((err = HYPERVISOR_memory_op(XENMEM_memory_map, ))
+		< 0)
+			panic("XENMEM_memory_map %d", err);
+		xen_mmap.bim.num = memmap.nr_entries;
+		x86_parse_clusters(_mmap.bim);
+	}
+}
+#endif /* XEN */
+/*
+ * init_x86_clusters: retrieve the memory clusters provided by the BIOS, and
+ * initialize mem_clusters.
+ */
+void
+init_x86_clusters(void)
+{
+	struct btinfo_memmap *bim;
+	struct btinfo_efimemmap *biem;
+
+	/*
+	 * Check to see if we have a memory map from the BIOS (passed to us by
+	 * the boot program).
+	 */
+#ifdef XEN
+	if (vm_guest == VM_GUEST_XENPVH) {
+		x86_add_xen_clusters();
 	}
 #endif /* XEN */
 



CVS commit: src/sys/arch/x86/x86

2020-05-02 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May  2 16:25:47 UTC 2020

Modified Files:
src/sys/arch/x86/x86: patch.c

Log Message:
Remove the D bit as part of the hotpatch cleanup procedure.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/arch/x86/x86/patch.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/patch.c
diff -u src/sys/arch/x86/x86/patch.c:1.47 src/sys/arch/x86/x86/patch.c:1.48
--- src/sys/arch/x86/x86/patch.c:1.47	Sat May  2 11:37:17 2020
+++ src/sys/arch/x86/x86/patch.c	Sat May  2 16:25:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: patch.c,v 1.47 2020/05/02 11:37:17 maxv Exp $	*/
+/*	$NetBSD: patch.c,v 1.48 2020/05/02 16:25:47 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.47 2020/05/02 11:37:17 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.48 2020/05/02 16:25:47 maxv Exp $");
 
 #include "opt_lockdebug.h"
 #ifdef i386
@@ -49,6 +49,9 @@ __KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.
 #include 
 #include 
 
+#include 
+#include 
+
 #include 
 #include 
 
@@ -256,6 +259,45 @@ x86_hotpatch_apply(uint8_t name, uint8_t
 	return 0;
 }
 
+#ifdef __x86_64__
+/*
+ * The CPU added the D bit on the text pages while we were writing to them.
+ * Remove that bit. Kinda annoying, but we can't avoid it.
+ */
+static void
+remove_d_bit(void)
+{
+	extern struct bootspace bootspace;
+	pt_entry_t pte;
+	vaddr_t va;
+	size_t i, n;
+
+	for (i = 0; i < BTSPACE_NSEGS; i++) {
+		if (bootspace.segs[i].type != BTSEG_TEXT)
+			continue;
+		va = bootspace.segs[i].va;
+		n = 0;
+		while (n < bootspace.segs[i].sz) {
+			if (L2_BASE[pl2_i(va)] & PTE_PS) {
+pte = L2_BASE[pl2_i(va)] & ~PTE_D;
+pmap_pte_set(_BASE[pl2_i(va)], pte);
+n += NBPD_L2;
+va += NBPD_L2;
+			} else {
+pte = L1_BASE[pl1_i(va)] & ~PTE_D;
+pmap_pte_set(_BASE[pl1_i(va)], pte);
+n += NBPD_L1;
+va += NBPD_L1;
+			}
+		}
+	}
+
+	tlbflushg();
+}
+#else
+#define remove_d_bit()	__nothing
+#endif
+
 /*
  * Interrupts disabled here. Called from ASM only, prototype not public.
  */
@@ -266,6 +308,8 @@ x86_hotpatch_cleanup(int retval)
 	if (retval != 0) {
 		panic("x86_hotpatch_apply failed");
 	}
+
+	remove_d_bit();
 }
 
 /* -- */



CVS commit: src/sys/arch/x86/x86

2020-05-01 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Fri May  1 14:16:15 UTC 2020

Modified Files:
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
Use PRIxPADDR for paddr_t to make i386/ALL compile.


To generate a diff of this commit:
cvs rdiff -u -r1.139 -r1.140 src/sys/arch/x86/x86/x86_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.139 src/sys/arch/x86/x86/x86_machdep.c:1.140
--- src/sys/arch/x86/x86/x86_machdep.c:1.139	Thu Apr 30 22:09:32 2020
+++ src/sys/arch/x86/x86/x86_machdep.c	Fri May  1 14:16:15 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.139 2020/04/30 22:09:32 bouyer Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.140 2020/05/01 14:16:15 hannken Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.139 2020/04/30 22:09:32 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.140 2020/05/01 14:16:15 hannken Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -959,7 +959,8 @@ init_x86_vm(paddr_t pa_kend)
 		if (seg_start <= pa_kstart && pa_kend <= seg_end) {
 #ifdef DEBUG_MEMLOAD
 			printf("split kernel overlapping to "
-			"%" PRIx64 " - %lx and %lx - %" PRIx64 "\n",
+			"%" PRIx64 " - %" PRIxPADDR " and "
+			"%" PRIxPADDR " - %" PRIx64 "\n",
 			seg_start, pa_kstart, pa_kend, seg_end);
 #endif
 			seg_start1 = pa_kend;
@@ -991,7 +992,8 @@ init_x86_vm(paddr_t pa_kend)
 		pa_kend < seg_end) {
 #ifdef DEBUG_MEMLOAD
 			printf("discard leading kernel overlap "
-			"%" PRIx64 " - %lx\n", seg_start, pa_kend);
+			"%" PRIx64 " - %" PRIxPADDR "\n",
+			seg_start, pa_kend);
 #endif
 			seg_start = pa_kend;
 		}
@@ -1006,7 +1008,8 @@ init_x86_vm(paddr_t pa_kend)
 		seg_end < pa_kend) {
 #ifdef DEBUG_MEMLOAD
 			printf("discard trailing kernel overlap "
-			"%lx - %" PRIx64 "\n", pa_kstart, seg_end);
+			"%" PRIxPADDR " - %" PRIx64 "\n",
+			pa_kstart, seg_end);
 #endif
 			seg_end = pa_kstart;
 		}



CVS commit: src/sys/arch/x86/x86

2020-04-30 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Thu Apr 30 22:09:32 UTC 2020

Modified Files:
src/sys/arch/x86/x86: x86_machdep.c

Log Message:
Change module path to xen-* only for XENPV


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 src/sys/arch/x86/x86/x86_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.138 src/sys/arch/x86/x86/x86_machdep.c:1.139
--- src/sys/arch/x86/x86/x86_machdep.c:1.138	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/x86_machdep.c	Thu Apr 30 22:09:32 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.138 2020/04/25 15:26:18 bouyer Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.139 2020/04/30 22:09:32 bouyer Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.138 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.139 2020/04/30 22:09:32 bouyer Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -95,7 +95,7 @@ void (*x86_cpu_idle)(void);
 static bool x86_cpu_idle_ipi;
 static char x86_cpu_idle_text[16];
 
-#ifdef XEN
+#ifdef XENPV
 char module_machine_amd64_xen[] = "amd64-xen";
 char module_machine_i386pae_xen[] = "i386pae-xen";
 #endif
@@ -222,7 +222,7 @@ module_init_md(void)
 	struct bi_modulelist_entry *bi, *bimax;
 
 	/* setup module path for XEN kernels */
-#ifdef XEN
+#ifdef XENPV
 #ifdef __x86_64__
 	module_machine = module_machine_amd64_xen;
 #else



CVS commit: src/sys/arch/x86/x86

2020-04-29 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed Apr 29 22:03:10 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu.c

Log Message:
Back out HPET delay & TSC changes to rule them out as the cause for recent
hangs during boot etc.


To generate a diff of this commit:
cvs rdiff -u -r1.187 -r1.188 src/sys/arch/x86/x86/cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.187 src/sys/arch/x86/x86/cpu.c:1.188
--- src/sys/arch/x86/x86/cpu.c:1.187	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/cpu.c	Wed Apr 29 22:03:09 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: cpu.c,v 1.187 2020/04/25 15:26:18 bouyer Exp $	*/
+/*	$NetBSD: cpu.c,v 1.188 2020/04/29 22:03:09 ad Exp $	*/
 
 /*
- * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
+ * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.187 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.188 2020/04/29 22:03:09 ad Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -73,7 +73,6 @@ __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.18
 #include "lapic.h"
 #include "ioapic.h"
 #include "acpica.h"
-#include "hpet.h"
 
 #include 
 #include 
@@ -120,7 +119,6 @@ __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.18
 #endif
 
 #include 
-#include 
 #include 
 #include 
 
@@ -204,8 +202,6 @@ static vaddr_t cmos_data_mapping;
 #endif
 struct cpu_info *cpu_starting;
 
-int (*cpu_nullop_ptr)(void *) = nullop;
-
 #ifdef MULTIPROCESSOR
 void		cpu_hatch(void *);
 static void	cpu_boot_secondary(struct cpu_info *ci);
@@ -437,11 +433,8 @@ cpu_attach(device_t parent, device_t sel
 	 * must be done to allow booting other processors.
 	 */
 	if (!again) {
-		/* Make sure DELAY() (likely i8254_delay()) is initialized. */
-		DELAY(1);
-
-		/* Basic init. */
 		atomic_or_32(>ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
+		/* Basic init. */
 		cpu_intr_init(ci);
 		cpu_get_tsc_freq(ci);
 		cpu_init(ci);
@@ -458,6 +451,8 @@ cpu_attach(device_t parent, device_t sel
 lapic_calibrate_timer(ci);
 		}
 #endif
+		/* Make sure DELAY() is initialized. */
+		DELAY(1);
 		kcsan_cpu_init(ci);
 		again = true;
 	}
@@ -723,6 +718,7 @@ cpu_init(struct cpu_info *ci)
 
 	if (ci != _info_primary) {
 		/* Synchronize TSC */
+		wbinvd();
 		atomic_or_32(>ci_flags, CPUF_RUNNING);
 		tsc_sync_ap(ci);
 	} else {
@@ -738,14 +734,6 @@ cpu_boot_secondary_processors(void)
 	kcpuset_t *cpus;
 	u_long i;
 
-#if NHPET > 0
-	/* Use HPET delay, and re-calibrate TSC on boot CPU using HPET. */
-	if (hpet_delay_p() && x86_delay == i8254_delay) {
-		delay_func = x86_delay = hpet_delay;
-		cpu_get_tsc_freq(curcpu());
-	}
-#endif
-
 	/* Now that we know the number of CPUs, patch the text segment. */
 	x86_patch(false);
 
@@ -854,6 +842,7 @@ cpu_start_secondary(struct cpu_info *ci)
 		 */
 		psl = x86_read_psl();
 		x86_disable_intr();
+		wbinvd();
 		tsc_sync_bp(ci);
 		x86_write_psl(psl);
 	}
@@ -884,6 +873,7 @@ cpu_boot_secondary(struct cpu_info *ci)
 		drift = ci->ci_data.cpu_cc_skew;
 		psl = x86_read_psl();
 		x86_disable_intr();
+		wbinvd();
 		tsc_sync_bp(ci);
 		x86_write_psl(psl);
 		drift -= ci->ci_data.cpu_cc_skew;
@@ -929,6 +919,7 @@ cpu_hatch(void *v)
 	 * Synchronize the TSC for the first time. Note that interrupts are
 	 * off at this point.
 	 */
+	wbinvd();
 	atomic_or_32(>ci_flags, CPUF_PRESENT);
 	tsc_sync_ap(ci);
 
@@ -1319,8 +1310,7 @@ cpu_shutdown(device_t dv, int how)
 void
 cpu_get_tsc_freq(struct cpu_info *ci)
 {
-	uint64_t freq = 0, t0, t1;
-	int64_t overhead;
+	uint64_t freq = 0, last_tsc;
 
 	if (cpu_hascounter())
 		freq = cpu_tsc_freq_cpuid(ci);
@@ -1329,31 +1319,11 @@ cpu_get_tsc_freq(struct cpu_info *ci)
 		/* Use TSC frequency taken from CPUID. */
 		ci->ci_data.cpu_cc_freq = freq;
 	} else {
-		/*
-		 * Work out the approximate overhead involved below.
-		 * Discard the result of the first go around the loop.
-		 */
-		overhead = 0;		
-		for (int i = 0; i <= 8; i++) {
-			__insn_barrier();
-			t0 = cpu_counter_serializing();
-			(*cpu_nullop_ptr)(NULL);
-			t1 = cpu_counter_serializing();
-			__insn_barrier();
-			if (i > 0) {
-overhead += (t1 - t0);
-			}
-		}
-		overhead >>= 3;
-
-		/* Now warm up x86_delay() and do the calibration. */
-		x86_delay(1);
-		__insn_barrier();
-		t0 = cpu_counter_serializing();
+		/* Calibrate TSC frequency. */
+		last_tsc = cpu_counter_serializing();
 		x86_delay(10);
-		t1 = cpu_counter_serializing();
-		__insn_barrier();
-		ci->ci_data.cpu_cc_freq = (t1 - t0 - overhead) * 10;
+		ci->ci_data.cpu_cc_freq =
+		(cpu_counter_serializing() - last_tsc) * 10;
 	}
 }
 



CVS commit: src/sys/arch/x86/x86

2020-04-24 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Fri Apr 24 07:50:24 UTC 2020

Modified Files:
src/sys/arch/x86/x86: procfs_machdep.c

Log Message:
 Lowercase ppin.


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/sys/arch/x86/x86/procfs_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/procfs_machdep.c
diff -u src/sys/arch/x86/x86/procfs_machdep.c:1.37 src/sys/arch/x86/x86/procfs_machdep.c:1.38
--- src/sys/arch/x86/x86/procfs_machdep.c:1.37	Fri Apr 24 02:27:59 2020
+++ src/sys/arch/x86/x86/procfs_machdep.c	Fri Apr 24 07:50:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.37 2020/04/24 02:27:59 msaitoh Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.38 2020/04/24 07:50:24 msaitoh Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.37 2020/04/24 02:27:59 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.38 2020/04/24 07:50:24 msaitoh Exp $");
 
 #include 
 #include 
@@ -155,7 +155,7 @@ static const char * const x86_features[]
 	{ /* (13) AMD 0x8008 ebx */
 	"clzero", "irperf", "xsaveerptr", NULL, "rdpru", NULL, NULL, NULL,
 	NULL, "wbnoinvd", NULL, NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, "PPIN",
+	NULL, NULL, NULL, NULL, NULL, NULL, NULL, "ppin",
 	NULL, "virt_ssbd", NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (14) 0x0006 eax */



CVS commit: src/sys/arch/x86/x86

2020-04-23 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Fri Apr 24 02:27:59 UTC 2020

Modified Files:
src/sys/arch/x86/x86: procfs_machdep.c

Log Message:
 Add AMD protected processor identification number (PPIN).


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/sys/arch/x86/x86/procfs_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/procfs_machdep.c
diff -u src/sys/arch/x86/x86/procfs_machdep.c:1.36 src/sys/arch/x86/x86/procfs_machdep.c:1.37
--- src/sys/arch/x86/x86/procfs_machdep.c:1.36	Wed Apr  1 08:21:38 2020
+++ src/sys/arch/x86/x86/procfs_machdep.c	Fri Apr 24 02:27:59 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.36 2020/04/01 08:21:38 msaitoh Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.37 2020/04/24 02:27:59 msaitoh Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.36 2020/04/01 08:21:38 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.37 2020/04/24 02:27:59 msaitoh Exp $");
 
 #include 
 #include 
@@ -155,7 +155,7 @@ static const char * const x86_features[]
 	{ /* (13) AMD 0x8008 ebx */
 	"clzero", "irperf", "xsaveerptr", NULL, "rdpru", NULL, NULL, NULL,
 	NULL, "wbnoinvd", NULL, NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+	NULL, NULL, NULL, NULL, NULL, NULL, NULL, "PPIN",
 	NULL, "virt_ssbd", NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (14) 0x0006 eax */



CVS commit: src/sys/arch/x86/x86

2020-04-23 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Thu Apr 23 21:35:18 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu.c

Log Message:
- Install HPET based DELAY() before going multiuser then recalibrate the TSC.
  Idea from joerg@.

- Take overhead into account when computing CPU frequency.

- Don't flush cache before computing TSC skew.


To generate a diff of this commit:
cvs rdiff -u -r1.185 -r1.186 src/sys/arch/x86/x86/cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.185 src/sys/arch/x86/x86/cpu.c:1.186
--- src/sys/arch/x86/x86/cpu.c:1.185	Tue Apr 21 02:56:37 2020
+++ src/sys/arch/x86/x86/cpu.c	Thu Apr 23 21:35:18 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: cpu.c,v 1.185 2020/04/21 02:56:37 msaitoh Exp $	*/
+/*	$NetBSD: cpu.c,v 1.186 2020/04/23 21:35:18 ad Exp $	*/
 
 /*
- * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
+ * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.185 2020/04/21 02:56:37 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.186 2020/04/23 21:35:18 ad Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -73,6 +73,7 @@ __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.18
 #include "lapic.h"
 #include "ioapic.h"
 #include "acpica.h"
+#include "hpet.h"
 
 #include 
 #include 
@@ -118,6 +119,7 @@ __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.18
 #endif
 
 #include 
+#include 
 #include 
 #include 
 
@@ -197,6 +199,8 @@ static vaddr_t cmos_data_mapping;
 #endif
 struct cpu_info *cpu_starting;
 
+int (*cpu_nullop_ptr)(void *) = nullop;
+
 #ifdef MULTIPROCESSOR
 void		cpu_hatch(void *);
 static void	cpu_boot_secondary(struct cpu_info *ci);
@@ -428,8 +432,11 @@ cpu_attach(device_t parent, device_t sel
 	 * must be done to allow booting other processors.
 	 */
 	if (!again) {
-		atomic_or_32(>ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
+		/* Make sure DELAY() (likely i8254_delay()) is initialized. */
+		DELAY(1);
+
 		/* Basic init. */
+		atomic_or_32(>ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
 		cpu_intr_init(ci);
 		cpu_get_tsc_freq(ci);
 		cpu_init(ci);
@@ -445,8 +452,6 @@ cpu_attach(device_t parent, device_t sel
 			lapic_calibrate_timer(ci);
 		}
 #endif
-		/* Make sure DELAY() is initialized. */
-		DELAY(1);
 		kcsan_cpu_init(ci);
 		again = true;
 	}
@@ -704,7 +709,6 @@ cpu_init(struct cpu_info *ci)
 
 	if (ci != _info_primary) {
 		/* Synchronize TSC */
-		wbinvd();
 		atomic_or_32(>ci_flags, CPUF_RUNNING);
 		tsc_sync_ap(ci);
 	} else {
@@ -720,6 +724,14 @@ cpu_boot_secondary_processors(void)
 	kcpuset_t *cpus;
 	u_long i;
 
+#if NHPET > 0
+	/* Use HPET delay, and re-calibrate TSC on boot CPU using HPET. */
+	if (hpet_delay_p() && x86_delay == i8254_delay) {
+		delay_func = x86_delay = hpet_delay;
+		cpu_get_tsc_freq(curcpu());
+	}
+#endif
+
 #ifndef XEN
 	/* Now that we know the number of CPUs, patch the text segment. */
 	x86_patch(false);
@@ -830,7 +842,6 @@ cpu_start_secondary(struct cpu_info *ci)
 		 */
 		psl = x86_read_psl();
 		x86_disable_intr();
-		wbinvd();
 		tsc_sync_bp(ci);
 		x86_write_psl(psl);
 	}
@@ -861,7 +872,6 @@ cpu_boot_secondary(struct cpu_info *ci)
 		drift = ci->ci_data.cpu_cc_skew;
 		psl = x86_read_psl();
 		x86_disable_intr();
-		wbinvd();
 		tsc_sync_bp(ci);
 		x86_write_psl(psl);
 		drift -= ci->ci_data.cpu_cc_skew;
@@ -907,7 +917,6 @@ cpu_hatch(void *v)
 	 * Synchronize the TSC for the first time. Note that interrupts are
 	 * off at this point.
 	 */
-	wbinvd();
 	atomic_or_32(>ci_flags, CPUF_PRESENT);
 	tsc_sync_ap(ci);
 
@@ -1295,7 +1304,8 @@ cpu_shutdown(device_t dv, int how)
 void
 cpu_get_tsc_freq(struct cpu_info *ci)
 {
-	uint64_t freq = 0, last_tsc;
+	uint64_t freq = 0, t0, t1;
+	int64_t overhead;
 
 	if (cpu_hascounter())
 		freq = cpu_tsc_freq_cpuid(ci);
@@ -1304,11 +1314,31 @@ cpu_get_tsc_freq(struct cpu_info *ci)
 		/* Use TSC frequency taken from CPUID. */
 		ci->ci_data.cpu_cc_freq = freq;
 	} else {
-		/* Calibrate TSC frequency. */
-		last_tsc = cpu_counter_serializing();
+		/*
+		 * Work out the approximate overhead involved below.
+		 * Discard the result of the first go around the loop.
+		 */
+		overhead = 0;		
+		for (int i = 0; i <= 8; i++) {
+			__insn_barrier();
+			t0 = cpu_counter_serializing();
+			(*cpu_nullop_ptr)(NULL);
+			t1 = cpu_counter_serializing();
+			__insn_barrier();
+			if (i > 0) {
+overhead += (t1 - t0);
+			}
+		}
+		overhead >>= 3;
+
+		/* Now warm up x86_delay() and do the calibration. */
+		x86_delay(1);
+		__insn_barrier();
+		t0 = cpu_counter_serializing();
 		x86_delay(10);
-		ci->ci_data.cpu_cc_freq =
-		(cpu_counter_serializing() - last_tsc) * 10;
+		t1 = cpu_counter_serializing();
+		__insn_barrier();
+		ci->ci_data.cpu_cc_freq = (t1 - t0 - 

CVS commit: src/sys/arch/x86/x86

2020-04-23 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Thu Apr 23 20:38:33 UTC 2020

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
When computing TSC skew make 8 measurements and use the average.


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.41 src/sys/arch/x86/x86/tsc.c:1.42
--- src/sys/arch/x86/x86/tsc.c:1.41	Tue Apr 21 02:56:37 2020
+++ src/sys/arch/x86/x86/tsc.c	Thu Apr 23 20:38:33 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: tsc.c,v 1.41 2020/04/21 02:56:37 msaitoh Exp $	*/
+/*	$NetBSD: tsc.c,v 1.42 2020/04/23 20:38:33 ad Exp $	*/
 
 /*-
- * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.41 2020/04/21 02:56:37 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.42 2020/04/23 20:38:33 ad Exp $");
 
 #include 
 #include 
@@ -229,13 +229,17 @@ tsc_read_bp(struct cpu_info *ci, uint64_
 void
 tsc_sync_bp(struct cpu_info *ci)
 {
-	uint64_t bptsc, aptsc;
+	int64_t bptsc, aptsc, bsum = 0, asum = 0;
 
 	tsc_read_bp(ci, , ); /* discarded - cache effects */
-	tsc_read_bp(ci, , );
+	for (int i = 0; i < 8; i++) {
+		tsc_read_bp(ci, , );
+		bsum += bptsc;
+		asum += aptsc;
+	}
 
 	/* Compute final value to adjust for skew. */
-	ci->ci_data.cpu_cc_skew = bptsc - aptsc;
+	ci->ci_data.cpu_cc_skew = (bsum - asum) >> 3;
 }
 
 /*
@@ -270,7 +274,9 @@ tsc_sync_ap(struct cpu_info *ci)
 {
 
 	tsc_post_ap(ci);
-	tsc_post_ap(ci);
+	for (int i = 0; i < 8; i++) {
+		tsc_post_ap(ci);
+	}
 }
 
 static void



CVS commit: src/sys/arch/x86/x86

2020-04-21 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Tue Apr 21 20:20:39 UTC 2020

Modified Files:
src/sys/arch/x86/x86: bus_space.c sys_machdep.c

Log Message:
two more files to convert to newer HYPERVISOR_physdev_op() interface


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/sys/arch/x86/x86/bus_space.c
cvs rdiff -u -r1.52 -r1.53 src/sys/arch/x86/x86/sys_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/bus_space.c
diff -u src/sys/arch/x86/x86/bus_space.c:1.43 src/sys/arch/x86/x86/bus_space.c:1.44
--- src/sys/arch/x86/x86/bus_space.c:1.43	Tue Dec  3 04:57:25 2019
+++ src/sys/arch/x86/x86/bus_space.c	Tue Apr 21 20:20:39 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_space.c,v 1.43 2019/12/03 04:57:25 riastradh Exp $	*/
+/*	$NetBSD: bus_space.c,v 1.44 2020/04/21 20:20:39 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.43 2019/12/03 04:57:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.44 2020/04/21 20:20:39 jdolecek Exp $");
 
 #include 
 #include 
@@ -136,10 +136,10 @@ x86_bus_space_init(void)
 #ifdef XENPV
 	/* We are privileged guest os - should have IO privileges. */
 	if (xendomain_is_privileged()) {
-		struct physdev_op physop;
-		physop.cmd = PHYSDEVOP_SET_IOPL;
-		physop.u.set_iopl.iopl = 1;
-		if (HYPERVISOR_physdev_op() != 0)
+		struct physdev_set_iopl set_iopl;
+		memset(_iopl, 0, sizeof(set_iopl));
+		set_iopl.iopl = 1;
+		if (HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, _iopl) != 0)
 			panic("Unable to obtain IOPL, "
 			"despite being SIF_PRIVILEGED");
 	}

Index: src/sys/arch/x86/x86/sys_machdep.c
diff -u src/sys/arch/x86/x86/sys_machdep.c:1.52 src/sys/arch/x86/x86/sys_machdep.c:1.53
--- src/sys/arch/x86/x86/sys_machdep.c:1.52	Sun Nov 10 21:16:34 2019
+++ src/sys/arch/x86/x86/sys_machdep.c	Tue Apr 21 20:20:39 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_machdep.c,v 1.52 2019/11/10 21:16:34 chs Exp $	*/
+/*	$NetBSD: sys_machdep.c,v 1.53 2020/04/21 20:20:39 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 1998, 2007, 2009, 2017 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.52 2019/11/10 21:16:34 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.53 2020/04/21 20:20:39 jdolecek Exp $");
 
 #include "opt_mtrr.h"
 #include "opt_user_ldt.h"
@@ -377,16 +377,15 @@ x86_iopl(struct lwp *l, void *args, regi
 		iopl = SEL_KPL;
 
 {
-	struct physdev_op physop;
 	struct pcb *pcb;
 
 	pcb = lwp_getpcb(l);
 	pcb->pcb_iopl = iopl;
 
 	/* Force the change at ring 0. */
-	physop.cmd = PHYSDEVOP_SET_IOPL;
-	physop.u.set_iopl.iopl = iopl;
-	HYPERVISOR_physdev_op();
+	struct physdev_set_iopl set_iopl;
+	set_iopl.iopl = iopl;
+	HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, _iopl);
 }
 #elif defined(__x86_64__)
 	if (ua.iopl)



CVS commit: src/sys/arch/x86/x86

2020-04-21 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Tue Apr 21 07:08:12 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu_subr.c

Log Message:
 Print "Hz".


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/x86/x86/identcpu_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu_subr.c
diff -u src/sys/arch/x86/x86/identcpu_subr.c:1.1 src/sys/arch/x86/x86/identcpu_subr.c:1.2
--- src/sys/arch/x86/x86/identcpu_subr.c:1.1	Tue Apr 21 02:56:37 2020
+++ src/sys/arch/x86/x86/identcpu_subr.c	Tue Apr 21 07:08:12 2020
@@ -33,7 +33,7 @@
  * See src/usr.sbin/cpuctl/{Makefile, arch/i386.c}).
  */
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.1 2020/04/21 02:56:37 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu_subr.c,v 1.2 2020/04/21 07:08:12 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "lapic.h"
@@ -110,7 +110,7 @@ cpu_tsc_freq_cpuid(struct cpu_info *ci)
 			x86_cpuid(0x16, descs);
 			if (descs[0] != 0) {
 aprint_verbose_dev(ci->ci_dev,
-"CPU base freq %" PRIu64 "\n",
+"CPU base freq %" PRIu64 " Hz\n",
 (uint64_t)descs[0] * 100);
 
 /*
@@ -125,7 +125,7 @@ cpu_tsc_freq_cpuid(struct cpu_info *ci)
 			}
 			if (descs[1] != 0) {
 aprint_verbose_dev(ci->ci_dev,
-"CPU max freq %" PRIu64 "\n",
+"CPU max freq %" PRIu64 " Hz\n",
 (uint64_t)descs[1] * 100);
 			}
 		}
@@ -139,7 +139,7 @@ cpu_tsc_freq_cpuid(struct cpu_info *ci)
 #endif
 	}
 	if (freq != 0)
-		aprint_verbose_dev(ci->ci_dev, "TSC freq %" PRIu64 "\n",
+		aprint_verbose_dev(ci->ci_dev, "TSC freq %" PRIu64 " Hz\n",
 		freq);
 
 	return freq;



CVS commit: src/sys/arch/x86/x86

2020-04-20 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Tue Apr 21 05:18:14 UTC 2020

Modified Files:
src/sys/arch/x86/x86: i8259.c

Log Message:
Whitespace fix. No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/arch/x86/x86/i8259.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/i8259.c
diff -u src/sys/arch/x86/x86/i8259.c:1.23 src/sys/arch/x86/x86/i8259.c:1.24
--- src/sys/arch/x86/x86/i8259.c:1.23	Mon Feb 11 14:59:33 2019
+++ src/sys/arch/x86/x86/i8259.c	Tue Apr 21 05:18:14 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: i8259.c,v 1.23 2019/02/11 14:59:33 cherry Exp $	*/
+/*	$NetBSD: i8259.c,v 1.24 2020/04/21 05:18:14 msaitoh Exp $	*/
 
 /*
  * Copyright 2002 (c) Wasabi Systems, Inc.
@@ -70,9 +70,9 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.23 2019/02/11 14:59:33 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.24 2020/04/21 05:18:14 msaitoh Exp $");
 
-#include  
+#include 
 #include 
 #include 
 #include 
@@ -83,7 +83,7 @@ __KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.
 #include 
 
 #include 
-#include   
+#include 
 #include 
 #include 
 #include 
@@ -163,7 +163,7 @@ i8259_default_setup(void)
 		/* reset; program device, level-triggered, four bytes */
 		outb(IO_ICU2 + PIC_ICW1, ICW1_SELECT | ICW1_LTIM | ICW1_IC4);
 	else
-#endif	
+#endif
 		/* reset; program device, four bytes */
 		outb(IO_ICU2 + PIC_ICW1, ICW1_SELECT | ICW1_IC4);
 
@@ -243,7 +243,7 @@ i8259_reinit_irqs(void)
 
 	irqs = 0;
 	for (irq = 0; irq < array_len; irq++)
-#if !defined(XENPV)		
+#if !defined(XENPV)
 		if (ci->ci_isources[irq] != NULL)
 #else
 		if (ci->ci_xsources[irq] != NULL)



CVS commit: src/sys/arch/x86/x86

2020-04-19 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Mon Apr 20 04:23:24 UTC 2020

Modified Files:
src/sys/arch/x86/x86: cpu.c

Log Message:
 Whitespace fix. No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.183 -r1.184 src/sys/arch/x86/x86/cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.183 src/sys/arch/x86/x86/cpu.c:1.184
--- src/sys/arch/x86/x86/cpu.c:1.183	Fri Apr 10 14:35:26 2020
+++ src/sys/arch/x86/x86/cpu.c	Mon Apr 20 04:23:23 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.183 2020/04/10 14:35:26 bouyer Exp $	*/
+/*	$NetBSD: cpu.c,v 1.184 2020/04/20 04:23:23 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.183 2020/04/10 14:35:26 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.184 2020/04/20 04:23:23 msaitoh Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -198,9 +198,9 @@ static vaddr_t cmos_data_mapping;
 struct cpu_info *cpu_starting;
 
 #ifdef MULTIPROCESSOR
-void	cpu_hatch(void *);
-static void	cpu_boot_secondary(struct cpu_info *ci);
-static void	cpu_start_secondary(struct cpu_info *ci);
+void		cpu_hatch(void *);
+static void	cpu_boot_secondary(struct cpu_info *ci);
+static void	cpu_start_secondary(struct cpu_info *ci);
 #if NLAPIC > 0
 static void	cpu_copy_trampoline(paddr_t);
 #endif
@@ -276,7 +276,7 @@ cpu_vm_init(struct cpu_info *ci)
 		cai = >ci_cinfo[i];
 
 		tcolors = atop(cai->cai_totalsize);
-		switch(cai->cai_associativity) {
+		switch (cai->cai_associativity) {
 		case 0xff:
 			tcolors = 1; /* fully associative */
 			break;
@@ -300,7 +300,7 @@ cpu_vm_init(struct cpu_info *ci)
 			}
 			if (picked == 1) {
 panic("desired number of cache colors %d is "
-			  	" > 1, but not even!", ncolors);
+" > 1, but not even!", ncolors);
 			}
 			ncolors = picked;
 		}
@@ -1018,7 +1018,7 @@ cpu_debug_dump(void)
 {
 	struct cpu_info *ci;
 	CPU_INFO_ITERATOR cii;
-	const char sixtyfour64space[] = 
+	const char sixtyfour64space[] =
 #ifdef _LP64
 			   ""
 #endif



CVS commit: src/sys/arch/x86/x86

2020-04-19 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Mon Apr 20 04:17:52 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
Whitespace fix. No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.105 -r1.106 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.105 src/sys/arch/x86/x86/identcpu.c:1.106
--- src/sys/arch/x86/x86/identcpu.c:1.105	Thu Apr  9 02:07:01 2020
+++ src/sys/arch/x86/x86/identcpu.c	Mon Apr 20 04:17:51 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.105 2020/04/09 02:07:01 christos Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.106 2020/04/20 04:17:51 msaitoh Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.105 2020/04/09 02:07:01 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.106 2020/04/20 04:17:51 msaitoh Exp $");
 
 #include "opt_xen.h"
 
@@ -62,7 +62,7 @@ __KERNEL_RCSID(0, "$NetBSD: identcpu.c,v
 
 static const struct x86_cache_info intel_cpuid_cache_info[] = INTEL_CACHE_INFO;
 
-static const struct x86_cache_info amd_cpuid_l2l3cache_assoc_info[] = 
+static const struct x86_cache_info amd_cpuid_l2l3cache_assoc_info[] =
 	AMD_L2L3CACHE_INFO;
 
 int cpu_vendor;
@@ -82,7 +82,7 @@ const int i386_nocpuid_cpus[] = {
 	CPUVENDOR_INTEL, CPUCLASS_386,	/* CPU_386SX */
 	CPUVENDOR_INTEL, CPUCLASS_386,	/* CPU_386   */
 	CPUVENDOR_INTEL, CPUCLASS_486,	/* CPU_486SX */
-	CPUVENDOR_INTEL, CPUCLASS_486, 	/* CPU_486   */
+	CPUVENDOR_INTEL, CPUCLASS_486,	/* CPU_486   */
 	CPUVENDOR_CYRIX, CPUCLASS_486,	/* CPU_486DLC */
 	CPUVENDOR_CYRIX, CPUCLASS_486,	/* CPU_6x86 */
 	CPUVENDOR_NEXGEN, CPUCLASS_386,	/* CPU_NX586 */
@@ -174,7 +174,7 @@ cpu_probe_intel_cache(struct cpu_info *c
 	int iterations, i, j;
 	uint8_t desc;
 
-	if (cpuid_level >= 2) { 
+	if (cpuid_level >= 2) {
 		/* Parse the cache info from `cpuid leaf 2', if we have it. */
 		x86_cpuid(2, descs);
 		iterations = descs[0] & 0xff;
@@ -488,7 +488,7 @@ cpu_probe_cyrix_cmn(struct cpu_info *ci)
 	 */
 	cyrix_write_reg(0xc2, cyrix_read_reg(0xc2) | 0x08);
 
-	/* 
+	/*
 	 * Do not disable the TSC on the Geode GX, it's reported to
 	 * work fine.
 	 */
@@ -525,7 +525,7 @@ cpu_probe_winchip(struct cpu_info *ci)
 
 	if (cpu_vendor != CPUVENDOR_IDT ||
 	CPUID_TO_FAMILY(ci->ci_signature) != 5)
-		return;
+		return;
 
 	/* WinChip C6 */
 	if (CPUID_TO_MODEL(ci->ci_signature) == 4)
@@ -556,7 +556,7 @@ cpu_probe_c3(struct cpu_info *ci)
 		 *
 		 * Quoting from page 3-4 of: "VIA Eden ESP Processor Datasheet"
 		 * http://www.via.com.tw/download/mainboards/6/14/Eden20v115.pdf
-		 * 
+		 *
 		 * 1. The CMPXCHG8B instruction is provided and always enabled,
 		 *however, it appears disabled in the corresponding CPUID
 		 *function bit 0 to avoid a bug in an early version of
@@ -646,7 +646,7 @@ cpu_probe_c3(struct cpu_info *ci)
 	if (ci->ci_feat_val[4] & CPUID_VIA_DO_ACE) {
 		msr = rdmsr(MSR_VIA_ACE);
 		wrmsr(MSR_VIA_ACE, msr & ~VIA_ACE_ALTINST);
-	} 
+	}
 
 	/*
 	 * Determine L1 cache/TLB info.
@@ -714,7 +714,7 @@ cpu_probe_geode(struct cpu_info *ci)
 
 	if (memcmp("Geode by NSC", ci->ci_vendor, 12) != 0 ||
 	CPUID_TO_FAMILY(ci->ci_signature) != 5)
-		return;
+		return;
 
 	cpu_probe_cyrix_cmn(ci);
 	cpu_probe_amd_cache(ci);



CVS commit: src/sys/arch/x86/x86

2020-04-08 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Apr  9 02:07:01 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
flip the comparison again


To generate a diff of this commit:
cvs rdiff -u -r1.104 -r1.105 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.104 src/sys/arch/x86/x86/identcpu.c:1.105
--- src/sys/arch/x86/x86/identcpu.c:1.104	Wed Apr  8 21:55:58 2020
+++ src/sys/arch/x86/x86/identcpu.c	Wed Apr  8 22:07:01 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.104 2020/04/09 01:55:58 christos Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.105 2020/04/09 02:07:01 christos Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.104 2020/04/09 01:55:58 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.105 2020/04/09 02:07:01 christos Exp $");
 
 #include "opt_xen.h"
 
@@ -750,7 +750,7 @@ cpu_probe_vortex86(struct cpu_info *ci)
 	static const char *cpu_vortex86_flavor[] = {
 	"??", "SX", "DX", "MX", "DX2", "MX+", "DX3", "EX", "EX2",
 	};
-	idx = __arraycount(cpu_vortex86_flavor) < idx ? idx : 0;
+	idx = idx < __arraycount(cpu_vortex86_flavor) ? idx : 0;
 	snprintf(cpu_brand_string, sizeof(cpu_brand_string), "Vortex86%s",
 	cpu_vortex86_flavor[idx]);
 



CVS commit: src/sys/arch/x86/x86

2020-04-08 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Apr  9 01:55:58 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
use __arraycount, and fix comparison


To generate a diff of this commit:
cvs rdiff -u -r1.103 -r1.104 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.103 src/sys/arch/x86/x86/identcpu.c:1.104
--- src/sys/arch/x86/x86/identcpu.c:1.103	Wed Apr  8 21:49:26 2020
+++ src/sys/arch/x86/x86/identcpu.c	Wed Apr  8 21:55:58 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.103 2020/04/09 01:49:26 christos Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.104 2020/04/09 01:55:58 christos Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.103 2020/04/09 01:49:26 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.104 2020/04/09 01:55:58 christos Exp $");
 
 #include "opt_xen.h"
 
@@ -750,7 +750,7 @@ cpu_probe_vortex86(struct cpu_info *ci)
 	static const char *cpu_vortex86_flavor[] = {
 	"??", "SX", "DX", "MX", "DX2", "MX+", "DX3", "EX", "EX2",
 	};
-	idx = sizeof(cpu_vortex86_flavor) > idx ? idx : 0;
+	idx = __arraycount(cpu_vortex86_flavor) < idx ? idx : 0;
 	snprintf(cpu_brand_string, sizeof(cpu_brand_string), "Vortex86%s",
 	cpu_vortex86_flavor[idx]);
 



CVS commit: src/sys/arch/x86/x86

2020-04-08 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Apr  9 01:49:26 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c

Log Message:
Add EX2 for Vortex86 SoCs (Andrius V)


To generate a diff of this commit:
cvs rdiff -u -r1.102 -r1.103 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.102 src/sys/arch/x86/x86/identcpu.c:1.103
--- src/sys/arch/x86/x86/identcpu.c:1.102	Sat Apr  4 10:49:35 2020
+++ src/sys/arch/x86/x86/identcpu.c	Wed Apr  8 21:49:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.102 2020/04/04 14:49:35 ad Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.103 2020/04/09 01:49:26 christos Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.102 2020/04/04 14:49:35 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.103 2020/04/09 01:49:26 christos Exp $");
 
 #include "opt_xen.h"
 
@@ -727,7 +727,7 @@ cpu_probe_vortex86(struct cpu_info *ci)
 #define PCI_MODE1_DATA_REG	0x0cfc
 #define PCI_MODE1_ENABLE	0x8000UL
 
-	uint32_t reg;
+	uint32_t reg, idx;
 
 	if (cpu_vendor != CPUVENDOR_VORTEX86)
 		return;
@@ -741,17 +741,18 @@ cpu_probe_vortex86(struct cpu_info *ci)
 	outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE | 0x90);
 	reg = inl(PCI_MODE1_DATA_REG);
 
-	if ((reg & 0xf8ff) != 0x30504d44) {
-		reg = 0;
+	if ((reg & 0xf0ff) != 0x30504d44) {
+		idx = 0;
 	} else {
-		reg = (reg >> 24) & 7;
+		idx = (reg >> 24) & 0xf;
 	}
 
 	static const char *cpu_vortex86_flavor[] = {
-	"??", "SX", "DX", "MX", "DX2", "MX+", "DX3", "EX",
+	"??", "SX", "DX", "MX", "DX2", "MX+", "DX3", "EX", "EX2",
 	};
+	idx = sizeof(cpu_vortex86_flavor) > idx ? idx : 0;
 	snprintf(cpu_brand_string, sizeof(cpu_brand_string), "Vortex86%s",
-	cpu_vortex86_flavor[reg]);
+	cpu_vortex86_flavor[idx]);
 
 #undef PCI_MODE1_ENABLE
 #undef PCI_MODE1_ADDRESS_REG



CVS commit: src/sys/arch/x86/x86

2020-04-04 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sat Apr  4 14:49:36 UTC 2020

Modified Files:
src/sys/arch/x86/x86: identcpu.c x86_machdep.c

Log Message:
Enable MONITOR/MWAIT idle on AMD chips, except some buggy Ryzens.


To generate a diff of this commit:
cvs rdiff -u -r1.101 -r1.102 src/sys/arch/x86/x86/identcpu.c
cvs rdiff -u -r1.135 -r1.136 src/sys/arch/x86/x86/x86_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.101 src/sys/arch/x86/x86/identcpu.c:1.102
--- src/sys/arch/x86/x86/identcpu.c:1.101	Fri Apr  3 22:45:30 2020
+++ src/sys/arch/x86/x86/identcpu.c	Sat Apr  4 14:49:35 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.101 2020/04/03 22:45:30 ad Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.102 2020/04/04 14:49:35 ad Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.101 2020/04/03 22:45:30 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.102 2020/04/04 14:49:35 ad Exp $");
 
 #include "opt_xen.h"
 
@@ -362,19 +362,17 @@ cpu_probe_amd_cache(struct cpu_info *ci)
 }
 
 static void
-cpu_probe_amd(struct cpu_info *ci)
+cpu_probe_amd_errata(struct cpu_info *ci)
 {
+	u_int model;
 	uint64_t val;
 	int flag;
 
-	if (cpu_vendor != CPUVENDOR_AMD)
-		return;
-	if (CPUID_TO_FAMILY(ci->ci_signature) < 5)
-		return;
+	model = CPUID_TO_MODEL(ci->ci_signature);
 
 	switch (CPUID_TO_FAMILY(ci->ci_signature)) {
 	case 0x05: /* K5 */
-		if (CPUID_TO_MODEL(ci->ci_signature) == 0) {
+		if (model == 0) {
 			/*
 			 * According to the AMD Processor Recognition App Note,
 			 * the AMD-K5 Model 0 uses the wrong bit to indicate
@@ -402,9 +400,34 @@ cpu_probe_amd(struct cpu_info *ci)
 			wrmsr(MSR_BU_CFG2, val);
 		}
 		break;
+
+	case 0x17:
+		/*
+		 * "Revision Guide for AMD Family 17h Models 00h-0Fh
+		 * Processors" revision 1.12:
+		 *
+		 * 1057 MWAIT or MWAITX Instructions May Fail to Correctly
+		 * Exit From the Monitor Event Pending State
+		 *
+		 * 1109 MWAIT Instruction May Hang a Thread
+		 */
+		if (model == 0x01) {
+			cpu_feature[1] &= ~CPUID2_MONITOR;
+			ci->ci_feat_val[1] &= ~CPUID2_MONITOR;
+		}
+		break;
 	}
+}
+
+static void
+cpu_probe_amd(struct cpu_info *ci)
+{
+
+	if (cpu_vendor != CPUVENDOR_AMD)
+		return;
 
 	cpu_probe_amd_cache(ci);
+	cpu_probe_amd_errata(ci);
 }
 
 static inline uint8_t

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.135 src/sys/arch/x86/x86/x86_machdep.c:1.136
--- src/sys/arch/x86/x86/x86_machdep.c:1.135	Wed Jan 29 01:54:34 2020
+++ src/sys/arch/x86/x86/x86_machdep.c	Sat Apr  4 14:49:35 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.135 2020/01/29 01:54:34 manu Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.136 2020/04/04 14:49:35 ad Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.135 2020/01/29 01:54:34 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.136 2020/04/04 14:49:35 ad Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -458,8 +458,7 @@ x86_cpu_idle_init(void)
 {
 
 #ifndef XENPV
-	if ((cpu_feature[1] & CPUID2_MONITOR) == 0 ||
-	cpu_vendor == CPUVENDOR_AMD)
+	if ((cpu_feature[1] & CPUID2_MONITOR) == 0)
 		x86_cpu_idle_set(x86_cpu_idle_halt, "halt", true);
 	else
 		x86_cpu_idle_set(x86_cpu_idle_mwait, "mwait", false);



CVS commit: src/sys/arch/x86/x86

2020-04-02 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Apr  3 03:05:39 UTC 2020

Modified Files:
src/sys/arch/x86/x86: tsc.c

Log Message:
Fix TSC drift is observed almost every time wrongly.

Ths "TSC drift" in tsc_tc_init() means the cpu_cc_skew delta between
first measurement (in cpu_start_secondary) and second measurement
(in cpu_boot_secondary), that is, the TSC drift is expected to be
almost zero.  However, the second measument in current implementation
is added extra cpu_cc_skew accidentally, so current delta value means
almost cpu_cc_skew wrongly.

tsc_sync_bp and tsc_sync_ap should use rdtsc() to get raw values.

Advised by nonaka@n.o, thanks.


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.38 src/sys/arch/x86/x86/tsc.c:1.39
--- src/sys/arch/x86/x86/tsc.c:1.38	Fri Feb 21 00:26:22 2020
+++ src/sys/arch/x86/x86/tsc.c	Fri Apr  3 03:05:39 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.38 2020/02/21 00:26:22 joerg Exp $	*/
+/*	$NetBSD: tsc.c,v 1.39 2020/04/03 03:05:39 knakahara Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.38 2020/02/21 00:26:22 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.39 2020/04/03 03:05:39 knakahara Exp $");
 
 #include 
 #include 
@@ -201,13 +201,13 @@ tsc_read_bp(struct cpu_info *ci, uint64_
 
 	/* Flag it and read our TSC. */
 	atomic_or_uint(>ci_flags, CPUF_SYNCTSC);
-	bptsc = cpu_counter_serializing() >> 1;
+	bptsc = (rdtsc() >> 1);
 
 	/* Wait for remote to complete, and read ours again. */
 	while ((ci->ci_flags & CPUF_SYNCTSC) != 0) {
 		__insn_barrier();
 	}
-	bptsc += (cpu_counter_serializing() >> 1);
+	bptsc += (rdtsc() >> 1);
 
 	/* Wait for the results to come in. */
 	while (tsc_sync_cpu == ci) {
@@ -246,11 +246,11 @@ tsc_post_ap(struct cpu_info *ci)
 	while ((ci->ci_flags & CPUF_SYNCTSC) == 0) {
 		__insn_barrier();
 	}
-	tsc = (cpu_counter_serializing() >> 1);
+	tsc = (rdtsc() >> 1);
 
 	/* Instruct primary to read its counter. */
 	atomic_and_uint(>ci_flags, ~CPUF_SYNCTSC);
-	tsc += (cpu_counter_serializing() >> 1);
+	tsc += (rdtsc() >> 1);
 
 	/* Post result.  Ensure the whole value goes out atomically. */
 	(void)atomic_swap_64(_sync_val, tsc);



CVS commit: src/sys/arch/x86/x86

2020-03-27 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Fri Mar 27 09:47:03 UTC 2020

Modified Files:
src/sys/arch/x86/x86: coretemp.c

Log Message:
 Add special handling for model 0x0f stepping >=2 or mode 0x0e to get Tjmax.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/sys/arch/x86/x86/coretemp.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/coretemp.c
diff -u src/sys/arch/x86/x86/coretemp.c:1.36 src/sys/arch/x86/x86/coretemp.c:1.37
--- src/sys/arch/x86/x86/coretemp.c:1.36	Wed Jul 11 03:36:32 2018
+++ src/sys/arch/x86/x86/coretemp.c	Fri Mar 27 09:47:03 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: coretemp.c,v 1.36 2018/07/11 03:36:32 msaitoh Exp $ */
+/* $NetBSD: coretemp.c,v 1.37 2020/03/27 09:47:03 msaitoh Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: coretemp.c,v 1.36 2018/07/11 03:36:32 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: coretemp.c,v 1.37 2020/03/27 09:47:03 msaitoh Exp $");
 
 #include 
 #include 
@@ -271,6 +271,19 @@ coretemp_tjmax(device_t self)
 	sc->sc_tjmax = 100;
 
 	if ((model == 0x0f && stepping >= 2) || (model == 0x0e)) {
+		/*
+		 * Check MSR_IA32_PLATFORM_ID(0x17) bit 28. It's not documented
+		 * in the datasheet, but the following page describes the
+		 * detail:
+		 *   http://software.intel.com/en-us/articles/
+		 * mobile-intel-core2-processor-detection-table/
+		 *   Was: http://softwarecommunity.intel.com/Wiki/Mobility/
+		 * 720.htm
+		 */
+		if (rdmsr_safe(MSR_IA32_PLATFORM_ID, ) != 0)
+			goto notee;
+		if ((model < 0x17) && ((msr & __BIT(28)) == 0))
+			goto notee;
 
 		if (rdmsr_safe(MSR_IA32_EXT_CONFIG, ) == EFAULT)
 			return;
@@ -290,6 +303,7 @@ coretemp_tjmax(device_t self)
 		} else
 			sc->sc_tjmax = 90;
 	} else {
+notee:
 		/*
 		 * Attempt to get Tj(max) from IA32_TEMPERATURE_TARGET,
 		 * but only consider the interval [70, 110] C as valid.



CVS commit: src/sys/arch/x86/x86

2020-03-20 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Fri Mar 20 19:06:14 UTC 2020

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
- pmap_extract(): This needs to take the pmap's lock, to allow for
  concurrent removal of pages (a new requirement).

- pmap_remove_pv(): Keep hold time of pp_lock as short as possible.

- pmap_get_ptp(): Don't re-init struct pmap_page for PD PTPs.  Would
  have no ill effects but is wrong regardless.


To generate a diff of this commit:
cvs rdiff -u -r1.378 -r1.379 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.378 src/sys/arch/x86/x86/pmap.c:1.379
--- src/sys/arch/x86/x86/pmap.c:1.378	Thu Mar 19 18:58:14 2020
+++ src/sys/arch/x86/x86/pmap.c	Fri Mar 20 19:06:14 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.378 2020/03/19 18:58:14 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.379 2020/03/20 19:06:14 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.378 2020/03/19 18:58:14 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.379 2020/03/20 19:06:14 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -2163,20 +2163,22 @@ pmap_remove_pv(struct pmap *pmap, struct
 
 	pmap_check_pv(pmap, ptp, pp, va, true);
 
-	mutex_spin_enter(>pp_lock);
-	pp->pp_attrs |= oattrs;
 	if (pve == NULL) {
+		mutex_spin_enter(>pp_lock);
 		KASSERT(pp->pp_pte.pte_ptp == ptp);
 		KASSERT(pp->pp_pte.pte_va == va);
+		pp->pp_attrs |= oattrs;
 		pp->pp_pte.pte_ptp = NULL;
 		pp->pp_pte.pte_va = 0;
 		mutex_spin_exit(>pp_lock);
 	} else {
+		mutex_spin_enter(>pp_lock);
 		KASSERT(pp->pp_pte.pte_ptp != ptp ||
 		pp->pp_pte.pte_va != va);
 		KASSERT(pve->pve_pte.pte_ptp == ptp);
 		KASSERT(pve->pve_pte.pte_va == va);
 		KASSERT(pve->pve_pp == pp);
+		pp->pp_attrs |= oattrs;
 		LIST_REMOVE(pve, pve_list);
 		mutex_spin_exit(>pp_lock);
 
@@ -2347,7 +2349,7 @@ pmap_get_ptp(struct pmap *pmap, struct p
 		if (pt->pg[i] == NULL) {
 			pmap_unget_ptp(pmap, pt);
 			return ENOMEM;
-		} else {
+		} else if (pt->alloced[i]) {
 			pt->pg[i]->uanon = (struct vm_anon *)(vaddr_t)~0L;
 			rb_tree_init(_PAGE_TO_PP(pt->pg[i])->pp_rb,
 			_rbtree_ops);
@@ -3427,10 +3429,8 @@ pmap_extract(struct pmap *pmap, vaddr_t 
 	pd_entry_t pde;
 	pd_entry_t * const *pdes;
 	struct pmap *pmap2;
-	struct cpu_info *ci;
 	paddr_t pa;
-	lwp_t *l;
-	bool hard, rv;
+	bool rv;
 	int lvl;
 
 	if (__predict_false(pmap->pm_extract != NULL)) {
@@ -3448,29 +3448,11 @@ pmap_extract(struct pmap *pmap, vaddr_t 
 
 	rv = false;
 	pa = 0;
-	l = curlwp;
 
-	ci = l->l_cpu;
-	if (pmap == pmap_kernel() ||
-	__predict_true(!ci->ci_want_pmapload && ci->ci_pmap == pmap)) {
-		/*
-		 * no need to lock, because it's pmap_kernel() or our
-		 * own pmap and is active.  if a user pmap, the caller
-		 * will hold the vm_map write/read locked and so prevent
-		 * entries from disappearing while we are here.  ptps
-		 * can disappear via pmap_remove() and pmap_protect(),
-		 * but they are called with the vm_map write locked.
-		 */
-		hard = false;
-		ptes = PTE_BASE;
-		pdes = normal_pdes;
-		kpreempt_disable();
-	} else {
-		/* we lose, do it the hard way. */
-		hard = true;
+	if (pmap != pmap_kernel()) {
 		mutex_enter(>pm_lock);
-		pmap_map_ptes(pmap, , , );
 	}
+	pmap_map_ptes(pmap, , , );
 	if (pmap_pdes_valid(va, pdes, , )) {
 		if (lvl == 2) {
 			pa = (pde & PTE_LGFRAME) | (va & (NBPD_L2 - 1));
@@ -3484,15 +3466,14 @@ pmap_extract(struct pmap *pmap, vaddr_t 
 			}
 		}
 	}
-	if (__predict_false(hard)) {
-		pmap_unmap_ptes(pmap, pmap2);
+	pmap_unmap_ptes(pmap, pmap2);
+	if (pmap != pmap_kernel()) {
 		mutex_exit(>pm_lock);
-	} else {
-		kpreempt_enable();
 	}
 	if (pap != NULL) {
 		*pap = pa;
 	}
+
 	return rv;
 }
 



  1   2   3   4   5   6   7   8   >