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/include

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

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Add some definitions from Intel SDM:

 - CPUID leaf 7:0 %ecx bit 13 TME_EN (Total Memory Encryption)
 - CPUID leaf 7:0 %edx bit 18 PCONFIG (Platform CONFIGuration)


To generate a diff of this commit:
cvs rdiff -u -r1.176 -r1.177 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.176 src/sys/arch/x86/include/specialreg.h:1.177
--- src/sys/arch/x86/include/specialreg.h:1.176	Tue Nov 24 00:46:28 2020
+++ src/sys/arch/x86/include/specialreg.h	Sat Jul 10 17:08:37 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.176 2020/11/24 00:46:28 msaitoh Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.177 2021/07/10 17:08:37 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -458,6 +458,7 @@
 #define CPUID_SEF_VPCLMULQDQ	__BIT(10)
 #define CPUID_SEF_AVX512_VNNI	__BIT(11) /* Vector neural Network Instruction */
 #define CPUID_SEF_AVX512_BITALG	__BIT(12)
+#define CPUID_SEF_TME_EN	__BIT(13) /* Total Memory Encryption */
 #define CPUID_SEF_AVX512_VPOPCNTDQ __BIT(14)
 #define CPUID_SEF_LA57		__BIT(16) /* 57bit linear addr & 5LVL paging */
 #define CPUID_SEF_MAWAU		__BITS(21, 17) /* MAWAU for BND{LD,ST}X */
@@ -473,7 +474,7 @@
 	"b\0PREFETCHWT1\0" "b\1AVX512_VBMI\0" "b\2UMIP\0" "b\3PKU\0"	\
 	"b\4OSPKE\0"	"b\5WAITPKG\0"	"b\6AVX512_VBMI2\0" "b\7CET_SS\0" \
 	"b\10GFNI\0"	"b\11VAES\0"	"b\12VPCLMULQDQ\0" "b\13AVX512_VNNI\0"\
-	"b\14AVX512_BITALG\0"		"b\16AVX512_VPOPCNTDQ\0"	\
+	"b\14AVX512_BITALG\0" "b\15TME_EN\0" "b\16AVX512_VPOPCNTDQ\0"	\
 	"b\20LA57\0"			\
 	"f\21\5MAWAU\0"			"b\26RDPID\0"	"b\27KL\0"	\
 			"b\31CLDEMOTE\0"		"b\33MOVDIRI\0"	\
@@ -490,6 +491,7 @@
 #define CPUID_SEF_SERIALIZE	__BIT(14) /* SERIALIZE instruction */
 #define CPUID_SEF_HYBRID	__BIT(15) /* Hybrid part */
 #define CPUID_SEF_TSXLDTRK	__BIT(16) /* TSX suspend load addr tracking */
+#define CPUID_SEF_PCONFIG	__BIT(18) /* Platform CONFIGuration */
 #define CPUID_SEF_CET_IBT	__BIT(20) /* CET Indirect Branch Tracking */
 #define CPUID_SEF_IBRS		__BIT(26) /* IBRS / IBPB Speculation Control */
 #define CPUID_SEF_STIBP		__BIT(27) /* STIBP Speculation Control */
@@ -503,7 +505,7 @@
 	"\5" "FSREP_MOV"		\
 	"\11VP2INTERSECT" "\12SRBDS_CTRL" "\13MD_CLEAR"			\
 			"\16TSX_FORCE_ABORT" "\17SERIALIZE" "\20HYBRID"	\
-	"\21" "TSXLDTRK"		\
+	"\21" "TSXLDTRK"		"\23" "PCONFIG"			\
 	"\25" "CET_IBT"			\
 	"\33" "IBRS"	"\34" "STIBP"	\
 	"\35" "L1D_FLUSH" "\36" "ARCH_CAP" "\37CORE_CAP"	"\40" "SSBD"



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

2021-06-20 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Jun 21 03:01:23 UTC 2021

Modified Files:
src/sys/arch/x86/pci: pci_ranges.c

Log Message:
prop_dictionary_set_cstring_nocopy -> prop_dictionary_set_string_nocopy


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/x86/pci/pci_ranges.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/pci/pci_ranges.c
diff -u src/sys/arch/x86/pci/pci_ranges.c:1.8 src/sys/arch/x86/pci/pci_ranges.c:1.9
--- src/sys/arch/x86/pci/pci_ranges.c:1.8	Fri Mar  1 04:25:59 2019
+++ src/sys/arch/x86/pci/pci_ranges.c	Sun Jun 20 23:01:23 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pci_ranges.c,v 1.8 2019/03/01 09:25:59 msaitoh Exp $	*/
+/*	$NetBSD: pci_ranges.c,v 1.9 2021/06/21 03:01:23 christos Exp $	*/
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pci_ranges.c,v 1.8 2019/03/01 09:25:59 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pci_ranges.c,v 1.9 2021/06/21 03:01:23 christos Exp $");
 
 #include 
 #include 
@@ -784,7 +784,7 @@ pci_range_record(pci_chipset_tag_t pc, p
 		pci_decompose_tag(pc, pal->pal_tag, , , );
 
 		ok = ok &&
-		prop_dictionary_set_cstring_nocopy(rsvn, "type",
+		prop_dictionary_set_string_nocopy(rsvn, "type",
 		pci_alloc_regtype_string(pal->pal_type)) &&
 		prop_dictionary_set_uint64(rsvn, "address",
 		pal->pal_addr) &&



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

2021-06-06 Thread NONAKA Kimihiro
Module Name:src
Committed By:   nonaka
Date:   Sun Jun  6 11:35:22 UTC 2021

Modified Files:
src/sys/arch/x86/pci: amdzentemp.c

Log Message:
Fix build failure on i386.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/x86/pci/amdzentemp.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/pci/amdzentemp.c
diff -u src/sys/arch/x86/pci/amdzentemp.c:1.13 src/sys/arch/x86/pci/amdzentemp.c:1.14
--- src/sys/arch/x86/pci/amdzentemp.c:1.13	Sun Jun  6 08:45:18 2021
+++ src/sys/arch/x86/pci/amdzentemp.c	Sun Jun  6 11:35:22 2021
@@ -1,4 +1,4 @@
-/*  $NetBSD: amdzentemp.c,v 1.13 2021/06/06 08:45:18 nonaka Exp $ */
+/*  $NetBSD: amdzentemp.c,v 1.14 2021/06/06 11:35:22 nonaka Exp $ */
 /*  $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $   */
 
 /*
@@ -53,7 +53,7 @@
 
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.13 2021/06/06 08:45:18 nonaka Exp $ ");
+__KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.14 2021/06/06 11:35:22 nonaka Exp $ ");
 
 #include 
 #include 
@@ -431,8 +431,9 @@ static void
 amdzentemp_setup_ccd_sensors(struct amdzentemp_softc *sc)
 {
 	envsys_data_t *edata;
+	size_t i;
 	uint32_t temp;
-	int i, error;
+	int error;
 
 	for (i = 0; i < sc->sc_numsensors - 1; i++) {
 		error = amdsmn_read(sc->sc_smn,
@@ -446,7 +447,7 @@ amdzentemp_setup_ccd_sensors(struct amdz
 		edata->flags = ENVSYS_FHAS_ENTROPY;
 		edata->private = CCD_BASE + i;
 		snprintf(edata->desc, sizeof(edata->desc),
-		"cpu%u ccd%u temperature", device_unit(sc->sc_dev), i);
+		"cpu%u ccd%zu temperature", device_unit(sc->sc_dev), i);
 	}
 }
 



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

2021-06-06 Thread NONAKA Kimihiro
Module Name:src
Committed By:   nonaka
Date:   Sun Jun  6 08:45:18 UTC 2021

Modified Files:
src/sys/arch/x86/pci: amdzentemp.c

Log Message:
amdzentemp(4): Add support for per CCD temperature sensor from FreeBSD.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/x86/pci/amdzentemp.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/pci/amdzentemp.c
diff -u src/sys/arch/x86/pci/amdzentemp.c:1.12 src/sys/arch/x86/pci/amdzentemp.c:1.13
--- src/sys/arch/x86/pci/amdzentemp.c:1.12	Sat Jun  5 01:38:22 2021
+++ src/sys/arch/x86/pci/amdzentemp.c	Sun Jun  6 08:45:18 2021
@@ -1,4 +1,4 @@
-/*  $NetBSD: amdzentemp.c,v 1.12 2021/06/05 01:38:22 nonaka Exp $ */
+/*  $NetBSD: amdzentemp.c,v 1.13 2021/06/06 08:45:18 nonaka Exp $ */
 /*  $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $   */
 
 /*
@@ -53,7 +53,7 @@
 
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.12 2021/06/05 01:38:22 nonaka Exp $ ");
+__KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.13 2021/06/06 08:45:18 nonaka Exp $ ");
 
 #include 
 #include 
@@ -76,6 +76,7 @@ __KERNEL_RCSID(0, "$NetBSD: amdzentemp.c
 #define	AMD_CURTMP_RANGE_ADJUST	4900	/* in microKelvins (ie, 49C) */
 #define	AMD_CURTMP_RANGE_CHECK	__BIT(19)
 #define	F10_TEMP_CURTMP		__BITS(31,21)	/* XXX same as amdtemp.c */
+#define	F10_TEMP_CURTMP_MASK	0x7ff
 #define	F15M60_CURTMP_TJSEL	__BITS(17,16)
 
 /*
@@ -96,7 +97,17 @@ __KERNEL_RCSID(0, "$NetBSD: amdzentemp.c
  */
 #define	AMD_17H_CUR_TMP			0x59800
 
+/*
+ * The following register set was discovered experimentally by Ondrej Čerman
+ * and collaborators, but is not (yet) documented in a PPR/OSRR (other than
+ * the M70H PPR SMN memory map showing [0x59800, +0x314] as allocated to
+ * SMU::THM).  It seems plausible and the Linux sensor folks have adopted it.
+ */
+#define	AMD_17H_CCD_TMP_BASE		0x59954
+#define	AMD_17H_CCD_TMP_VALID		__BIT(11)
+
 struct amdzentemp_softc {
+	device_t sc_dev;
 	struct sysmon_envsys *sc_sme;
 	device_t sc_smn;
 	envsys_data_t *sc_sensor;
@@ -105,15 +116,33 @@ struct amdzentemp_softc {
 	int32_t sc_offset;
 };
 
+enum {
+	NOSENSOR = 0,
+	CORE0_SENSOR0,
+	CCD_BASE,
+	CCD0 = CCD_BASE,
+	CCD1,
+	CCD2,
+	CCD3,
+	CCD4,
+	CCD5,
+	CCD6,
+	CCD7,
+	CCD_MAX,
+	NUM_CCDS = CCD_MAX - CCD_BASE
+};
+
 
 static int  amdzentemp_match(device_t, cfdata_t, void *);
 static void amdzentemp_attach(device_t, device_t, void *);
 static int  amdzentemp_detach(device_t, int);
 
-static void amdzentemp_init(struct amdzentemp_softc *);
-static void amdzentemp_setup_sensors(struct amdzentemp_softc *, int);
+static void amdzentemp_init(struct amdzentemp_softc *, int, int);
+static void amdzentemp_setup_sensors(struct amdzentemp_softc *);
 static void amdzentemp_family15_refresh(struct sysmon_envsys *, envsys_data_t *);
 static void amdzentemp_family17_refresh(struct sysmon_envsys *, envsys_data_t *);
+static int  amdzentemp_probe_ccd_sensors(struct amdzentemp_softc *, int, int);
+static void amdzentemp_setup_ccd_sensors(struct amdzentemp_softc *);
 
 CFATTACH_DECL_NEW(amdzentemp, sizeof(struct amdzentemp_softc),
 amdzentemp_match, amdzentemp_attach, amdzentemp_detach, NULL);
@@ -124,9 +153,9 @@ amdzentemp_match(device_t parent, cfdata
 	struct pci_attach_args *pa __diagused = aux;
 
 	KASSERT(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD);
- 
+
 	cfdata_t parent_cfdata = device_cfdata(parent);
- 
+
 	/* Got AMD family 17h system management network */
 	return parent_cfdata->cf_name &&
 	memcmp(parent_cfdata->cf_name, "amdsmn", 6) == 0;
@@ -137,18 +166,20 @@ amdzentemp_attach(device_t parent, devic
 {
 	struct amdzentemp_softc *sc = device_private(self);
 	struct cpu_info *ci = curcpu();
-	int family;
+	int family, model;
 	int error;
 	size_t i;
 
+	sc->sc_dev = self;
+
 	family = CPUID_TO_FAMILY(ci->ci_signature);
+	model = CPUID_TO_MODEL(ci->ci_signature);
 	aprint_naive("\n");
-	aprint_normal(": AMD CPU Temperature Sensors (Family%xh)",
-	family);
+	aprint_normal(": AMD CPU Temperature Sensors (Family%xh)", family);
 
 	sc->sc_smn = parent;
 
-	amdzentemp_init(sc);
+	amdzentemp_init(sc, family, model);
 
 	aprint_normal("\n");
 
@@ -156,12 +187,14 @@ amdzentemp_attach(device_t parent, devic
 	sc->sc_sensor_len = sizeof(envsys_data_t) * sc->sc_numsensors;
 	sc->sc_sensor = kmem_zalloc(sc->sc_sensor_len, KM_SLEEP);
 
-	amdzentemp_setup_sensors(sc, device_unit(self));
+	amdzentemp_setup_sensors(sc);
 
 	/*
 	 * Set properties in sensors.
 	 */
 	for (i = 0; i < sc->sc_numsensors; i++) {
+		if (sc->sc_sensor[i].private == NOSENSOR)
+			continue;
 		if (sysmon_envsys_sensor_attach(sc->sc_sme, >sc_sensor[i]))
 			goto bad;
 	}
@@ -223,10 +256,10 @@ amdzentemp_detach(device_t self, int fla
 
 
 static void
-amdzentemp_init(struct amdzentemp_softc *sc) 
+amdzentemp_init(struct amdzentemp_softc *sc, int family, int model)
 {
 
-	sc->sc_numsensors = 1;
+	

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

2021-06-04 Thread NONAKA Kimihiro
Module Name:src
Committed By:   nonaka
Date:   Sat Jun  5 01:38:22 UTC 2021

Modified Files:
src/sys/arch/x86/pci: amdzentemp.c

Log Message:
amdzentemp(4): Add Zen 3 support.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/x86/pci/amdzentemp.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/pci/amdzentemp.c
diff -u src/sys/arch/x86/pci/amdzentemp.c:1.11 src/sys/arch/x86/pci/amdzentemp.c:1.12
--- src/sys/arch/x86/pci/amdzentemp.c:1.11	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/pci/amdzentemp.c	Sat Jun  5 01:38:22 2021
@@ -1,4 +1,4 @@
-/*  $NetBSD: amdzentemp.c,v 1.11 2020/04/25 15:26:18 bouyer Exp $ */
+/*  $NetBSD: amdzentemp.c,v 1.12 2021/06/05 01:38:22 nonaka Exp $ */
 /*  $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $   */
 
 /*
@@ -53,7 +53,7 @@
 
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.11 2020/04/25 15:26:18 bouyer Exp $ ");
+__KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.12 2021/06/05 01:38:22 nonaka Exp $ ");
 
 #include 
 #include 
@@ -177,6 +177,7 @@ amdzentemp_attach(device_t parent, devic
 		sc->sc_sme->sme_refresh = amdzentemp_family15_refresh;
 		break;
 	case 0x17:
+	case 0x19:
 		sc->sc_sme->sme_refresh = amdzentemp_family17_refresh;
 		break;
 	default:



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

2021-06-01 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jun  1 21:29:24 UTC 2021

Modified Files:
src/sys/arch/x86/acpi: acpi_wakeup.c
src/sys/arch/x86/x86: tsc.c tsc.h

Log Message:
x86: Reset cached tsc in every lwp to 0 on suspend/resume.

This avoids spuriously warning about tsc going backwards, which is to
be expected after a suspend/resume cycle.


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/sys/arch/x86/acpi/acpi_wakeup.c
cvs rdiff -u -r1.54 -r1.55 src/sys/arch/x86/x86/tsc.c
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/x86/x86/tsc.h

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/acpi/acpi_wakeup.c
diff -u src/sys/arch/x86/acpi/acpi_wakeup.c:1.53 src/sys/arch/x86/acpi/acpi_wakeup.c:1.54
--- src/sys/arch/x86/acpi/acpi_wakeup.c:1.53	Thu May 21 21:12:30 2020
+++ src/sys/arch/x86/acpi/acpi_wakeup.c	Tue Jun  1 21:29:24 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_wakeup.c,v 1.53 2020/05/21 21:12:30 ad Exp $	*/
+/*	$NetBSD: acpi_wakeup.c,v 1.54 2021/06/01 21:29:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2011 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_wakeup.c,v 1.53 2020/05/21 21:12:30 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_wakeup.c,v 1.54 2021/06/01 21:29:24 riastradh Exp $");
 
 #include 
 #include 
@@ -341,6 +341,7 @@ acpi_md_sleep(int state)
 	 */
 
 	/* We just woke up (cpu0), execution is resumed here */
+	tsc_tc_reset();
 	cpu_init_msrs(_info_primary, false);
 	fpuinit(_info_primary);
 	if (rcr4() & CR4_OSXSAVE)

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.54 src/sys/arch/x86/x86/tsc.c:1.55
--- src/sys/arch/x86/x86/tsc.c:1.54	Fri Feb 19 02:15:58 2021
+++ src/sys/arch/x86/x86/tsc.c	Tue Jun  1 21:29:24 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.54 2021/02/19 02:15:58 christos Exp $	*/
+/*	$NetBSD: tsc.c,v 1.55 2021/06/01 21:29:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.54 2021/02/19 02:15:58 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.55 2021/06/01 21:29:24 riastradh Exp $");
 
 #include 
 #include 
@@ -440,3 +440,18 @@ tsc_get_timecount(struct timecounter *tc
 	return cpu_counter32();
 #endif
 }
+
+/*
+ * tsc has been reset; zero the cached tsc of every lwp in the system
+ * so we don't spuriously report that the tsc has gone backward.
+ * Caller must ensure all LWPs are quiescent (except the current one,
+ * obviously) and interrupts are blocked while we update this.
+ */
+void
+tsc_tc_reset(void)
+{
+	struct lwp *l;
+
+	LIST_FOREACH(l, , l_list)
+		l->l_md.md_tsc = 0;
+}

Index: src/sys/arch/x86/x86/tsc.h
diff -u src/sys/arch/x86/x86/tsc.h:1.7 src/sys/arch/x86/x86/tsc.h:1.8
--- src/sys/arch/x86/x86/tsc.h:1.7	Mon Jun 15 09:09:24 2020
+++ src/sys/arch/x86/x86/tsc.h	Tue Jun  1 21:29:24 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.h,v 1.7 2020/06/15 09:09:24 msaitoh Exp $	*/
+/*	$NetBSD: tsc.h,v 1.8 2021/06/01 21:29:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -26,6 +26,9 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#ifndef	_X86_TSC_H_
+#define	_X86_TSC_H_
+
 bool	tsc_is_invariant(void);
 void	tsc_setfunc(struct cpu_info *);
 void	tsc_tc_init(void);
@@ -34,3 +37,6 @@ void	tsc_sync_bp(struct cpu_info *);
 void	tsc_sync_drift(int64_t);
 void	tsc_user_enable(void);
 void	tsc_user_disable(void);
+void	tsc_tc_reset(void);
+
+#endif	/* _X86_TSC_H_ */



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

2021-05-29 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat May 29 09:47:28 UTC 2021

Modified Files:
src/sys/arch/x86/pci: dwiic_pci.c

Log Message:
dwiic(4): Attribute output correctly and relegate to debug-level.

Tidy up a little while here.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/pci/dwiic_pci.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/pci/dwiic_pci.c
diff -u src/sys/arch/x86/pci/dwiic_pci.c:1.4 src/sys/arch/x86/pci/dwiic_pci.c:1.5
--- src/sys/arch/x86/pci/dwiic_pci.c:1.4	Sat Apr 24 23:36:51 2021
+++ src/sys/arch/x86/pci/dwiic_pci.c	Sat May 29 09:47:28 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: dwiic_pci.c,v 1.4 2021/04/24 23:36:51 thorpej Exp $ */
+/* $NetBSD: dwiic_pci.c,v 1.5 2021/05/29 09:47:28 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dwiic_pci.c,v 1.4 2021/04/24 23:36:51 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dwiic_pci.c,v 1.5 2021/05/29 09:47:28 riastradh Exp $");
 
 #include 
 #include 
@@ -68,9 +68,8 @@ struct pci_dwiic_softc {
 static uint32_t
 lpss_read(struct pci_dwiic_softc *sc, int offset)
 {
-	u_int32_t b = bus_space_read_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
-	 offset);
-	return b;
+	return bus_space_read_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
+	offset);
 }
 
 static void
@@ -169,7 +168,7 @@ pci_dwiic_attach(device_t parent, device
 	pa->pa_bus, pa->pa_device, pa->pa_function);
 
 	if (sc->sc_acpinode) {
-		sc->sc_dwiic.sc_iba.iba_child_devices = 
+		sc->sc_dwiic.sc_iba.iba_child_devices =
 		acpi_enter_i2c_devs(NULL, sc->sc_acpinode);
 	} else {
 		aprint_verbose_dev(self, "no matching ACPI node\n");
@@ -188,13 +187,18 @@ out:
 static bool
 dwiic_pci_power(struct dwiic_softc *dwsc, bool power)
 {
-	struct pci_dwiic_softc *sc = (void *)dwsc;
-	pcireg_t pmreg;
-
-	printf("status 0x%x\n", pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_COMMAND_STATUS_REG));
-	printf("reset 0x%x\n", lpss_read(sc, LPSS_RESET));
-	printf("rlo 0x%x\n", lpss_read(sc, LPSS_REMAP_LO));
-	printf("rho 0x%x\n", lpss_read(sc, LPSS_REMAP_HI));
+	struct pci_dwiic_softc *sc = container_of(dwsc, struct pci_dwiic_softc,
+	sc_dwiic);
+	pcireg_t pmreg, csr;
+	uint32_t reset, rlo, rhi;
+
+	csr = pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_COMMAND_STATUS_REG);
+	reset = lpss_read(sc, LPSS_RESET);
+	rlo = lpss_read(sc, LPSS_REMAP_LO);
+	rhi = lpss_read(sc, LPSS_REMAP_HI);
+	aprint_debug_dev(dwsc->sc_dev,
+	"status 0x%x reset 0x%x rlo 0x%x rhi 0x%x\n",
+	csr, reset, rlo, rhi);
 
 	if (!power)
 		lpss_write(sc, LPSS_CLKGATE, LPSS_CLKGATE_CTRL_OFF);
@@ -205,7 +209,7 @@ dwiic_pci_power(struct dwiic_softc *dwsc
 		pci_conf_write(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR,
 		power ? PCI_PMCSR_STATE_D0 : PCI_PMCSR_STATE_D3);
 		DELAY(1); /* 10 milliseconds */
-		DPRINTF((" -> 0x%x\n", 
+		DPRINTF((" -> 0x%x\n",
 		pci_conf_read(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR)));
 	}
 	if (power) {



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/include

2021-04-30 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Apr 30 15:37:06 UTC 2021

Added Files:
src/sys/arch/x86/include: gdt.h

Log Message:
merge the i386 and amd64 gdt.h files.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/x86/include/gdt.h

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/include/gdt.h
diff -u /dev/null src/sys/arch/x86/include/gdt.h:1.1
--- /dev/null	Fri Apr 30 11:37:06 2021
+++ src/sys/arch/x86/include/gdt.h	Fri Apr 30 11:37:05 2021
@@ -0,0 +1,67 @@
+/*	$NetBSD: gdt.h,v 1.1 2021/04/30 15:37:05 christos Exp $	*/
+
+/*-
+ * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by John T. Kohl and Charles M. Hannum.
+ *
+ * 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.
+ */
+
+#ifndef _X86_GDT_H_
+#define _X86_GDT_H_
+
+#if !defined(_LOCORE)
+
+struct cpu_info;
+void gdt_init(void);
+void gdt_init_cpu(struct cpu_info *);
+void gdt_alloc_cpu(struct cpu_info *);
+
+#ifdef _LP64
+struct x86_64_tss;
+int tss_alloc(struct x86_64_tss *);
+#else
+struct i386tss;
+int tss_alloc(const struct i386tss *);
+#endif
+
+void tss_free(int); 
+int ldt_alloc(void *, size_t);
+void ldt_free(int);
+
+#endif /* LOCORE */
+
+
+#ifndef MAXGDTSIZ
+# define MAXGDTSIZ		65536	/* XXX: see  */
+#endif
+
+#ifndef MAX_USERLDT_SIZE
+# define MAX_USERLDT_SIZE	65536	/* XXX: see  */
+#endif
+
+#define MAX_USERLDT_SLOTS	(int)(MAX_USERLDT_SIZE / sizeof(union descriptor))
+
+#endif /* _X86_GDT_H_ */



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/include

2021-03-17 Thread NONAKA Kimihiro
Module Name:src
Committed By:   nonaka
Date:   Thu Mar 18 01:50:12 UTC 2021

Modified Files:
src/sys/arch/x86/include: intrdefs.h

Log Message:
LIR_HV priority should be lower than softint.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/x86/include/intrdefs.h

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/include/intrdefs.h
diff -u src/sys/arch/x86/include/intrdefs.h:1.24 src/sys/arch/x86/include/intrdefs.h:1.25
--- src/sys/arch/x86/include/intrdefs.h:1.24	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/include/intrdefs.h	Thu Mar 18 01:50:12 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: intrdefs.h,v 1.24 2020/04/25 15:26:18 bouyer Exp $	*/
+/*	$NetBSD: intrdefs.h,v 1.25 2021/03/18 01:50:12 nonaka Exp $	*/
 
 #ifndef _X86_INTRDEFS_H_
 #define _X86_INTRDEFS_H_
@@ -27,7 +27,6 @@
  */
 #define LIR_IPI		31
 #define LIR_TIMER	30
-#define LIR_HV		29
 
 /*
  * XXX These should be lowest numbered, but right now would
@@ -35,11 +34,12 @@
  * means that soft interrupt take priority over hardware
  * interrupts when lowering the priority level!
  */
-#define	SIR_SERIAL	28
-#define	SIR_NET		27
-#define	SIR_BIO		26
-#define	SIR_CLOCK	25
-#define	SIR_PREEMPT	24
+#define	SIR_SERIAL	29
+#define	SIR_NET		28
+#define	SIR_BIO		27
+#define	SIR_CLOCK	26
+#define	SIR_PREEMPT	25
+#define	LIR_HV		24
 #define	SIR_XENIPL_HIGH 23
 #define	SIR_XENIPL_SCHED 22
 #define	SIR_XENIPL_VM	21



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

2021-03-14 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Sun Mar 14 08:10:23 UTC 2021

Modified Files:
src/sys/arch/x86/pci: pci_msi_machdep.c

Log Message:
Remoave an extra space from a comment


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/x86/pci/pci_msi_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/pci/pci_msi_machdep.c
diff -u src/sys/arch/x86/pci/pci_msi_machdep.c:1.14 src/sys/arch/x86/pci/pci_msi_machdep.c:1.15
--- src/sys/arch/x86/pci/pci_msi_machdep.c:1.14	Sun Jul 19 14:27:07 2020
+++ src/sys/arch/x86/pci/pci_msi_machdep.c	Sun Mar 14 08:10:23 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pci_msi_machdep.c,v 1.14 2020/07/19 14:27:07 jdolecek Exp $	*/
+/*	$NetBSD: pci_msi_machdep.c,v 1.15 2021/03/14 08:10:23 skrll Exp $	*/
 
 /*
  * Copyright (c) 2015 Internet Initiative Japan Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pci_msi_machdep.c,v 1.14 2020/07/19 14:27:07 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pci_msi_machdep.c,v 1.15 2021/03/14 08:10:23 skrll Exp $");
 
 #include "opt_intrdebug.h"
 #include "ioapic.h"
@@ -509,7 +509,7 @@ x86_pci_msix_disestablish(pci_chipset_ta
 /*
  * This function is used by device drivers like pci_intr_map().
  *
- * "ihps" is the array  of vector numbers which MSI used instead of IRQ number.
+ * "ihps" is the array of vector numbers which MSI used instead of IRQ number.
  * "count" must be power of 2.
  * "count" can decrease if struct intrsource cannot be allocated.
  * if count == 0, return non-zero value.



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

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

Modified Files:
src/sys/arch/x86/include: cpu.h
src/sys/arch/x86/x86: identcpu.c

Log Message:
Identify VirtualBox as a separate guest type.


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/sys/arch/x86/include/cpu.h
cvs rdiff -u -r1.118 -r1.119 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/include/cpu.h
diff -u src/sys/arch/x86/include/cpu.h:1.129 src/sys/arch/x86/include/cpu.h:1.130
--- src/sys/arch/x86/include/cpu.h:1.129	Sat Aug  8 15:08:48 2020
+++ src/sys/arch/x86/include/cpu.h	Thu Feb 18 21:15:24 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.h,v 1.129 2020/08/08 19:08:48 christos Exp $	*/
+/*	$NetBSD: cpu.h,v 1.130 2021/02/19 02:15:24 christos Exp $	*/
 
 /*
  * Copyright (c) 1990 The Regents of the University of California.
@@ -503,6 +503,7 @@ typedef enum vm_guest {
 	VM_GUEST_HV,
 	VM_GUEST_VMWARE,
 	VM_GUEST_KVM,
+	VM_GUEST_VIRTUALBOX,
 	VM_LAST
 } vm_guest_t;
 extern vm_guest_t vm_guest;

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.118 src/sys/arch/x86/x86/identcpu.c:1.119
--- src/sys/arch/x86/x86/identcpu.c:1.118	Tue Oct 27 04:57:11 2020
+++ src/sys/arch/x86/x86/identcpu.c	Thu Feb 18 21:15:24 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.118 2020/10/27 08:57:11 ryo Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.119 2021/02/19 02:15:24 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.118 2020/10/27 08:57:11 ryo Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.119 2021/02/19 02:15:24 christos Exp $");
 
 #include "opt_xen.h"
 
@@ -1104,21 +1104,27 @@ cpu_identify(struct cpu_info *ci)
  */
 vm_guest_t vm_guest = VM_GUEST_NO;
 
-static const char * const vm_bios_vendors[] = {
-	"QEMU",/* QEMU */
-	"Plex86",			/* Plex86 */
-	"Bochs",			/* Bochs */
-	"Xen",/* Xen */
-	"BHYVE",			/* bhyve */
-	"Seabios",			/* KVM */
+struct vm_name_guest {
+	const char *name;
+	vm_guest_t guest;
 };
 
-static const char * const vm_system_products[] = {
-	"VMware Virtual Platform",	/* VMWare VM */
-	"Virtual Machine",		/* Microsoft VirtualPC */
-	"VirtualBox",			/* Sun xVM VirtualBox */
-	"Parallels Virtual Platform",	/* Parallels VM */
-	"KVM",/* KVM */
+static const struct vm_name_guest vm_bios_vendors[] = {
+	{ "QEMU", VM_GUEST_VM },			/* QEMU */
+	{ "Plex86", VM_GUEST_VM },			/* Plex86 */
+	{ "Bochs", VM_GUEST_VM },			/* Bochs */
+	{ "Xen", VM_GUEST_VM },/* Xen */
+	{ "BHYVE", VM_GUEST_VM },			/* bhyve */
+	{ "Seabios", VM_GUEST_VM },			/* KVM */
+	{ "innotek GmbH", VM_GUEST_VIRTUALBOX },	/* Oracle VirtualBox */
+};
+
+static const struct vm_name_guest vm_system_products[] = {
+	{ "VMware Virtual Platform", VM_GUEST_VM },	/* VMWare VM */
+	{ "Virtual Machine", VM_GUEST_VM },		/* Microsoft VirtualPC */
+	{ "VirtualBox", VM_GUEST_VIRTUALBOX },		/* Sun xVM VirtualBox */
+	{ "Parallels Virtual Platform", VM_GUEST_VM },	/* Parallels VM */
+	{ "KVM", VM_GUEST_VM },/* KVM */
 };
 
 void
@@ -1129,8 +1135,18 @@ 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)
 		return;
+#endif
 
 	/*
 	 * [RFC] CPUID usage for interaction between Hypervisors and Linux.
@@ -1162,7 +1178,9 @@ identify_hypervisor(void)
 			/* OpenBSD vmm:   "OpenBSDVMM58" */
 			/* NetBSD nvmm:   "___ NVMM ___" */
 		}
-		return;
+		// VirtualBox returns KVM, so keep going.
+		if (vm_guest != VM_GUEST_KVM)
+			return;
 	}
 
 	/*
@@ -1181,8 +1199,8 @@ identify_hypervisor(void)
 	p = pmf_get_platform("bios-vendor");
 	if (p != NULL) {
 		for (i = 0; i < __arraycount(vm_bios_vendors); i++) {
-			if (strcmp(p, vm_bios_vendors[i]) == 0) {
-vm_guest = VM_GUEST_VM;
+			if (strcmp(p, vm_bios_vendors[i].name) == 0) {
+vm_guest = vm_bios_vendors[i].guest;
 return;
 			}
 		}
@@ -1190,8 +1208,8 @@ identify_hypervisor(void)
 	p = pmf_get_platform("system-product");
 	if (p != NULL) {
 		for (i = 0; i < __arraycount(vm_system_products); i++) {
-			if (strcmp(p, vm_system_products[i]) == 0) {
-vm_guest = VM_GUEST_VM;
+			if (strcmp(p, vm_system_products[i].name) == 0) {
+vm_guest = vm_system_products[i].guest;
 return;
 			}
 		}



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/acpi

2021-02-04 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Thu Feb  4 23:54:48 UTC 2021

Modified Files:
src/sys/arch/x86/acpi: acpi_machdep.c

Log Message:
Call acpi_device_register() as appropriate.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/x86/acpi/acpi_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/acpi/acpi_machdep.c
diff -u src/sys/arch/x86/acpi/acpi_machdep.c:1.30 src/sys/arch/x86/acpi/acpi_machdep.c:1.31
--- src/sys/arch/x86/acpi/acpi_machdep.c:1.30	Sat May  2 16:44:35 2020
+++ src/sys/arch/x86/acpi/acpi_machdep.c	Thu Feb  4 23:54:48 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_machdep.c,v 1.30 2020/05/02 16:44:35 bouyer Exp $ */
+/* $NetBSD: acpi_machdep.c,v 1.31 2021/02/04 23:54:48 thorpej Exp $ */
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.30 2020/05/02 16:44:35 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.31 2021/02/04 23:54:48 thorpej Exp $");
 
 #include 
 #include 
@@ -603,6 +603,8 @@ device_acpi_register(device_t dev, void 
 	if (parent == NULL)
 		return;
 
+	acpi_device_register(dev, aux);
+
 	device_is_vga = device_is_a(dev, "vga") || device_is_a(dev, "genfb");
 	device_is_pci = device_is_a(parent, "pci");
 	device_is_isa = device_is_a(parent, "isa");



CVS commit: src/sys/arch/x86

2021-01-27 Thread Jared D. McNeill
Module Name:src
Committed By:   jmcneill
Date:   Thu Jan 28 01:57:31 UTC 2021

Modified Files:
src/sys/arch/x86/include: genfb_machdep.h
src/sys/arch/x86/pci: pci_machdep.c
src/sys/arch/x86/x86: genfb_machdep.c hyperv.c

Log Message:
Remove x86_genfb_mtrr_init. PATs have been available since the Pentium III
and this code has been #if notyet'd shortly after being introduced.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/include/genfb_machdep.h
cvs rdiff -u -r1.87 -r1.88 src/sys/arch/x86/pci/pci_machdep.c
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/x86/x86/genfb_machdep.c
cvs rdiff -u -r1.12 -r1.13 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/include/genfb_machdep.h
diff -u src/sys/arch/x86/include/genfb_machdep.h:1.4 src/sys/arch/x86/include/genfb_machdep.h:1.5
--- src/sys/arch/x86/include/genfb_machdep.h:1.4	Sat Nov 30 05:28:28 2019
+++ src/sys/arch/x86/include/genfb_machdep.h	Thu Jan 28 01:57:31 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: genfb_machdep.h,v 1.4 2019/11/30 05:28:28 nonaka Exp $ */
+/* $NetBSD: genfb_machdep.h,v 1.5 2021/01/28 01:57:31 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2009 Jared D. McNeill 
@@ -31,7 +31,6 @@
 
 int	x86_genfb_init(void);
 int	x86_genfb_cnattach(void);
-void	x86_genfb_mtrr_init(uint64_t, uint32_t);
 void	x86_genfb_set_console_dev(device_t);
 void	x86_genfb_ddb_trap_callback(int);
 

Index: src/sys/arch/x86/pci/pci_machdep.c
diff -u src/sys/arch/x86/pci/pci_machdep.c:1.87 src/sys/arch/x86/pci/pci_machdep.c:1.88
--- src/sys/arch/x86/pci/pci_machdep.c:1.87	Mon May  4 15:55:56 2020
+++ src/sys/arch/x86/pci/pci_machdep.c	Thu Jan 28 01:57:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pci_machdep.c,v 1.87 2020/05/04 15:55:56 jdolecek Exp $	*/
+/*	$NetBSD: pci_machdep.c,v 1.88 2021/01/28 01:57:31 jmcneill Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.87 2020/05/04 15:55:56 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.88 2021/01/28 01:57:31 jmcneill Exp $");
 
 #include 
 #include 
@@ -1018,8 +1018,6 @@ x86_genfb_setmode(struct genfb_softc *sc
 
 	switch (newmode) {
 	case WSDISPLAYIO_MODE_EMUL:
-		x86_genfb_mtrr_init(sc->sc_fboffset,
-		sc->sc_height * sc->sc_stride);
 # if NACPICA > 0 && defined(VGA_POST)
 		if (curmode != newmode) {
 			if (vga_posth != NULL && acpi_md_vesa_modenum != 0) {

Index: src/sys/arch/x86/x86/genfb_machdep.c
diff -u src/sys/arch/x86/x86/genfb_machdep.c:1.15 src/sys/arch/x86/x86/genfb_machdep.c:1.16
--- src/sys/arch/x86/x86/genfb_machdep.c:1.15	Sat Nov 30 05:28:28 2019
+++ src/sys/arch/x86/x86/genfb_machdep.c	Thu Jan 28 01:57:31 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: genfb_machdep.c,v 1.15 2019/11/30 05:28:28 nonaka Exp $ */
+/* $NetBSD: genfb_machdep.c,v 1.16 2021/01/28 01:57:31 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2009 Jared D. McNeill 
@@ -31,9 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: genfb_machdep.c,v 1.15 2019/11/30 05:28:28 nonaka Exp $");
-
-#include "opt_mtrr.h"
+__KERNEL_RCSID(0, "$NetBSD: genfb_machdep.c,v 1.16 2021/01/28 01:57:31 jmcneill Exp $");
 
 #include 
 #include 
@@ -45,7 +43,6 @@ __KERNEL_RCSID(0, "$NetBSD: genfb_machde
 
 #include 
 #include 
-#include 
 
 #include 
 #include 
@@ -99,42 +96,6 @@ x86_genfb_ddb_trap_callback(int where)
 	}
 }
 
-void
-x86_genfb_mtrr_init(uint64_t physaddr, uint32_t size)
-{
-#if notyet
-#ifdef MTRR
-	struct mtrr mtrr;
-	int error, n;
-
-	if (mtrr_funcs == NULL) {
-		aprint_debug("%s: no mtrr funcs\n", __func__);
-		return;
-	}
-
-	mtrr.base = physaddr;
-	mtrr.len = size;
-	mtrr.type = MTRR_TYPE_WC;
-	mtrr.flags = MTRR_VALID;
-	mtrr.owner = 0;
-
-	aprint_debug("%s: 0x%" PRIx64 "-0x%" PRIx64 "\n", __func__,
-	mtrr.base, mtrr.base + mtrr.len - 1);
-
-	n = 1;
-	KERNEL_LOCK(1, NULL);
-	error = mtrr_set(, , curlwp->l_proc, MTRR_GETSET_KERNEL);
-	if (n != 0)
-		mtrr_commit();
-	KERNEL_UNLOCK_ONE(NULL);
-
-	aprint_debug("%s: mtrr_set returned %d\n", __func__, error);
-#else
-	aprint_debug("%s: kernel lacks MTRR option\n", __func__);
-#endif
-#endif
-}
-
 int
 x86_genfb_init(void)
 {

Index: src/sys/arch/x86/x86/hyperv.c
diff -u src/sys/arch/x86/x86/hyperv.c:1.12 src/sys/arch/x86/x86/hyperv.c:1.13
--- src/sys/arch/x86/x86/hyperv.c:1.12	Mon Oct 12 12:11:03 2020
+++ src/sys/arch/x86/x86/hyperv.c	Thu Jan 28 01:57:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: hyperv.c,v 1.12 2020/10/12 12:11:03 ryoon Exp $	*/
+/*	$NetBSD: hyperv.c,v 1.13 2021/01/28 01:57:31 jmcneill 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.12 2020/10/12 12:11:03 ryoon Exp $");
+__KERNEL_RCSID(0, "$NetBSD: hyperv.c,v 1.13 2021/01/28 01:57:31 jmcneill Exp $");
 #endif
 #ifdef __FBSDID
 

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

2021-01-21 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Thu Jan 21 09:50:37 UTC 2021

Modified Files:
src/sys/arch/x86/include: bus_defs.h

Log Message:
PRIx (etc) definitions should not include the %

Will fix anything this ends up breaking later.


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

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/include/bus_defs.h
diff -u src/sys/arch/x86/include/bus_defs.h:1.5 src/sys/arch/x86/include/bus_defs.h:1.6
--- src/sys/arch/x86/include/bus_defs.h:1.5	Thu Nov 14 16:23:52 2019
+++ src/sys/arch/x86/include/bus_defs.h	Thu Jan 21 09:50:37 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_defs.h,v 1.5 2019/11/14 16:23:52 maxv Exp $	*/
+/*	$NetBSD: bus_defs.h,v 1.6 2021/01/21 09:50:37 kre Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -86,8 +86,8 @@ typedef paddr_t bus_addr_t;
 typedef size_t bus_size_t;
 
 #define PRIxBUSADDR	PRIxPADDR
-#define PRIxBUSSIZE	"%zx"
-#define PRIuBUSSIZE	"%zu"
+#define PRIxBUSSIZE	"zx"
+#define PRIuBUSSIZE	"zu"
 
 struct bus_space_tag;
 typedef	struct bus_space_tag *bus_space_tag_t;



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/pci

2020-12-11 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Dec 11 09:22:20 UTC 2020

Modified Files:
src/sys/arch/x86/pci: msipic.c

Log Message:
Fix build failure when XNEPV is defined.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/x86/pci/msipic.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/pci/msipic.c
diff -u src/sys/arch/x86/pci/msipic.c:1.24 src/sys/arch/x86/pci/msipic.c:1.25
--- src/sys/arch/x86/pci/msipic.c:1.24	Fri Dec 11 07:49:39 2020
+++ src/sys/arch/x86/pci/msipic.c	Fri Dec 11 09:22:20 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: msipic.c,v 1.24 2020/12/11 07:49:39 knakahara Exp $	*/
+/*	$NetBSD: msipic.c,v 1.25 2020/12/11 09:22:20 knakahara Exp $	*/
 
 /*
  * Copyright (c) 2015 Internet Initiative Japan Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: msipic.c,v 1.24 2020/12/11 07:49:39 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: msipic.c,v 1.25 2020/12/11 09:22:20 knakahara Exp $");
 
 #include "opt_intrdebug.h"
 
@@ -548,8 +548,8 @@ msix_addroute(struct pic *pic, struct cp
 	pcitag_t tag;
 	bus_space_tag_t bstag;
 	bus_space_handle_t bshandle;
-	uint64_t entry_base;
 #ifndef XENPV
+	uint64_t entry_base;
 	pcireg_t addr, data;
 #endif
 	pcireg_t ctl;
@@ -574,9 +574,9 @@ msix_addroute(struct pic *pic, struct cp
 
 	bstag = pic->pic_msipic->mp_bstag;
 	bshandle = pic->pic_msipic->mp_bshandle;
+#ifndef XENPV
 	entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
 
-#ifndef XENPV
 	/*
 	 * See Intel 64 and IA-32 Architectures Software Developer's Manual
 	 * Volume 3 10.11 Message Signalled Interrupts.



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

2020-12-10 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Dec 11 07:49:40 UTC 2020

Modified Files:
src/sys/arch/x86/pci: msipic.c

Log Message:
Not pic->pic_addroute but pic->pic_hwunmask should enable interrupts for MSI-X.

pic->pic_addroute should not enable interrupt, because callers expect
interrupts have been disabled until they call pic->pic_hwunmask.

By the way, the old implement writes zero to Vector Control for MSI-X Table
Entries, howerver it must be read and updated.  Because, there are not only
Mask Bit but also ST lower and ST upper.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/arch/x86/pci/msipic.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/pci/msipic.c
diff -u src/sys/arch/x86/pci/msipic.c:1.23 src/sys/arch/x86/pci/msipic.c:1.24
--- src/sys/arch/x86/pci/msipic.c:1.23	Mon May  4 15:55:56 2020
+++ src/sys/arch/x86/pci/msipic.c	Fri Dec 11 07:49:39 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: msipic.c,v 1.23 2020/05/04 15:55:56 jdolecek Exp $	*/
+/*	$NetBSD: msipic.c,v 1.24 2020/12/11 07:49:39 knakahara Exp $	*/
 
 /*
  * Copyright (c) 2015 Internet Initiative Japan Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: msipic.c,v 1.23 2020/05/04 15:55:56 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: msipic.c,v 1.24 2020/12/11 07:49:39 knakahara Exp $");
 
 #include "opt_intrdebug.h"
 
@@ -598,8 +598,6 @@ msix_addroute(struct pic *pic, struct cp
 	bus_space_write_4(bstag, bshandle,
 	entry_base + PCI_MSIX_TABLE_ENTRY_DATA, data);
 #endif /* !XENPV */
-	bus_space_write_4(bstag, bshandle,
-	entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
 	BUS_SPACE_WRITE_FLUSH(bstag, bshandle);
 
 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);



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/include

2020-11-23 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Tue Nov 24 00:46:28 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Add some definitions from the latest Intel SDM:

 - Add CPUID leaf 7 %edx bit 23 "KL" (Key Locker).
 - Add CPUID leaf 7 subleaf 1 %eax bit 5 "AVX512_BF16".


To generate a diff of this commit:
cvs rdiff -u -r1.175 -r1.176 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.175 src/sys/arch/x86/include/specialreg.h:1.176
--- src/sys/arch/x86/include/specialreg.h:1.175	Mon Sep  7 13:19:20 2020
+++ src/sys/arch/x86/include/specialreg.h	Tue Nov 24 00:46:28 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.175 2020/09/07 13:19:20 jakllsch Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.176 2020/11/24 00:46:28 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -395,9 +395,12 @@
  *	%ebx: Feature bits.
  *	%ecx: Feature bits.
  *	%edx: Feature bits.
+ *
+ * %ecx == 1: Structure Extendede Feature Enumeration Sub-leaf
+ *	%eax: See below.
  */
 
-/* %ebx */
+/* %ecx = 0, %ebx */
 #define CPUID_SEF_FSGSBASE	__BIT(0)  /* {RD,WR}{FS,GS}BASE */
 #define CPUID_SEF_TSC_ADJUST	__BIT(1)  /* IA32_TSC_ADJUST MSR support */
 #define CPUID_SEF_SGX		__BIT(2)  /* Software Guard Extensions */
@@ -441,7 +444,7 @@
 	"\31" "CLWB"	"\32" "PT"	"\33" "AVX512PF" "\34" "AVX512ER" \
 	"\35" "AVX512CD""\36" "SHA"	"\37" "AVX512BW" "\40" "AVX512VL"
 
-/* %ecx */
+/* %ecx = 0, %ecx */
 #define CPUID_SEF_PREFETCHWT1	__BIT(0)  /* PREFETCHWT1 instruction */
 #define CPUID_SEF_AVX512_VBMI	__BIT(1)  /* AVX-512 Vector Byte Manipulation */
 #define CPUID_SEF_UMIP		__BIT(2)  /* User-Mode Instruction prevention */
@@ -459,6 +462,7 @@
 #define CPUID_SEF_LA57		__BIT(16) /* 57bit linear addr & 5LVL paging */
 #define CPUID_SEF_MAWAU		__BITS(21, 17) /* MAWAU for BND{LD,ST}X */
 #define CPUID_SEF_RDPID		__BIT(22) /* RDPID and IA32_TSC_AUX */
+#define CPUID_SEF_KL		__BIT(23) /* Key Locker */
 #define CPUID_SEF_CLDEMOTE	__BIT(25) /* Cache line demote */
 #define CPUID_SEF_MOVDIRI	__BIT(27) /* MOVDIRI instruction */
 #define CPUID_SEF_MOVDIR64B	__BIT(28) /* MOVDIR64B instruction */
@@ -471,12 +475,11 @@
 	"b\10GFNI\0"	"b\11VAES\0"	"b\12VPCLMULQDQ\0" "b\13AVX512_VNNI\0"\
 	"b\14AVX512_BITALG\0"		"b\16AVX512_VPOPCNTDQ\0"	\
 	"b\20LA57\0"			\
-	"f\21\5MAWAU\0"			\
-	"b\26RDPID\0"			\
+	"f\21\5MAWAU\0"			"b\26RDPID\0"	"b\27KL\0"	\
 			"b\31CLDEMOTE\0"		"b\33MOVDIRI\0"	\
 	"b\34MOVDIR64B\0"		"b\36SGXLC\0"	"b\37PKS\0"
 
-/* %edx */
+/* %ecx = 0, %edx */
 #define CPUID_SEF_AVX512_4VNNIW	__BIT(2)
 #define CPUID_SEF_AVX512_4FMAPS	__BIT(3)
 #define CPUID_SEF_FSREP_MOV	__BIT(4)  /* Fast Short REP MOV */
@@ -505,6 +508,10 @@
 	"\33" "IBRS"	"\34" "STIBP"	\
 	"\35" "L1D_FLUSH" "\36" "ARCH_CAP" "\37CORE_CAP"	"\40" "SSBD"
 
+/* %ecx = 1, %eax */
+#define CPUID_SEF_AVX512_BF16	__BIT(5)
+#define CPUID_SEF1_FLAGS_A	"\20" \
+"\6" "AVX512_BF16"
 /*
  * Intel CPUID Architectural Performance Monitoring Fn000a
  *



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/include

2020-11-15 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Sun Nov 15 13:59:42 UTC 2020

Modified Files:
src/sys/arch/x86/include: machdep.h

Log Message:
remove unused x86_cpu_initclock_func()


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/x86/include/machdep.h

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/include/machdep.h
diff -u src/sys/arch/x86/include/machdep.h:1.10 src/sys/arch/x86/include/machdep.h:1.11
--- src/sys/arch/x86/include/machdep.h:1.10	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/include/machdep.h	Sun Nov 15 13:59:42 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: machdep.h,v 1.10 2020/04/25 15:26:18 bouyer Exp $ */
+/* $NetBSD: machdep.h,v 1.11 2020/11/15 13:59:42 bouyer Exp $ */
 /*
  * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -53,7 +53,6 @@ void	x86_cpu_idle_set(void (*)(void), co
 
 extern u_long x86_rtclock_tval;
 extern void (*x86_initclock_func)(void);
-extern void (*x86_cpu_initclock_func)(void);
 
 void x86_dummy_initclock(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: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/include

2020-09-07 Thread Jonathan A. Kollasch
Module Name:src
Committed By:   jakllsch
Date:   Mon Sep  7 13:19:20 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Fix printb string for LA57


To generate a diff of this commit:
cvs rdiff -u -r1.174 -r1.175 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.174 src/sys/arch/x86/include/specialreg.h:1.175
--- src/sys/arch/x86/include/specialreg.h:1.174	Mon Sep  7 03:03:09 2020
+++ src/sys/arch/x86/include/specialreg.h	Mon Sep  7 13:19:20 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.174 2020/09/07 03:03:09 msaitoh Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.175 2020/09/07 13:19:20 jakllsch Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -470,7 +470,7 @@
 	"b\4OSPKE\0"	"b\5WAITPKG\0"	"b\6AVX512_VBMI2\0" "b\7CET_SS\0" \
 	"b\10GFNI\0"	"b\11VAES\0"	"b\12VPCLMULQDQ\0" "b\13AVX512_VNNI\0"\
 	"b\14AVX512_BITALG\0"		"b\16AVX512_VPOPCNTDQ\0"	\
-	"b\20\LA57\0"			\
+	"b\20LA57\0"			\
 	"f\21\5MAWAU\0"			\
 	"b\26RDPID\0"			\
 			"b\31CLDEMOTE\0"		"b\33MOVDIRI\0"	\



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

2020-09-06 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Mon Sep  7 03:03:09 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
 Add CPUID(EAX=07H, ECX=0) ECX bit 16 LA57 from maxv.


To generate a diff of this commit:
cvs rdiff -u -r1.173 -r1.174 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.173 src/sys/arch/x86/include/specialreg.h:1.174
--- src/sys/arch/x86/include/specialreg.h:1.173	Sat Sep  5 07:45:44 2020
+++ src/sys/arch/x86/include/specialreg.h	Mon Sep  7 03:03:09 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.173 2020/09/05 07:45:44 maxv Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.174 2020/09/07 03:03:09 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -456,6 +456,7 @@
 #define CPUID_SEF_AVX512_VNNI	__BIT(11) /* Vector neural Network Instruction */
 #define CPUID_SEF_AVX512_BITALG	__BIT(12)
 #define CPUID_SEF_AVX512_VPOPCNTDQ __BIT(14)
+#define CPUID_SEF_LA57		__BIT(16) /* 57bit linear addr & 5LVL paging */
 #define CPUID_SEF_MAWAU		__BITS(21, 17) /* MAWAU for BND{LD,ST}X */
 #define CPUID_SEF_RDPID		__BIT(22) /* RDPID and IA32_TSC_AUX */
 #define CPUID_SEF_CLDEMOTE	__BIT(25) /* Cache line demote */
@@ -469,6 +470,7 @@
 	"b\4OSPKE\0"	"b\5WAITPKG\0"	"b\6AVX512_VBMI2\0" "b\7CET_SS\0" \
 	"b\10GFNI\0"	"b\11VAES\0"	"b\12VPCLMULQDQ\0" "b\13AVX512_VNNI\0"\
 	"b\14AVX512_BITALG\0"		"b\16AVX512_VPOPCNTDQ\0"	\
+	"b\20\LA57\0"			\
 	"f\21\5MAWAU\0"			\
 	"b\26RDPID\0"			\
 			"b\31CLDEMOTE\0"		"b\33MOVDIRI\0"	\



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

2020-09-04 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Sep  4 17:05:09 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Add a few more CPUID flags.


To generate a diff of this commit:
cvs rdiff -u -r1.171 -r1.172 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.171 src/sys/arch/x86/include/specialreg.h:1.172
--- src/sys/arch/x86/include/specialreg.h:1.171	Wed Aug  5 15:40:46 2020
+++ src/sys/arch/x86/include/specialreg.h	Fri Sep  4 17:05:09 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.171 2020/08/05 15:40:46 maxv Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.172 2020/09/04 17:05:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -759,6 +759,11 @@
 #define CPUID_CAPEX_VIRT_SSBD	__BIT(25)	/* Virt Spec Control SSBD */
 #define CPUID_CAPEX_SSB_NO	__BIT(26)	/* SSBD not required */
 
+/* %ecx */
+#define CPUID_CAPEX_PerfTscSize	__BITS(17,16)
+#define CPUID_CAPEX_ApicIdSize	__BITS(15,12)
+#define CPUID_CAPEX_NC		__BITS(7,0)
+
 #define CPUID_CAPEX_FLAGS	"\20"	 \
 	"\1CLZERO"	"\2IRPERF"	"\3XSAVEERPTR"			 \
 	"\5RDPRU"			"\7B6" \
@@ -767,6 +772,9 @@
 	"\21IBRS_ALWAYSON" "\22STIBP_ALWAYSON" "\23PREFER_IBRS"	"\24B19" \
 	"\31SSBD"	"\32VIRT_SSBD"	"\33SSB_NO"
 
+/* AMD Fn8000_000a %eax (SVM Revision) */
+#define CPUID_AMD_SVM_REV		__BITS(7,0)
+
 /* AMD Fn8000_000a %edx features (SVM features) */
 #define CPUID_AMD_SVM_NP		0x0001
 #define CPUID_AMD_SVM_LbrVirt		0x0002



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/include

2020-08-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Aug  5 15:40:46 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Add new fields here and there.


To generate a diff of this commit:
cvs rdiff -u -r1.170 -r1.171 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.170 src/sys/arch/x86/include/specialreg.h:1.171
--- src/sys/arch/x86/include/specialreg.h:1.170	Mon Jul 20 05:50:55 2020
+++ src/sys/arch/x86/include/specialreg.h	Wed Aug  5 15:40:46 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.170 2020/07/20 05:50:55 maxv Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.171 2020/08/05 15:40:46 maxv Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -109,6 +109,7 @@
 #define CR4_OSFXSR	0x0200 /* enable fxsave/fxrestor and SSE */
 #define CR4_OSXMMEXCPT	0x0400 /* enable unmasked SSE exceptions */
 #define CR4_UMIP	0x0800 /* user-mode instruction prevention */
+#define CR4_LA57	0x1000 /* 57-bit linear addresses */
 #define CR4_VMXE	0x2000 /* enable VMX operations */
 #define CR4_SMXE	0x4000 /* enable SMX operations */
 #define CR4_FSGSBASE	0x0001 /* enable *FSBASE and *GSBASE instructions */
@@ -116,7 +117,9 @@
 #define CR4_OSXSAVE	0x0004 /* enable xsave and xrestore */
 #define CR4_SMEP	0x0010 /* enable SMEP support */
 #define CR4_SMAP	0x0020 /* enable SMAP support */
-#define CR4_PKE		0x0040 /* protection key enable */
+#define CR4_PKE		0x0040 /* enable Protection Keys for user pages */
+#define CR4_CET		0x0080 /* enable CET */
+#define CR4_PKS		0x0100 /* enable Protection Keys for kern pages */
 
 /*
  * Extended Control Register XCR0
@@ -131,13 +134,17 @@
 #define XCR0_Hi16_ZMM	0x0080	/* AVX-512 512 bits upper registers */
 #define XCR0_PT		0x0100	/* Processor Trace state */
 #define XCR0_PKRU	0x0200	/* Protection Key state */
+#define XCR0_CET_U	0x0800	/* User CET state */
+#define XCR0_CET_S	0x1000	/* Kern CET state */
 #define XCR0_HDC	0x2000	/* Hardware Duty Cycle state */
+#define XCR0_HWP	0x0001	/* Hardware P-states */
 
 #define XCR0_FLAGS1	"\20" \
 	"\1" "x87"		"\2" "SSE"		"\3" "AVX"	\
 	"\4" "BNDREGS"		"\5" "BNDCSR"		"\6" "Opmask"	\
 	"\7" "ZMM_Hi256"	"\10" "Hi16_ZMM"	"\11" "PT"	\
-	"\12" "PKRU"		"\16" "HDC"
+	"\12" "PKRU"		"\14" "CET_U"		"\15" "CET_S"	\
+	"\16" "HDC"		"\21" "HWP"
 
 /*
  * Known FPU bits, only these get enabled. The save area is sized for all the
@@ -147,7 +154,7 @@
 			 XCR0_Opmask | XCR0_ZMM_Hi256 | XCR0_Hi16_ZMM)
 
 /*
- * XSAVE component indices.
+ * XSAVE component indices, internal to NetBSD.
  */
 #define XSAVE_X87	0
 #define XSAVE_SSE	1
@@ -157,9 +164,6 @@
 #define XSAVE_Opmask	5
 #define XSAVE_ZMM_Hi256	6
 #define XSAVE_Hi16_ZMM	7
-#define XSAVE_PT	8
-#define XSAVE_PKRU	9
-#define XSAVE_HDC	10
 
 /*
  * Highest XSAVE component enabled by XCR0_FPU.
@@ -448,7 +452,7 @@
 #define CPUID_SEF_OSPKE		__BIT(4)  /* OS has set CR4.PKE to ena. protec. keys */
 #define CPUID_SEF_WAITPKG	__BIT(5)  /* TPAUSE,UMONITOR,UMWAIT */
 #define CPUID_SEF_AVX512_VBMI2	__BIT(6)  /* AVX-512 Vector Byte Manipulation 2 */
-#define CPUID_SEF_CET_SS	__BIT(7)  /* CET shadow stack */
+#define CPUID_SEF_CET_SS	__BIT(7)  /* CET Shadow Stack */
 #define CPUID_SEF_GFNI		__BIT(8)
 #define CPUID_SEF_VAES		__BIT(9)
 #define CPUID_SEF_VPCLMULQDQ	__BIT(10)
@@ -461,7 +465,7 @@
 #define CPUID_SEF_MOVDIRI	__BIT(27) /* MOVDIRI instruction */
 #define CPUID_SEF_MOVDIR64B	__BIT(28) /* MOVDIR64B instruction */
 #define CPUID_SEF_SGXLC		__BIT(30) /* SGX Launch Configuration */
-#define CPUID_SEF_PKS		__BIT(31) /* Protection Keys */
+#define CPUID_SEF_PKS		__BIT(31) /* Protection Keys for Kern-mode pages */
 
 #define CPUID_SEF_FLAGS1	"\177\20" \
 	"b\0PREFETCHWT1\0" "b\1AVX512_VBMI\0" "b\2UMIP\0" "b\3PKU\0"	\
@@ -481,7 +485,7 @@
 #define CPUID_SEF_SRBDS_CTRL	__BIT(9)  /* IA32_MCU_OPT_CTRL */
 #define CPUID_SEF_MD_CLEAR	__BIT(10)
 #define CPUID_SEF_TSX_FORCE_ABORT __BIT(13) /* MSR_TSX_FORCE_ABORT bit 0 */
-#define CPUID_SEF_SERIALIZE	__BIT(14)
+#define CPUID_SEF_SERIALIZE	__BIT(14) /* SERIALIZE instruction */
 #define CPUID_SEF_HYBRID	__BIT(15) /* Hybrid part */
 #define CPUID_SEF_TSXLDTRK	__BIT(16) /* TSX suspend load addr tracking */
 #define CPUID_SEF_CET_IBT	__BIT(20) /* CET Indirect Branch Tracking */



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

2020-08-01 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Sat Aug  1 12:36:36 UTC 2020

Modified Files:
src/sys/arch/x86/pci: pci_intr_machdep.c
src/sys/arch/x86/x86: mainbus.c

Log Message:
reorder includes to pull __HAVE_PCI_MSI_MSIX properly via



To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/sys/arch/x86/pci/pci_intr_machdep.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/x86/mainbus.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/pci/pci_intr_machdep.c
diff -u src/sys/arch/x86/pci/pci_intr_machdep.c:1.50 src/sys/arch/x86/pci/pci_intr_machdep.c:1.51
--- src/sys/arch/x86/pci/pci_intr_machdep.c:1.50	Mon Jun 17 06:38:29 2019
+++ src/sys/arch/x86/pci/pci_intr_machdep.c	Sat Aug  1 12:36:35 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pci_intr_machdep.c,v 1.50 2019/06/17 06:38:29 msaitoh Exp $	*/
+/*	$NetBSD: pci_intr_machdep.c,v 1.51 2020/08/01 12:36:35 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2009 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pci_intr_machdep.c,v 1.50 2019/06/17 06:38:29 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pci_intr_machdep.c,v 1.51 2020/08/01 12:36:35 jdolecek Exp $");
 
 #include 
 #include 
@@ -85,13 +85,14 @@ __KERNEL_RCSID(0, "$NetBSD: pci_intr_mac
 #include 
 #include 
 
-#include 
-
 #include "ioapic.h"
 #include "eisa.h"
 #include "acpica.h"
 #include "opt_mpbios.h"
 #include "opt_acpi.h"
+#include "opt_pci.h"
+
+#include 
 
 #include 
 

Index: src/sys/arch/x86/x86/mainbus.c
diff -u src/sys/arch/x86/x86/mainbus.c:1.4 src/sys/arch/x86/x86/mainbus.c:1.5
--- src/sys/arch/x86/x86/mainbus.c:1.4	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/mainbus.c	Sat Aug  1 12:36:35 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: mainbus.c,v 1.4 2020/04/25 15:26:18 bouyer Exp $ */
+/* $NetBSD: mainbus.c,v 1.5 2020/08/01 12:36:35 jdolecek Exp $ */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -28,15 +28,18 @@
 
 #include 
 
-__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.4 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.5 2020/08/01 12:36:35 jdolecek Exp $");
+
+#include "opt_acpi.h"
+#include "opt_mpbios.h"
+#include "opt_pcifixup.h"
+#include "opt_pci.h"
 
 #include 
 #include 
 #include 
 #include 
 
-#include 
-
 #include 
 #include 
 #include 
@@ -48,10 +51,6 @@ __KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 
 #include "acpica.h"
 #include "ipmi.h"
 
-#include "opt_acpi.h"
-#include "opt_mpbios.h"
-#include "opt_pcifixup.h"
-
 #if NACPICA > 0
 #include 
 #endif
@@ -63,6 +62,7 @@ __KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 
 #endif
 
 #if NPCI > 0
+#include 
 #if defined(PCI_BUS_FIXUP)
 #include 
 #if defined(PCI_ADDR_FIXUP)



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

2020-08-01 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Sat Aug  1 12:15:40 UTC 2020

Modified Files:
src/sys/arch/x86/conf: files.x86

Log Message:
defflag NO_PCI_MSI_MSIX


To generate a diff of this commit:
cvs rdiff -u -r1.118 -r1.119 src/sys/arch/x86/conf/files.x86

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/conf/files.x86
diff -u src/sys/arch/x86/conf/files.x86:1.118 src/sys/arch/x86/conf/files.x86:1.119
--- src/sys/arch/x86/conf/files.x86:1.118	Sat Jul 25 22:49:20 2020
+++ src/sys/arch/x86/conf/files.x86	Sat Aug  1 12:15:40 2020
@@ -1,4 +1,4 @@
-#	$NetBSD: files.x86,v 1.118 2020/07/25 22:49:20 riastradh Exp $
+#	$NetBSD: files.x86,v 1.119 2020/08/01 12:15:40 jdolecek Exp $
 
 # options for MP configuration through the MP spec
 defflag opt_mpbios.h MPBIOS MPDEBUG MPBIOS_SCANPCI
@@ -13,6 +13,7 @@ defflag opt_intrdebug.h	INTRDEBUG
 # PCI fixup options
 defflag opt_pcifixup.h	PCI_ADDR_FIXUP PCI_BUS_FIXUP
 			PCI_INTR_FIXUP PCI_INTR_FIXUP_FORCE
+defflag opt_pci.h	NO_PCI_MSI_MSIX
 
 # To be able to test for NetBSD/xen in shared files
 defflag	opt_xen.h		DO_NOT_DEFINE



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/include

2020-07-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Jul 20 05:50:56 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Revert previous, to unbreak the build (NVMM declares the macro too).

There are hundreds of MSRs, we're not going to list them all, especially
when the majority are unused.


To generate a diff of this commit:
cvs rdiff -u -r1.169 -r1.170 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.169 src/sys/arch/x86/include/specialreg.h:1.170
--- src/sys/arch/x86/include/specialreg.h:1.169	Sun Jul 19 16:17:00 2020
+++ src/sys/arch/x86/include/specialreg.h	Mon Jul 20 05:50:55 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.169 2020/07/19 16:17:00 jdolecek Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.170 2020/07/20 05:50:55 maxv Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -865,9 +865,6 @@
 #define 	APICBASE_PHYSADDR	0xf000	/* physical address */
 #define MSR_EBL_CR_POWERON	0x02a
 #define MSR_EBC_FREQUENCY_ID	0x02c	/* PIV only */
-#define MSR_IA32_FEATURE_CONTROL	0x03a
-#define 	IA32_FEATURE_CONTROL_ENABLE_VMXON_INSIDE_SMX	0x0002
-#define 	IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX	0x0004
 #define MSR_IA32_SPEC_CTRL	0x048
 #define 	IA32_SPEC_CTRL_IBRS	0x01
 #define 	IA32_SPEC_CTRL_STIBP	0x02



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

2020-07-19 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Sun Jul 19 16:17:00 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
add definition for MSR_IA32_FEATURE_CONTROL, just for information


To generate a diff of this commit:
cvs rdiff -u -r1.168 -r1.169 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.168 src/sys/arch/x86/include/specialreg.h:1.169
--- src/sys/arch/x86/include/specialreg.h:1.168	Thu Jun 18 16:27:24 2020
+++ src/sys/arch/x86/include/specialreg.h	Sun Jul 19 16:17:00 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.168 2020/06/18 16:27:24 maxv Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.169 2020/07/19 16:17:00 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
@@ -865,6 +865,9 @@
 #define 	APICBASE_PHYSADDR	0xf000	/* physical address */
 #define MSR_EBL_CR_POWERON	0x02a
 #define MSR_EBC_FREQUENCY_ID	0x02c	/* PIV only */
+#define MSR_IA32_FEATURE_CONTROL	0x03a
+#define 	IA32_FEATURE_CONTROL_ENABLE_VMXON_INSIDE_SMX	0x0002
+#define 	IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX	0x0004
 #define MSR_IA32_SPEC_CTRL	0x048
 #define 	IA32_SPEC_CTRL_IBRS	0x01
 #define 	IA32_SPEC_CTRL_STIBP	0x02



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

2020-06-29 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jun 29 23:38:02 UTC 2020

Modified Files:
src/sys/arch/x86/conf: files.x86
src/sys/arch/x86/include: via_padlock.h
src/sys/arch/x86/x86: via_padlock.c

Log Message:
padlock(4): Remove legacy rijndael API use.

This doesn't actually need to compute AES -- it just needs the
standard AES key schedule, so use the BearSSL constant-time key
schedule implementation.

XXX Compile-tested only.
XXX The byte-order business here seems highly questionable.


To generate a diff of this commit:
cvs rdiff -u -r1.112 -r1.113 src/sys/arch/x86/conf/files.x86
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/x86/include/via_padlock.h
cvs rdiff -u -r1.29 -r1.30 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/conf/files.x86
diff -u src/sys/arch/x86/conf/files.x86:1.112 src/sys/arch/x86/conf/files.x86:1.113
--- src/sys/arch/x86/conf/files.x86:1.112	Mon Jun 29 23:29:39 2020
+++ src/sys/arch/x86/conf/files.x86	Mon Jun 29 23:38:02 2020
@@ -1,4 +1,4 @@
-#	$NetBSD: files.x86,v 1.112 2020/06/29 23:29:39 riastradh Exp $
+#	$NetBSD: files.x86,v 1.113 2020/06/29 23:38:02 riastradh Exp $
 
 # options for MP configuration through the MP spec
 defflag opt_mpbios.h MPBIOS MPDEBUG MPBIOS_SCANPCI
@@ -59,7 +59,7 @@ device	odcm
 attach	odcm at cpufeaturebus
 file	arch/x86/x86/odcm.c		odcm
 
-device	padlock: opencrypto, rijndael
+device	padlock: opencrypto, aes
 attach	padlock at cpufeaturebus
 file	arch/x86/x86/via_padlock.c	padlock
 

Index: src/sys/arch/x86/include/via_padlock.h
diff -u src/sys/arch/x86/include/via_padlock.h:1.9 src/sys/arch/x86/include/via_padlock.h:1.10
--- src/sys/arch/x86/include/via_padlock.h:1.9	Sat Feb 27 00:54:59 2016
+++ src/sys/arch/x86/include/via_padlock.h	Mon Jun 29 23:38:02 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: via_padlock.h,v 1.9 2016/02/27 00:54:59 tls Exp $	*/
+/*	$NetBSD: via_padlock.h,v 1.10 2020/06/29 23:38:02 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2003 Jason Wright
@@ -25,7 +25,8 @@
 
 #include 
 #include 
-#include 
+
+#include 
 
 /* VIA C3 xcrypt-* instruction context control options */
 #define C3_CRYPT_CWLO_ROUND_M		0x000f
@@ -43,9 +44,8 @@
 #define C3_CRYPT_CWLO_KEY256		0x080e  /* 256bit, 15 rds */
 
 struct via_padlock_session {
-uint32_t	ses_ekey[4 * (RIJNDAEL_MAXNR + 1) + 4];	/* 128 bit aligned */
-uint32_t	ses_dkey[4 * (RIJNDAEL_MAXNR + 1) + 4];	/* 128 bit aligned */
-uint8_t	ses_iv[16];/* 128 bit aligned */
+uint32_t	ses_ekey[4*(AES_256_NROUNDS + 1)];
+uint32_t	ses_dkey[4*(AES_256_NROUNDS + 1)];
 uint32_t	ses_cw0;
 struct swcr_data	*swd;
 int	ses_klen;

Index: src/sys/arch/x86/x86/via_padlock.c
diff -u src/sys/arch/x86/x86/via_padlock.c:1.29 src/sys/arch/x86/x86/via_padlock.c:1.30
--- src/sys/arch/x86/x86/via_padlock.c:1.29	Sun Jun 14 23:20:15 2020
+++ src/sys/arch/x86/x86/via_padlock.c	Mon Jun 29 23:38:02 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.29 2020/06/14 23:20:15 riastradh Exp $ */
+/*	$NetBSD: via_padlock.c,v 1.30 2020/06/29 23:38:02 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2003 Jason Wright
@@ -20,7 +20,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.29 2020/06/14 23:20:15 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.30 2020/06/29 23:38:02 riastradh Exp $");
 
 #include 
 #include 
@@ -37,10 +37,11 @@ __KERNEL_RCSID(0, "$NetBSD: via_padlock.
 #include 
 #include 
 
+#include 
+
 #include 
 #include 
 #include 
-#include 
 
 #include 
 
@@ -174,14 +175,29 @@ via_padlock_crypto_newsession(void *arg,
 	for (c = cri; c != NULL; c = c->cri_next) {
 		switch (c->cri_alg) {
 		case CRYPTO_AES_CBC:
+			memset(ses->ses_ekey, 0, sizeof(ses->ses_ekey));
+			memset(ses->ses_dkey, 0, sizeof(ses->ses_dkey));
+
 			switch (c->cri_klen) {
 			case 128:
+br_aes_ct_keysched_stdenc(ses->ses_ekey,
+c->cri_key, 16);
+br_aes_ct_keysched_stddec(ses->ses_dkey,
+c->cri_key, 16);
 cw0 = C3_CRYPT_CWLO_KEY128;
 break;
 			case 192:
+br_aes_ct_keysched_stdenc(ses->ses_ekey,
+c->cri_key, 24);
+br_aes_ct_keysched_stddec(ses->ses_dkey,
+c->cri_key, 24);
 cw0 = C3_CRYPT_CWLO_KEY192;
 break;
 			case 256:
+br_aes_ct_keysched_stdenc(ses->ses_ekey,
+c->cri_key, 32);
+br_aes_ct_keysched_stddec(ses->ses_dkey,
+c->cri_key, 32);
 cw0 = C3_CRYPT_CWLO_KEY256;
 break;
 			default:
@@ -194,16 +210,11 @@ via_padlock_crypto_newsession(void *arg,
 			ses->ses_klen = c->cri_klen;
 			ses->ses_cw0 = cw0;
 
-			/* Build expanded keys for both directions */
-			rijndaelKeySetupEnc(ses->ses_ekey, c->cri_key,
-			c->cri_klen);
-			rijndaelKeySetupDec(ses->ses_dkey, c->cri_key,
-			c->cri_klen);
-			for (i = 0; i < 4 * 

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

2020-06-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Jun 19 16:20:22 UTC 2020

Modified Files:
src/sys/arch/x86/include: cpu.h sysarch.h
src/sys/arch/x86/x86: sys_machdep.c

Log Message:
localify


To generate a diff of this commit:
cvs rdiff -u -r1.125 -r1.126 src/sys/arch/x86/include/cpu.h
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/x86/include/sysarch.h
cvs rdiff -u -r1.55 -r1.56 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/include/cpu.h
diff -u src/sys/arch/x86/include/cpu.h:1.125 src/sys/arch/x86/include/cpu.h:1.126
--- src/sys/arch/x86/include/cpu.h:1.125	Sat May  2 16:44:35 2020
+++ src/sys/arch/x86/include/cpu.h	Fri Jun 19 16:20:22 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.h,v 1.125 2020/05/02 16:44:35 bouyer Exp $	*/
+/*	$NetBSD: cpu.h,v 1.126 2020/06/19 16:20:22 maxv Exp $	*/
 
 /*
  * Copyright (c) 1990 The Regents of the University of California.
@@ -565,12 +565,6 @@ void	cpu_probe_features(struct cpu_info 
 void	cpu_proc_fork(struct proc *, struct proc *);
 paddr_t	kvtop(void *);
 
-#ifdef USER_LDT
-/* sys_machdep.h */
-int	x86_get_ldt(struct lwp *, void *, register_t *);
-int	x86_set_ldt(struct lwp *, void *, register_t *);
-#endif
-
 /* isa_machdep.c */
 void	isa_defaultirq(void);
 int	isa_nmi(void);

Index: src/sys/arch/x86/include/sysarch.h
diff -u src/sys/arch/x86/include/sysarch.h:1.14 src/sys/arch/x86/include/sysarch.h:1.15
--- src/sys/arch/x86/include/sysarch.h:1.14	Fri Jul 13 09:37:32 2018
+++ src/sys/arch/x86/include/sysarch.h	Fri Jun 19 16:20:22 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: sysarch.h,v 1.14 2018/07/13 09:37:32 maxv Exp $	*/
+/*	$NetBSD: sysarch.h,v 1.15 2020/06/19 16:20:22 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -125,11 +125,7 @@ struct mtrr;
 
 #ifdef _KERNEL
 int x86_iopl(struct lwp *, void *, register_t *);
-int x86_get_mtrr(struct lwp *, void *, register_t *);
-int x86_set_mtrr(struct lwp *, void *, register_t *);
-int x86_get_ldt(struct lwp *, void *, register_t *);
 int x86_get_ldt1(struct lwp *, struct x86_get_ldt_args *, union descriptor *);
-int x86_set_ldt(struct lwp *, void *, register_t *);
 int x86_set_ldt1(struct lwp *, struct x86_set_ldt_args *, union descriptor *);
 int x86_set_sdbase(void *, char, lwp_t *, bool);
 int x86_get_sdbase(void *, char);

Index: src/sys/arch/x86/x86/sys_machdep.c
diff -u src/sys/arch/x86/x86/sys_machdep.c:1.55 src/sys/arch/x86/x86/sys_machdep.c:1.56
--- src/sys/arch/x86/x86/sys_machdep.c:1.55	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/x86/x86/sys_machdep.c	Fri Jun 19 16:20:22 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_machdep.c,v 1.55 2020/04/25 15:26:18 bouyer Exp $	*/
+/*	$NetBSD: sys_machdep.c,v 1.56 2020/06/19 16:20:22 maxv 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.55 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.56 2020/06/19 16:20:22 maxv Exp $");
 
 #include "opt_mtrr.h"
 #include "opt_user_ldt.h"
@@ -75,16 +75,15 @@ __KERNEL_RCSID(0, "$NetBSD: sys_machdep.
 
 extern struct vm_map *kernel_map;
 
-int x86_get_ioperm(struct lwp *, void *, register_t *);
-int x86_set_ioperm(struct lwp *, void *, register_t *);
-int x86_get_mtrr(struct lwp *, void *, register_t *);
-int x86_set_mtrr(struct lwp *, void *, register_t *);
-int x86_set_sdbase32(void *, char, lwp_t *, bool);
+static int x86_get_ioperm(struct lwp *, void *, register_t *);
+static int x86_set_ioperm(struct lwp *, void *, register_t *);
+static int x86_set_sdbase32(void *, char, lwp_t *, bool);
 int x86_set_sdbase(void *, char, lwp_t *, bool);
-int x86_get_sdbase32(void *, char);
+static int x86_get_sdbase32(void *, char);
 int x86_get_sdbase(void *, char);
 
-int
+#ifdef i386
+static int
 x86_get_ldt(struct lwp *l, void *args, register_t *retval)
 {
 #ifndef USER_LDT
@@ -113,6 +112,7 @@ x86_get_ldt(struct lwp *l, void *args, r
 	return error;
 #endif
 }
+#endif
 
 int
 x86_get_ldt1(struct lwp *l, struct x86_get_ldt_args *ua, union descriptor *cp)
@@ -175,7 +175,8 @@ x86_get_ldt1(struct lwp *l, struct x86_g
 #endif
 }
 
-int
+#ifdef i386
+static int
 x86_set_ldt(struct lwp *l, void *args, register_t *retval)
 {
 #ifndef USER_LDT
@@ -201,6 +202,7 @@ x86_set_ldt(struct lwp *l, void *args, r
 	return error;
 #endif
 }
+#endif
 
 int
 x86_set_ldt1(struct lwp *l, struct x86_set_ldt_args *ua,
@@ -388,7 +390,7 @@ x86_iopl(struct lwp *l, void *args, regi
 	return 0;
 }
 
-int
+static int
 x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
 {
 #ifdef IOPERM
@@ -421,7 +423,7 @@ x86_get_ioperm(struct lwp *l, void *args
 #endif
 }
 
-int
+static int
 x86_set_ioperm(struct lwp *l, void *args, register_t *retval)
 {
 #ifdef IOPERM
@@ -467,7 +469,7 @@ x86_set_ioperm(struct lwp *l, void *args
 #endif
 }
 
-int
+static int
 

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

2020-06-18 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Jun 18 16:27:24 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
style and fix typo


To generate a diff of this commit:
cvs rdiff -u -r1.167 -r1.168 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.167 src/sys/arch/x86/include/specialreg.h:1.168
--- src/sys/arch/x86/include/specialreg.h:1.167	Wed Jun 10 03:39:03 2020
+++ src/sys/arch/x86/include/specialreg.h	Thu Jun 18 16:27:24 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: specialreg.h,v 1.167 2020/06/10 03:39:03 msaitoh Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.168 2020/06/18 16:27:24 maxv Exp $	*/
 
 /*
- * Copyright (c) 2014-2019 The NetBSD Foundation, Inc.
+ * Copyright (c) 2014-2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -294,10 +294,10 @@
 		? 0 : (CPUID_TO_EXTMODEL(cpuid) << 4)))
 
 /* CPUID Fn0001 %ebx */
-#define	CPUID_BRAND_INDEX	__BITS(7,0)
-#define	CPUID_CLFLUSH_SIZE	__BITS(15,8)
-#define	CPUID_HTT_CORES		__BITS(23,16)
-#define	CPUID_LOCAL_APIC_ID	__BITS(31,24)
+#define CPUID_BRAND_INDEX	__BITS(7,0)
+#define CPUID_CLFLUSH_SIZE	__BITS(15,8)
+#define CPUID_HTT_CORES		__BITS(23,16)
+#define CPUID_LOCAL_APIC_ID	__BITS(31,24)
 
 /*
  * Intel Deterministic Cache Parameter Leaf
@@ -389,7 +389,7 @@
 
 /*
  * Intel/AMD Structured Extended Feature leaf Fn_0007
- * %eax == 0: Subleaf 0
+ * %ecx == 0: Subleaf 0
  *	%eax: The Maximum input value for supported subleaf.
  *	%ebx: Feature bits.
  *	%ecx: Feature bits.



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

2020-06-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jun 14 16:12:05 UTC 2020

Modified Files:
src/sys/arch/x86/include: cpufunc.h fpu.h
src/sys/arch/x86/x86: fpu.c

Log Message:
Use static constant rather than stack memset buffer for zero fpregs.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/sys/arch/x86/include/cpufunc.h
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/x86/include/fpu.h
cvs rdiff -u -r1.64 -r1.65 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/include/cpufunc.h
diff -u src/sys/arch/x86/include/cpufunc.h:1.39 src/sys/arch/x86/include/cpufunc.h:1.40
--- src/sys/arch/x86/include/cpufunc.h:1.39	Sat May  2 11:37:17 2020
+++ src/sys/arch/x86/include/cpufunc.h	Sun Jun 14 16:12:05 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpufunc.h,v 1.39 2020/05/02 11:37:17 maxv Exp $	*/
+/*	$NetBSD: cpufunc.h,v 1.40 2020/06/14 16:12:05 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2007, 2019 The NetBSD Foundation, Inc.
@@ -381,7 +381,7 @@ fnsave(void *addr)
 }
 
 static inline void
-frstor(void *addr)
+frstor(const void *addr)
 {
 	const uint8_t *area = addr;
 
@@ -407,7 +407,7 @@ fxsave(void *addr)
 }
 
 static inline void
-fxrstor(void *addr)
+fxrstor(const void *addr)
 {
 	const uint8_t *area = addr;
 
@@ -452,7 +452,7 @@ xsaveopt(void *addr, uint64_t mask)
 }
 
 static inline void
-xrstor(void *addr, uint64_t mask)
+xrstor(const void *addr, uint64_t mask)
 {
 	const uint8_t *area = addr;
 	uint32_t low, high;

Index: src/sys/arch/x86/include/fpu.h
diff -u src/sys/arch/x86/include/fpu.h:1.20 src/sys/arch/x86/include/fpu.h:1.21
--- src/sys/arch/x86/include/fpu.h:1.20	Wed Nov 27 06:24:33 2019
+++ src/sys/arch/x86/include/fpu.h	Sun Jun 14 16:12:05 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.h,v 1.20 2019/11/27 06:24:33 maxv Exp $	*/
+/*	$NetBSD: fpu.h,v 1.21 2020/06/14 16:12:05 riastradh Exp $	*/
 
 #ifndef	_X86_FPU_H_
 #define	_X86_FPU_H_
@@ -15,7 +15,7 @@ void fpuinit(struct cpu_info *);
 void fpuinit_mxcsr_mask(void);
 
 void fpu_area_save(void *, uint64_t);
-void fpu_area_restore(void *, uint64_t);
+void fpu_area_restore(const void *, uint64_t);
 
 void fpu_save(void);
 

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.64 src/sys/arch/x86/x86/fpu.c:1.65
--- src/sys/arch/x86/x86/fpu.c:1.64	Sat Jun 13 19:01:11 2020
+++ src/sys/arch/x86/x86/fpu.c	Sun Jun 14 16:12:05 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.64 2020/06/13 19:01:11 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.65 2020/06/14 16:12:05 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.64 2020/06/13 19:01:11 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.65 2020/06/14 16:12:05 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -267,7 +267,7 @@ fpu_area_save(void *area, uint64_t xsave
 }
 
 void
-fpu_area_restore(void *area, uint64_t xsave_features)
+fpu_area_restore(const void *area, uint64_t xsave_features)
 {
 	clts();
 
@@ -392,7 +392,7 @@ fpu_kern_enter(void)
 void
 fpu_kern_leave(void)
 {
-	union savefpu zero_fpu __aligned(64);
+	static const union savefpu zero_fpu __aligned(64);
 	struct cpu_info *ci = curcpu();
 	int s;
 
@@ -404,7 +404,6 @@ fpu_kern_leave(void)
 	 * 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);
 
 	/*



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/include

2020-06-09 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Wed Jun 10 03:39:03 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
 Add SRBDS_CTRL bit.


To generate a diff of this commit:
cvs rdiff -u -r1.166 -r1.167 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.166 src/sys/arch/x86/include/specialreg.h:1.167
--- src/sys/arch/x86/include/specialreg.h:1.166	Mon Jun  1 08:32:39 2020
+++ src/sys/arch/x86/include/specialreg.h	Wed Jun 10 03:39:03 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.166 2020/06/01 08:32:39 msaitoh Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.167 2020/06/10 03:39:03 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2014-2019 The NetBSD Foundation, Inc.
@@ -478,6 +478,7 @@
 #define CPUID_SEF_AVX512_4FMAPS	__BIT(3)
 #define CPUID_SEF_FSREP_MOV	__BIT(4)  /* Fast Short REP MOV */
 #define CPUID_SEF_AVX512_VP2INTERSECT __BIT(8)
+#define CPUID_SEF_SRBDS_CTRL	__BIT(9)  /* IA32_MCU_OPT_CTRL */
 #define CPUID_SEF_MD_CLEAR	__BIT(10)
 #define CPUID_SEF_TSX_FORCE_ABORT __BIT(13) /* MSR_TSX_FORCE_ABORT bit 0 */
 #define CPUID_SEF_SERIALIZE	__BIT(14)
@@ -494,7 +495,7 @@
 #define CPUID_SEF_FLAGS2	"\20" \
 "\3" "AVX512_4VNNIW" "\4" "AVX512_4FMAPS" \
 	"\5" "FSREP_MOV"		\
-	"\11" "VP2INTERSECT"	"\13" "MD_CLEAR"			\
+	"\11VP2INTERSECT" "\12SRBDS_CTRL" "\13MD_CLEAR"			\
 			"\16TSX_FORCE_ABORT" "\17SERIALIZE" "\20HYBRID"	\
 	"\21" "TSXLDTRK"		\
 	"\25" "CET_IBT"			\



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/include

2020-06-01 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Mon Jun  1 08:32:39 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Add some definitions from the latest Intel SDM plus small fix:

 - Add CPUID leaf 6 %eax bit 19 for HW_FEEDBACK* and IA32_PACKAGE_TERM* MSRs.
 - Add CPUID leaf 7 %ecx bit 31 for Protection Keys.
 - Add definition of Load only TLB and Store only TLB.
 - Add IF_PSCHANGE_MC_NO bit of IA32_ARCH_CAPABILITIES
 - Fix HWP_IGNIDL.


To generate a diff of this commit:
cvs rdiff -u -r1.165 -r1.166 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.165 src/sys/arch/x86/include/specialreg.h:1.166
--- src/sys/arch/x86/include/specialreg.h:1.165	Thu May 28 07:59:38 2020
+++ src/sys/arch/x86/include/specialreg.h	Mon Jun  1 08:32:39 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.165 2020/05/28 07:59:38 msaitoh Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.166 2020/06/01 08:32:39 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2014-2019 The NetBSD Foundation, Inc.
@@ -367,6 +367,7 @@
 #define CPUID_DSPM_HWP_PECI   __BIT(16)	/* HWP PECI override */
 #define CPUID_DSPM_HWP_FLEX   __BIT(17)	/* Flexible HWP */
 #define CPUID_DSPM_HWP_FAST   __BIT(18)	/* Fast access for IA32_HWP_REQUEST */
+#define CPUID_DSPM_HW_FEEDBACK __BIT(19) /* HW_FEEDBACK*, IA32_PACKAGE_TERM* */
 #define CPUID_DSPM_HWP_IGNIDL __BIT(20)	/* Ignore Idle Logical Processor HWP */
 
 #define CPUID_DSPM_FLAGS	"\20" \
@@ -374,8 +375,8 @@
 	"\5" "PLN"	"\6" "ECMD"	"\7" "PTM"	"\10" "HWP"	\
 	"\11" "HWP_NOTIFY" "\12" "HWP_ACTWIN" "\13" "HWP_EPP" "\14" "HWP_PLR" \
 			"\16" "HDC"	"\17" "TBM3"	"\20" "HWP_CAP" \
-	"\21" "HWP_PECI" "\22" "HWP_FLEX" "\23" "HWP_FAST"		\
-	"25" "HWP_IGNIDL"
+	"\21" "HWP_PECI" "\22" "HWP_FLEX" "\23" "HWP_FAST" "\24HW_FEEDBACK"   \
+	"\25" "HWP_IGNIDL"
 
 /*
  * Intel/AMD Digital Thermal Sensor and
@@ -460,6 +461,7 @@
 #define CPUID_SEF_MOVDIRI	__BIT(27) /* MOVDIRI instruction */
 #define CPUID_SEF_MOVDIR64B	__BIT(28) /* MOVDIR64B instruction */
 #define CPUID_SEF_SGXLC		__BIT(30) /* SGX Launch Configuration */
+#define CPUID_SEF_PKS		__BIT(31) /* Protection Keys */
 
 #define CPUID_SEF_FLAGS1	"\177\20" \
 	"b\0PREFETCHWT1\0" "b\1AVX512_VBMI\0" "b\2UMIP\0" "b\3PKU\0"	\
@@ -469,7 +471,7 @@
 	"f\21\5MAWAU\0"			\
 	"b\26RDPID\0"			\
 			"b\31CLDEMOTE\0"		"b\33MOVDIRI\0"	\
-	"b\34MOVDIR64B\0"		"b\36SGXLC\0"
+	"b\34MOVDIR64B\0"		"b\36SGXLC\0"	"b\37PKS\0"
 
 /* %edx */
 #define CPUID_SEF_AVX512_4VNNIW	__BIT(2)
@@ -608,6 +610,8 @@
 #define CPUID_DATP_TCTYPE_D	1		/*   Data TLB */
 #define CPUID_DATP_TCTYPE_I	2		/*   Instruction TLB */
 #define CPUID_DATP_TCTYPE_U	3		/*   Unified TLB */
+#define CPUID_DATP_TCTYPE_L	4		/*   Load only TLB */
+#define CPUID_DATP_TCTYPE_S	5		/*   Store only TLB */
 #define CPUID_DATP_TCLEVEL	__BITS(7, 5)	/* TLB level (start at 1) */
 #define CPUID_DATP_FULLASSOC	__BIT(8)	/* Full associative */
 #define CPUID_DATP_SHAREING	__BITS(25, 14)	/* shareing */
@@ -882,6 +886,7 @@
 #define 	IA32_ARCH_SKIP_L1DFL_VMENTRY 0x08
 #define 	IA32_ARCH_SSB_NO	0x10
 #define 	IA32_ARCH_MDS_NO	0x20
+#define 	IA32_ARCH_IF_PSCHANGE_MC_NO 0x40
 #define 	IA32_ARCH_TSX_CTRL	0x80
 #define 	IA32_ARCH_TAA_NO	0x100
 #define MSR_IA32_FLUSH_CMD	0x10b



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/include

2020-05-28 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Thu May 28 07:59:38 UTC 2020

Modified Files:
src/sys/arch/x86/include: specialreg.h

Log Message:
Add AMD MSR_DE_CFG's bit 1 as DE_CFG_LFENCE_SERIALIZE.
This bit makes lfence instruction serializing.


To generate a diff of this commit:
cvs rdiff -u -r1.164 -r1.165 src/sys/arch/x86/include/specialreg.h

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/include/specialreg.h
diff -u src/sys/arch/x86/include/specialreg.h:1.164 src/sys/arch/x86/include/specialreg.h:1.165
--- src/sys/arch/x86/include/specialreg.h:1.164	Fri May  1 04:07:24 2020
+++ src/sys/arch/x86/include/specialreg.h	Thu May 28 07:59:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: specialreg.h,v 1.164 2020/05/01 04:07:24 msaitoh Exp $	*/
+/*	$NetBSD: specialreg.h,v 1.165 2020/05/28 07:59:38 msaitoh Exp $	*/
 
 /*
  * Copyright (c) 2014-2019 The NetBSD Foundation, Inc.
@@ -1121,6 +1121,7 @@
 
 #define MSR_DE_CFG	0xc0011029
 #define 	DE_CFG_ERRATA_721	0x0001
+#define 	DE_CFG_LFENCE_SERIALIZE	__BIT(1)
 #define 	DE_CFG_ERRATA_1021	__BIT(13)
 
 #define MSR_BU_CFG2	0xc001102a



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);



  1   2   3   4   5   6   7   8   9   10   >