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

2020-03-19 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Thu Mar 19 18:58:14 UTC 2020

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

Log Message:
PR port-amd64/55083 (assertion "pmap->pm_stats.resident_count == PDP_SIZE" 
failed)

Reported-by: syzbot+2c1e17352173a60ee...@syzkaller.appspotmail.com

Don't screw up resident_count in failure path.


To generate a diff of this commit:
cvs rdiff -u -r1.377 -r1.378 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.377 src/sys/arch/x86/x86/pmap.c:1.378
--- src/sys/arch/x86/x86/pmap.c:1.377	Wed Mar 18 18:54:28 2020
+++ src/sys/arch/x86/x86/pmap.c	Thu Mar 19 18:58:14 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.377 2020/03/18 18:54:28 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.378 2020/03/19 18:58:14 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.377 2020/03/18 18:54:28 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.378 2020/03/19 18:58:14 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -2229,11 +2229,11 @@ pmap_freepage(struct pmap *pmap, struct 
 {
 	int lidx;
 
-	KASSERT(ptp->wire_count == 1);
+	KASSERT(ptp->wire_count <= 1);
 	PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
 
 	lidx = level - 1;
-	pmap_stats_update(pmap, -1, 0);
+	pmap_stats_update(pmap, -ptp->wire_count, 0);
 	if (pmap->pm_ptphint[lidx] == ptp)
 		pmap->pm_ptphint[lidx] = NULL;
 	ptp->wire_count = 0;
@@ -2450,7 +2450,6 @@ pmap_unget_ptp(struct pmap *pmap, struct
 			continue;
 		}
 		KASSERT(pt->pg[i]->wire_count == 0);
-		pt->pg[i]->wire_count = 1;
 		PMAP_CHECK_PP(VM_PAGE_TO_PP(pt->pg[i]));
 		pmap_freepage(pmap, pt->pg[i], i - 1);
 	}



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

2020-03-18 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed Mar 18 18:54:28 UTC 2020

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

Log Message:
Pacify assertion in a failure path.

Reported-by: syzbot+e666891e2bc5caee1...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.376 -r1.377 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.376 src/sys/arch/x86/x86/pmap.c:1.377
--- src/sys/arch/x86/x86/pmap.c:1.376	Tue Mar 17 22:38:14 2020
+++ src/sys/arch/x86/x86/pmap.c	Wed Mar 18 18:54:28 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.376 2020/03/17 22:38:14 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.377 2020/03/18 18:54:28 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.376 2020/03/17 22:38:14 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.377 2020/03/18 18:54:28 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -2450,6 +2450,7 @@ pmap_unget_ptp(struct pmap *pmap, struct
 			continue;
 		}
 		KASSERT(pt->pg[i]->wire_count == 0);
+		pt->pg[i]->wire_count = 1;
 		PMAP_CHECK_PP(VM_PAGE_TO_PP(pt->pg[i]));
 		pmap_freepage(pmap, pt->pg[i], i - 1);
 	}



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

2020-03-17 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue Mar 17 22:38:14 UTC 2020

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

Log Message:
- Change some expensive checks DEBUG -> DIAGNOSTIC.
- Mark some small functions inline.
- Add an assertion.


To generate a diff of this commit:
cvs rdiff -u -r1.375 -r1.376 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.375 src/sys/arch/x86/x86/pmap.c:1.376
--- src/sys/arch/x86/x86/pmap.c:1.375	Tue Mar 17 22:37:05 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Mar 17 22:38:14 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.375 2020/03/17 22:37:05 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.376 2020/03/17 22:38:14 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.375 2020/03/17 22:37:05 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.376 2020/03/17 22:38:14 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -509,7 +509,7 @@ pmap_stats_update_bypte(struct pmap *pma
 /*
  * ptp_to_pmap: lookup pmap by ptp
  */
-static struct pmap *
+static inline struct pmap *
 ptp_to_pmap(struct vm_page *ptp)
 {
 	struct pmap *pmap;
@@ -556,7 +556,7 @@ pv_pte_embedded(struct pmap_page *pp)
 /*
  * pv_pte_first, pv_pte_next: PV list iterator.
  */
-static struct pv_pte *
+static inline struct pv_pte *
 pv_pte_first(struct pmap_page *pp)
 {
 
@@ -567,7 +567,7 @@ pv_pte_first(struct pmap_page *pp)
 	return pve_to_pvpte(LIST_FIRST(>pp_pvlist));
 }
 
-static struct pv_pte *
+static inline struct pv_pte *
 pv_pte_next(struct pmap_page *pp, struct pv_pte *pvpte)
 {
 
@@ -589,7 +589,7 @@ pmap_is_curpmap(struct pmap *pmap)
 	return ((pmap == pmap_kernel()) || (pmap == curcpu()->ci_pmap));
 }
 
-void
+inline void
 pmap_reference(struct pmap *pmap)
 {
 
@@ -1947,7 +1947,7 @@ static void
 pmap_check_pv(struct pmap *pmap, struct vm_page *ptp, struct pmap_page *pp,
 vaddr_t va, bool tracked)
 {
-#ifdef DIAGNOSTIC /* XXX too slow make this DEBUG before April 2020 */
+#ifdef DEBUG
 	struct pv_pte *pvpte;
 
 	PMAP_CHECK_PP(pp);
@@ -4157,6 +4157,7 @@ pmap_pp_remove(struct pmap_page *pp, pad
 		 */
 		ptp = pvpte->pte_ptp;
 		pmap = ptp_to_pmap(ptp);
+		KASSERT(pmap->pm_obj[0].uo_refs > 0);
 		if (ptp != NULL) {
 			pmap_reference(pmap);
 		}
@@ -4188,7 +4189,7 @@ pmap_pp_remove(struct pmap_page *pp, pad
 		KASSERTMSG(ptp == NULL || ptp->wire_count > 1,
 		"va %lx pmap %p ptp %p is empty", va, pmap, ptp);
 		
-#ifdef DIAGNOSTIC /* XXX Too expensive make DEBUG before April 2020 */
+#ifdef DEBUG
 		pmap_check_pv(pmap, ptp, pp, pvpte->pte_va, true);
 		rb_tree_t *tree = (ptp != NULL ?
 		_PAGE_TO_PP(ptp)->pp_rb : _kernel_rb);



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

2020-03-17 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue Mar 17 22:37:05 UTC 2020

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

Log Message:
- pmap_enter(): under low memory conditions, if PTP allocation succeeded and
  then PV entry allocation failed, PTP pages were being freed without their
  struct pmap_page being reset back to the non-PTP setup, which then caused
  havoc with pmap_page_removed().  Fix it.

- pmap_enter_pv(): don't do the PV check if memory allocation failed.

Reported-by: syzbot+d9b42238107c155ca...@syzkaller.appspotmail.com
Reported-by: syzbot+80cf4850dc1cf2990...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.374 -r1.375 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.374 src/sys/arch/x86/x86/pmap.c:1.375
--- src/sys/arch/x86/x86/pmap.c:1.374	Tue Mar 17 22:29:19 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Mar 17 22:37:05 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.374 2020/03/17 22:29:19 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.375 2020/03/17 22:37:05 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.374 2020/03/17 22:29:19 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.375 2020/03/17 22:37:05 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -635,30 +635,6 @@ pmap_compare_key(void *context, const vo
 }
 
 /*
- * pmap_ptp_init: initialize new page table page
- */
-static inline void
-pmap_ptp_init(struct vm_page *ptp)
-{
-
-	ptp->uanon = (struct vm_anon *)(vaddr_t)~0L;
-	rb_tree_init(_PAGE_TO_PP(ptp)->pp_rb, _rbtree_ops);
-	PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
-}
-
-/*
- * pmap_ptp_fini: finalize a page table page
- */
-static inline void
-pmap_ptp_fini(struct vm_page *ptp)
-{
-
-	KASSERT(RB_TREE_MIN(_PAGE_TO_PP(ptp)->pp_rb) == NULL);
-	PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
-	ptp->uanon = NULL;
-}
-
-/*
  * pmap_ptp_range_set: abuse ptp->uanon to record minimum VA of PTE
  */
 static inline void
@@ -2158,7 +2134,9 @@ pmap_enter_pv(struct pmap *pmap, struct 
 		LIST_INSERT_HEAD(>pp_pvlist, pve, pve_list);
 	}
 	mutex_spin_exit(>pp_lock);
-	pmap_check_pv(pmap, ptp, pp, va, true);
+	if (error == 0) {
+		pmap_check_pv(pmap, ptp, pp, va, true);
+	}
 
 	return error;
 }
@@ -2252,13 +2230,15 @@ pmap_freepage(struct pmap *pmap, struct 
 	int lidx;
 
 	KASSERT(ptp->wire_count == 1);
+	PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
 
 	lidx = level - 1;
 	pmap_stats_update(pmap, -1, 0);
 	if (pmap->pm_ptphint[lidx] == ptp)
 		pmap->pm_ptphint[lidx] = NULL;
 	ptp->wire_count = 0;
-	pmap_ptp_fini(ptp);
+	ptp->uanon = NULL;
+	KASSERT(RB_TREE_MIN(_PAGE_TO_PP(ptp)->pp_rb) == NULL);
 
 	/*
 	 * Enqueue the PTP to be freed by pmap_update().  We can't remove
@@ -2357,19 +2337,21 @@ pmap_get_ptp(struct pmap *pmap, struct p
 
 		if (pt->pg[i] == NULL) {
 			pt->pg[i] = uvm_pagealloc(obj, off, NULL, aflags);
-			pt->alloced[i] = true;
-			if (pt->pg[i] != NULL) {
-pmap_ptp_init(pt->pg[i]);
-			}
+			pt->alloced[i] = (pt->pg[i] != NULL);
 		} else if (pt->pg[i]->wire_count == 0) {
 			/* This page was queued to be freed; dequeue it. */
 			LIST_REMOVE(pt->pg[i], mdpage.mp_pp.pp_link);
-			pmap_ptp_init(pt->pg[i]);
+			pt->alloced[i] = true;
 		}
 		PMAP_DUMMY_UNLOCK(pmap);
 		if (pt->pg[i] == NULL) {
 			pmap_unget_ptp(pmap, pt);
 			return ENOMEM;
+		} else {
+			pt->pg[i]->uanon = (struct vm_anon *)(vaddr_t)~0L;
+			rb_tree_init(_PAGE_TO_PP(pt->pg[i])->pp_rb,
+			_rbtree_ops);
+			PMAP_CHECK_PP(VM_PAGE_TO_PP(pt->pg[i]));
 		}
 	}
 	ptp = pt->pg[2];
@@ -2464,22 +2446,12 @@ pmap_unget_ptp(struct pmap *pmap, struct
 	KASSERT(mutex_owned(>pm_lock));
 
 	for (i = PTP_LEVELS; i > 1; i--) {
-		if (pt->pg[i] == NULL) {
-			break;
-		}
 		if (!pt->alloced[i]) {
 			continue;
 		}
 		KASSERT(pt->pg[i]->wire_count == 0);
 		PMAP_CHECK_PP(VM_PAGE_TO_PP(pt->pg[i]));
-		/* pmap zeros all pages before freeing. */
-		pt->pg[i]->flags |= PG_ZERO; 
-		pmap_ptp_fini(pt->pg[i]);
-		PMAP_DUMMY_LOCK(pmap);
-		uvm_pagefree(pt->pg[i]);
-		PMAP_DUMMY_UNLOCK(pmap);
-		pt->pg[i] = NULL;
-		pmap->pm_ptphint[0] = NULL;
+		pmap_freepage(pmap, pt->pg[i], i - 1);
 	}
 }
 
@@ -5232,6 +5204,7 @@ pmap_update(struct pmap *pmap)
 		mutex_enter(>pm_lock);
 		while ((ptp = LIST_FIRST(>pm_gc_ptp)) != NULL) {
 			KASSERT(ptp->wire_count == 0);
+			KASSERT(ptp->uanon == NULL);
 			LIST_REMOVE(ptp, mdpage.mp_pp.pp_link);
 			pp = VM_PAGE_TO_PP(ptp);
 			LIST_INIT(>pp_pvlist);



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

2020-03-17 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue Mar 17 18:40:35 UTC 2020

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

Log Message:
- Add more assertions.

- Range clipping for pmap_remove(): only need to keep track of the lowest VA
  in PTP, as ptp->wire_count provides an upper bound.  D'oh.  Move set of
  range to where there is already a writeback to the PTP.

- pmap_pp_remove(): panic if pmap_sync_pv() returns an error, because it means
  something has gone very wrong.  The PTE should not change here since the
  pmap is locked.

- pmap_pp_clear_attrs(): wait for the competing V->P operation by acquiring
  and releasing the pmap's lock, rather than busy looping.

- pmap_test_attrs(): this needs to wait for any competing operations,
  otherwise it could return without all necessary updates reflected in
  pp_attrs.

- pmap_enter(): fix cut-n-paste screwup in an error path for Xen.


To generate a diff of this commit:
cvs rdiff -u -r1.371 -r1.372 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.371 src/sys/arch/x86/x86/pmap.c:1.372
--- src/sys/arch/x86/x86/pmap.c:1.371	Tue Mar 17 13:34:50 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Mar 17 18:40:35 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.371 2020/03/17 13:34:50 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.372 2020/03/17 18:40:35 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.371 2020/03/17 13:34:50 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.372 2020/03/17 18:40:35 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -640,13 +640,8 @@ pmap_compare_key(void *context, const vo
 static inline void
 pmap_ptp_init(struct vm_page *ptp)
 {
-	uint16_t *minidx, *maxidx;
 
-	minidx = (uint16_t *)>uanon;
-	maxidx = (uint16_t *)>uanon + 1;
-
-	*minidx = UINT16_MAX;
-	*maxidx = 0;
+	ptp->uanon = (struct vm_anon *)(vaddr_t)~0L;
 	rb_tree_init(_PAGE_TO_PP(ptp)->pp_rb, _rbtree_ops);
 	PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
 }
@@ -664,24 +659,15 @@ pmap_ptp_fini(struct vm_page *ptp)
 }
 
 /*
- * pmap_ptp_range_set: abuse ptp->uanon to record min/max idx of PTE
+ * pmap_ptp_range_set: abuse ptp->uanon to record minimum VA of PTE
  */
 static inline void
 pmap_ptp_range_set(struct vm_page *ptp, vaddr_t va)
 {
-	uint16_t *minidx, *maxidx;
-	u_int idx;
-
-	idx = pl1_pi(va);
-	KASSERT(idx < UINT16_MAX);
-	minidx = (uint16_t *)>uanon;
-	maxidx = (uint16_t *)>uanon + 1;
+	vaddr_t *min = (vaddr_t *)>uanon;
 
-	if (idx < *minidx) {
-		*minidx = idx;
-	}
-	if (idx > *maxidx) {
-		*maxidx = idx;
+	if (va < *min) {
+		*min = va;
 	}
 }
 
@@ -689,27 +675,18 @@ pmap_ptp_range_set(struct vm_page *ptp, 
  * pmap_ptp_range_clip: abuse ptp->uanon to clip range of PTEs to remove
  */
 static inline void
-pmap_ptp_range_clip(struct vm_page *ptp, vaddr_t *startva, vaddr_t *endva,
-pt_entry_t **pte)
+pmap_ptp_range_clip(struct vm_page *ptp, vaddr_t *startva, pt_entry_t **pte)
 {
-	vaddr_t sclip, eclip;
+	vaddr_t sclip;
 
 	if (ptp == NULL) {
 		return;
 	}
 
-	sclip = (*startva & L2_FRAME) +
-	x86_ptob(*(uint16_t *)>uanon);
-	eclip = (*startva & L2_FRAME) +
-	x86_ptob(*((uint16_t *)>uanon + 1) + 1);
-
-	KASSERT(sclip < eclip);
-
+	sclip = (vaddr_t)ptp->uanon;
 	sclip = (*startva < sclip ? sclip : *startva);
-	eclip = (*endva > eclip ? eclip : *endva);
 	*pte += (sclip - *startva) / PAGE_SIZE;
 	*startva = sclip;
-	*endva = eclip;
 }
 
 /*
@@ -2966,7 +2943,7 @@ pmap_remove_all(struct pmap *pmap)
 			blkendva, _tofree);
 
 			/* PTP should now be unused - free it. */
-			KASSERT(ptps[i]->wire_count <= 1);
+			KASSERT(ptps[i]->wire_count == 1);
 			pmap_free_ptp(pmap, ptps[i], va, ptes, pdes);
 		}
 		pmap_unmap_ptes(pmap, pmap2);
@@ -2982,6 +2959,9 @@ pmap_remove_all(struct pmap *pmap)
 
 	/* Verify that the pmap is now completely empty. */
 	pmap_check_ptps(pmap);
+	KASSERTMSG(pmap->pm_stats.resident_count == PDP_SIZE,
+	"pmap %p not empty", pmap);
+
 	return true;
 }
 
@@ -3817,7 +3797,7 @@ pmap_remove_ptes(struct pmap *pmap, stru
 	 * mappings are very often sparse, so clip the given range to the
 	 * range of PTEs that are known present in the PTP.
 	 */
-	pmap_ptp_range_clip(ptp, , , );
+	pmap_ptp_range_clip(ptp, , );
 
 	/*
 	 * note that ptpva points to the PTE that maps startva.   this may
@@ -4168,9 +4148,9 @@ pmap_pp_remove(struct pmap_page *pp, pad
 {
 	struct pv_pte *pvpte;
 	struct vm_page *ptp;
+	uintptr_t sum;
 	uint8_t oattrs;
 	bool locked;
-	int count;
 
 	/*
 	 * Do an unlocked check to see if the page has no mappings, eg when
@@ -4179,20 +4159,19 @@ pmap_pp_remove(struct pmap_page *pp, pad
 	 * out, so we don't have to worry about concurrent attempts to enter
 	 * it (otherwise the caller either doesn't care or has 

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

2020-03-17 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue Mar 17 13:34:51 UTC 2020

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

Log Message:
Add a bunch of assertions.


To generate a diff of this commit:
cvs rdiff -u -r1.370 -r1.371 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.370 src/sys/arch/x86/x86/pmap.c:1.371
--- src/sys/arch/x86/x86/pmap.c:1.370	Sun Mar 15 19:41:04 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Mar 17 13:34:50 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.370 2020/03/15 19:41:04 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.371 2020/03/17 13:34:50 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.370 2020/03/15 19:41:04 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.371 2020/03/17 13:34:50 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -139,6 +139,8 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.3
 #include "opt_svs.h"
 #include "opt_kaslr.h"
 
+#define	__MUTEX_PRIVATE	/* for assertions */
+
 #include 
 #include 
 #include 
@@ -333,6 +335,8 @@ paddr_t pmap_pa_end;   /* PA of last phy
 #endif
 
 #define	VM_PAGE_TO_PP(pg)	(&(pg)->mdpage.mp_pp)
+#define	PMAP_CHECK_PP(pp) \
+KASSERTMSG((pp)->pp_lock.mtx_ipl._ipl == IPL_VM, "bad pmap_page %p", pp)
 
 /*
  * Other data structures
@@ -644,6 +648,7 @@ pmap_ptp_init(struct vm_page *ptp)
 	*minidx = UINT16_MAX;
 	*maxidx = 0;
 	rb_tree_init(_PAGE_TO_PP(ptp)->pp_rb, _rbtree_ops);
+	PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
 }
 
 /*
@@ -654,6 +659,7 @@ pmap_ptp_fini(struct vm_page *ptp)
 {
 
 	KASSERT(RB_TREE_MIN(_PAGE_TO_PP(ptp)->pp_rb) == NULL);
+	PMAP_CHECK_PP(VM_PAGE_TO_PP(ptp));
 	ptp->uanon = NULL;
 }
 
@@ -1982,6 +1988,34 @@ pmap_free_pvs(struct pmap *pmap, struct 
 }
 
 /*
+ * pmap_check_pv: verify {VA, PTP} pair is either tracked/untracked by page
+ */
+static void
+pmap_check_pv(struct pmap *pmap, struct vm_page *ptp, struct pmap_page *pp,
+vaddr_t va, bool tracked)
+{
+#ifdef DIAGNOSTIC /* XXX too slow make this DEBUG before April 2020 */
+	struct pv_pte *pvpte;
+
+	PMAP_CHECK_PP(pp);
+
+	mutex_spin_enter(>pp_lock);
+	for (pvpte = pv_pte_first(pp); pvpte; pvpte = pv_pte_next(pp, pvpte)) {
+		if (pvpte->pte_ptp == ptp && pvpte->pte_va == va) {
+			break;
+		}
+	}
+	mutex_spin_exit(>pp_lock);
+
+	if (pvpte && !tracked) {
+		panic("pmap_check_pv: %p/%lx found on pp %p", ptp, va, pp);
+	} else if (!pvpte && tracked) {
+		panic("pmap_check_pv: %p/%lx missing on pp %p", ptp, va, pp);
+	}
+#endif
+}
+
+/*
  * pmap_treelookup_pv: search the PV tree for a dynamic entry
  *
  * => pmap must be locked
@@ -2010,6 +2044,7 @@ pmap_treelookup_pv(const struct pmap *pm
 		node = node->rb_nodes[pve->pve_pte.pte_va < va];
 	}
 }
+
 /*
  * pmap_lookup_pv: look up a non-embedded pv entry for the given pmap
  *
@@ -2083,6 +2118,7 @@ pmap_enter_pv(struct pmap *pmap, struct 
 	KASSERT(ptp_to_pmap(ptp) == pmap);
 	KASSERT(ptp == NULL || ptp->uobject != NULL);
 	KASSERT(ptp == NULL || ptp_va2o(va, 1) == ptp->offset);
+	PMAP_CHECK_PP(pp);
 
 	/*
 	 * If entering the same page and it's already tracked with an
@@ -2093,6 +2129,7 @@ pmap_enter_pv(struct pmap *pmap, struct 
 	if (atomic_load_relaxed(>pp_pte.pte_ptp) == ptp &&
 	atomic_load_relaxed(>pp_pte.pte_va) == va) {
 		*samepage = true;
+		pmap_check_pv(pmap, ptp, pp, va, true);
 		return 0;
 	}
 
@@ -2104,6 +2141,7 @@ pmap_enter_pv(struct pmap *pmap, struct 
 	*old_pve = pmap_treelookup_pv(pmap, ptp, tree, va);
 	if (*old_pve != NULL && (*old_pve)->pve_pp == pp) {
 		*samepage = true;
+		pmap_check_pv(pmap, ptp, pp, va, true);
 		return 0;
 	}
 
@@ -2116,6 +2154,7 @@ pmap_enter_pv(struct pmap *pmap, struct 
 	}
 
 	error = 0;
+	pmap_check_pv(pmap, ptp, pp, va, false);
 	mutex_spin_enter(>pp_lock);
 	if (!pv_pte_embedded(pp)) {
 		/*
@@ -2142,6 +2181,7 @@ pmap_enter_pv(struct pmap *pmap, struct 
 		LIST_INSERT_HEAD(>pp_pvlist, pve, pve_list);
 	}
 	mutex_spin_exit(>pp_lock);
+	pmap_check_pv(pmap, ptp, pp, va, true);
 
 	return error;
 }
@@ -2157,7 +2197,8 @@ static void
 pmap_remove_pv(struct pmap *pmap, struct pmap_page *pp, struct vm_page *ptp,
 vaddr_t va, struct pv_entry *pve, uint8_t oattrs)
 {
-	rb_tree_t *tree;
+	rb_tree_t *tree = (ptp != NULL ?
+	_PAGE_TO_PP(ptp)->pp_rb : _kernel_rb);
 
 	KASSERT(mutex_owned(>pm_lock));
 	KASSERT(ptp_to_pmap(ptp) == pmap);
@@ -2165,6 +2206,8 @@ pmap_remove_pv(struct pmap *pmap, struct
 	KASSERT(ptp == NULL || ptp_va2o(va, 1) == ptp->offset);
 	KASSERT(ptp != NULL || pmap == pmap_kernel());
 
+	pmap_check_pv(pmap, ptp, pp, va, true);
+
 	mutex_spin_enter(>pp_lock);
 	pp->pp_attrs |= oattrs;
 	if (pve == NULL) {
@@ -2174,16 +2217,23 @@ pmap_remove_pv(struct pmap *pmap, struct
 		pp->pp_pte.pte_va = 0;
 		

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

2020-03-15 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Mar 15 19:41:05 UTC 2020

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

Log Message:
Fix a comment.


To generate a diff of this commit:
cvs rdiff -u -r1.369 -r1.370 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.369 src/sys/arch/x86/x86/pmap.c:1.370
--- src/sys/arch/x86/x86/pmap.c:1.369	Sun Mar 15 15:58:24 2020
+++ src/sys/arch/x86/x86/pmap.c	Sun Mar 15 19:41:04 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.369 2020/03/15 15:58:24 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.370 2020/03/15 19:41:04 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.369 2020/03/15 15:58:24 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.370 2020/03/15 19:41:04 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -2032,7 +2032,7 @@ pmap_lookup_pv(const struct pmap *pmap, 
 	 *
 	 * If the page is tracked with an embedded entry then the tree
 	 * lookup can be avoided.  It's safe to check for this specific
-	 * set of values without pp_lock because all 3 will only ever be
+	 * set of values without pp_lock because both will only ever be
 	 * set together for this pmap.
 	 *
 	 */
@@ -2088,7 +2088,7 @@ pmap_enter_pv(struct pmap *pmap, struct 
 	 * If entering the same page and it's already tracked with an
 	 * embedded entry, we can avoid the expense below.  It's safe
 	 * to check for this very specific set of values without a lock
-	 * because all 3 will only ever be set together for this pmap.
+	 * because both will only ever be set together for this pmap.
 	 */
 	if (atomic_load_relaxed(>pp_pte.pte_ptp) == ptp &&
 	atomic_load_relaxed(>pp_pte.pte_va) == va) {



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

2020-03-15 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Mar 15 15:14:23 UTC 2020

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

Log Message:
pmap_enter(): look directly in the tree for old PVE when installing an
unmanaged mapping, because there is no existing pmap_page to check in
the shortcut path (it traps).

pv_pte_next(): don't assert pp_embedded because it could have been removed
(during pmap_pp_remove()).


To generate a diff of this commit:
cvs rdiff -u -r1.367 -r1.368 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.367 src/sys/arch/x86/x86/pmap.c:1.368
--- src/sys/arch/x86/x86/pmap.c:1.367	Sat Mar 14 20:48:40 2020
+++ src/sys/arch/x86/x86/pmap.c	Sun Mar 15 15:14:22 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.367 2020/03/14 20:48:40 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.368 2020/03/15 15:14:22 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.367 2020/03/14 20:48:40 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.368 2020/03/15 15:14:22 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -559,7 +559,6 @@ pv_pte_next(struct pmap_page *pp, struct
 	KASSERT(mutex_owned(>pp_lock));
 	KASSERT(pvpte != NULL);
 	if (pvpte == >pp_pte) {
-		KASSERT(pp->pp_embedded);
 		return pve_to_pvpte(LIST_FIRST(>pp_pvlist));
 	}
 	return pve_to_pvpte(LIST_NEXT(pvpte_to_pve(pvpte), pve_list));
@@ -4668,7 +4667,7 @@ pmap_enter_ma(struct pmap *pmap, vaddr_t
 			panic("%s: alloc pve failed", __func__);
 		}
 	} else {
-		old_pve = pmap_lookup_pv(pmap, ptp, NULL, va);
+		old_pve = pmap_treelookup_pv(pmap, ptp, tree, va);
 	}
 
 	/* Map PTEs into address space. */
@@ -4773,6 +4772,7 @@ pmap_enter_ma(struct pmap *pmap, vaddr_t
 		pmap_remove_pv(pmap, old_pp, ptp, va, old_pve,
 		pmap_pte_to_pp_attrs(opte));
 		if (old_pve != NULL) {
+			KASSERT(old_pve->pve_pp == old_pp);
 			if (pmap->pm_pve == NULL) {
 pmap->pm_pve = old_pve;
 			} else {
@@ -5584,7 +5584,7 @@ pmap_ept_enter(struct pmap *pmap, vaddr_
 			panic("%s: alloc pve failed", __func__);
 		}
 	} else {
-		old_pve = pmap_lookup_pv(pmap, ptp, NULL, va);
+		old_pve = pmap_treelookup_pv(pmap, ptp, tree, va);
 	}
 
 	/* Map PTEs into address space. */
@@ -5654,6 +5654,7 @@ pmap_ept_enter(struct pmap *pmap, vaddr_
 		pmap_remove_pv(pmap, old_pp, ptp, va, old_pve,
 		pmap_ept_to_pp_attrs(opte));
 		if (old_pve != NULL) {
+			KASSERT(old_pve->pve_pp == old_pp);
 			if (pmap->pm_pve == NULL) {
 pmap->pm_pve = old_pve;
 			} else {



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

2020-03-14 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sat Mar 14 20:48:40 UTC 2020

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

Log Message:
Re: kern/55071 (Panic shortly after running X11 due to kernel diagnostic 
assertion "mutex_owned(>pp_lock)")

pmap_pp_remove(): get rid of a "goto" to make it clearer what's going on.


To generate a diff of this commit:
cvs rdiff -u -r1.366 -r1.367 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.366 src/sys/arch/x86/x86/pmap.c:1.367
--- src/sys/arch/x86/x86/pmap.c:1.366	Sat Mar 14 18:24:10 2020
+++ src/sys/arch/x86/x86/pmap.c	Sat Mar 14 20:48:40 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.366 2020/03/14 18:24:10 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.367 2020/03/14 20:48:40 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.366 2020/03/14 18:24:10 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.367 2020/03/14 20:48:40 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -4115,15 +4115,19 @@ pmap_pp_remove(struct pmap_page *pp, pad
 
 	count = SPINLOCK_BACKOFF_MIN;
 	kpreempt_disable();
-startover:
-	mutex_spin_enter(>pp_lock);
-	while ((pvpte = pv_pte_first(pp)) != NULL) {
+	for (;;) {
 		struct pmap *pmap;
 		struct pv_entry *pve;
 		pt_entry_t opte;
 		vaddr_t va;
 		int error;
 
+		mutex_spin_enter(>pp_lock);
+		if ((pvpte = pv_pte_first(pp)) == NULL) {
+			mutex_spin_exit(>pp_lock);
+			break;
+		}
+
 		/*
 		 * Add a reference to the pmap before clearing the pte.
 		 * Otherwise the pmap can disappear behind us.
@@ -4150,7 +4154,7 @@ startover:
 			if (ptp != NULL) {
 pmap_destroy(pmap);
 			}
-			goto startover;
+			continue;
 		}
 
 		error = pmap_sync_pv(pvpte, pa, ~0, , );
@@ -4167,7 +4171,7 @@ startover:
 			}
 			SPINLOCK_BACKOFF(count);
 			KERNEL_LOCK(hold_count, curlwp);
-			goto startover;
+			continue;
 		}
 
 		va = pvpte->pte_va;
@@ -4196,9 +4200,7 @@ startover:
 		if (ptp != NULL) {
 			pmap_destroy(pmap);
 		}
-		mutex_spin_enter(>pp_lock);
 	}
-	mutex_spin_exit(>pp_lock);
 	kpreempt_enable();
 }
 



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

2020-03-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Mar 14 05:19:50 UTC 2020

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

Log Message:
On amd64, mark the whole tree as NX. No real functional change, just to
prevent possible future surprises, and to make it a little harder to map
executable pages in ROP chains.


To generate a diff of this commit:
cvs rdiff -u -r1.363 -r1.364 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.363 src/sys/arch/x86/x86/pmap.c:1.364
--- src/sys/arch/x86/x86/pmap.c:1.363	Tue Mar 10 22:38:41 2020
+++ src/sys/arch/x86/x86/pmap.c	Sat Mar 14 05:19:50 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.363 2020/03/10 22:38:41 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.364 2020/03/14 05:19:50 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.363 2020/03/10 22:38:41 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.364 2020/03/14 05:19:50 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -4601,7 +4601,7 @@ pmap_get_physpage(void)
 /*
  * Expand the page tree with the specified amount of PTPs, mapping virtual
  * addresses starting at kva. We populate all the levels but the last one
- * (L1). The nodes of the tree are created as RWX, but the pages covered
+ * (L1). The nodes of the tree are created as RW, but the pages covered
  * will be kentered in L1, with proper permissions.
  *
  * Used only by pmap_growkernel.
@@ -4632,6 +4632,9 @@ pmap_alloc_level(struct pmap *cpm, vaddr
 			KASSERT(!pmap_valid_entry(pdep[i]));
 			pa = pmap_get_physpage();
 			pte = pmap_pa2pte(pa) | PTE_P | PTE_W;
+#ifdef __x86_64__
+			pte |= pmap_pg_nx;
+#endif
 			pmap_pte_set([i], pte);
 
 #ifdef XENPV



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

2020-03-07 Thread Maya Rashish
Module Name:src
Committed By:   maya
Date:   Sat Mar  7 13:28:45 UTC 2020

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

Log Message:
Fold constant. err is always 0, so switch to return 0;


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 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.27 src/sys/arch/x86/x86/via_padlock.c:1.28
--- src/sys/arch/x86/x86/via_padlock.c:1.27	Sat Mar  7 12:31:50 2020
+++ src/sys/arch/x86/x86/via_padlock.c	Sat Mar  7 13:28:45 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.27 2020/03/07 12:31:50 fcambus Exp $ */
+/*	$NetBSD: via_padlock.c,v 1.28 2020/03/07 13:28:45 maya Exp $ */
 
 /*-
  * Copyright (c) 2003 Jason Wright
@@ -20,7 +20,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.27 2020/03/07 12:31:50 fcambus Exp $");
+__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.28 2020/03/07 13:28:45 maya Exp $");
 
 #include 
 #include 
@@ -370,7 +370,6 @@ via_padlock_crypto_encdec(struct cryptop
 struct via_padlock_session *ses, struct via_padlock_softc *sc, void *buf)
 {
 	uint32_t *key;
-	int err = 0;
 
 	if ((crd->crd_len % 16) != 0)
 		return (EINVAL);
@@ -461,7 +460,7 @@ via_padlock_crypto_encdec(struct cryptop
 		sc->op_buf = NULL;
 	}
 
-	return (err);
+	return 0;
 }
 
 int



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

2020-03-07 Thread Frederic Cambus
Module Name:src
Committed By:   fcambus
Date:   Sat Mar  7 12:31:50 UTC 2020

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

Log Message:
Return error values directly where appropriate, instead of using the err
variable.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 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.26 src/sys/arch/x86/x86/via_padlock.c:1.27
--- src/sys/arch/x86/x86/via_padlock.c:1.26	Sat Jul 14 14:46:41 2018
+++ src/sys/arch/x86/x86/via_padlock.c	Sat Mar  7 12:31:50 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.26 2018/07/14 14:46:41 maxv Exp $ */
+/*	$NetBSD: via_padlock.c,v 1.27 2020/03/07 12:31:50 fcambus Exp $ */
 
 /*-
  * Copyright (c) 2003 Jason Wright
@@ -20,7 +20,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.26 2018/07/14 14:46:41 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: via_padlock.c,v 1.27 2020/03/07 12:31:50 fcambus Exp $");
 
 #include 
 #include 
@@ -372,16 +372,12 @@ via_padlock_crypto_encdec(struct cryptop
 	uint32_t *key;
 	int err = 0;
 
-	if ((crd->crd_len % 16) != 0) {
-		err = EINVAL;
-		return (err);
-	}
+	if ((crd->crd_len % 16) != 0)
+		return (EINVAL);
 
 	sc->op_buf = malloc(crd->crd_len, M_DEVBUF, M_NOWAIT);
-	if (sc->op_buf == NULL) {
-		err = ENOMEM;
-		return (err);
-	}
+	if (sc->op_buf == NULL)
+		return (ENOMEM);
 
 	if (crd->crd_flags & CRD_F_ENCRYPT) {
 		sc->op_cw[0] = ses->ses_cw0 | C3_CRYPT_CWLO_ENCRYPT;



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

2020-03-04 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed Mar  4 22:00:03 UTC 2020

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

Log Message:
pmap_enter(): ditch pv_entry if unmanaged.  Shouldn't happen I think, but
do for the sake of correctness.


To generate a diff of this commit:
cvs rdiff -u -r1.361 -r1.362 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.361 src/sys/arch/x86/x86/pmap.c:1.362
--- src/sys/arch/x86/x86/pmap.c:1.361	Sun Mar  1 21:42:58 2020
+++ src/sys/arch/x86/x86/pmap.c	Wed Mar  4 22:00:03 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.361 2020/03/01 21:42:58 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.362 2020/03/04 22:00:03 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.361 2020/03/01 21:42:58 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.362 2020/03/04 22:00:03 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -4415,8 +4415,8 @@ pmap_enter_ma(struct pmap *pmap, vaddr_t
 		if ((opte & PTE_PVLIST) != 0 && pve != NULL) {
 			KASSERT(pve->pve_pte.pte_ptp == ptp);
 			KASSERT(pve->pve_pte.pte_va == va);
+			pve = NULL;
 		}
-		pve = NULL;
 		goto same_pa;
 	}
 
@@ -5293,8 +5293,8 @@ pmap_ept_enter(struct pmap *pmap, vaddr_
 		if ((opte & EPT_PVLIST) != 0 && pve != NULL) {
 			KASSERT(pve->pve_pte.pte_ptp == ptp);
 			KASSERT(pve->pve_pte.pte_va == va);
+			pve = NULL;
 		}
-		pve = NULL;
 		goto same_pa;
 	}
 



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

2020-03-01 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Mar  1 21:42:58 UTC 2020

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

Log Message:
- Give pmap uvm_objects an empty pagerops to avoid special casing in UVM.
  (This use of uvm_object causes a disproportionate amount of work.)

- Undo the pmap_destroy()/pmap_delref() split.  I misunderstood the flow of
  control, and there's no need for this.

- For pmap_remove_pv(), always look up the pv_entry in advance as those
  calls will need to be covered by lock again soon.


To generate a diff of this commit:
cvs rdiff -u -r1.360 -r1.361 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.360 src/sys/arch/x86/x86/pmap.c:1.361
--- src/sys/arch/x86/x86/pmap.c:1.360	Sat Feb 29 20:17:11 2020
+++ src/sys/arch/x86/x86/pmap.c	Sun Mar  1 21:42:58 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.360 2020/02/29 20:17:11 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.361 2020/03/01 21:42:58 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.360 2020/02/29 20:17:11 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.361 2020/03/01 21:42:58 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -252,6 +252,10 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.3
 #define	PMAP_DUMMY_UNLOCK(pm)
 #endif
 
+static const struct uvm_pagerops pmap_pager = {
+	/* nothing */
+};
+
 const vaddr_t ptp_masks[] = PTP_MASK_INITIALIZER;
 const vaddr_t ptp_frames[] = PTP_FRAME_INITIALIZER;
 const int ptp_shifts[] = PTP_SHIFT_INITIALIZER;
@@ -444,7 +448,6 @@ static void pmap_alloc_level(struct pmap
 
 static void pmap_load1(struct lwp *, struct pmap *, struct pmap *);
 static void pmap_reactivate(struct pmap *);
-static void pmap_dropref(struct pmap *);
 
 /*
  * p m a p   h e l p e r   f u n c t i o n s
@@ -677,7 +680,7 @@ pmap_unmap_ptes(struct pmap *pmap, struc
 
 	/* Toss reference to other pmap taken earlier. */
 	if (pmap2 != NULL) {
-		pmap_dropref(pmap2);
+		pmap_destroy(pmap2);
 	}
 }
 
@@ -1024,16 +1027,12 @@ pmap_bootstrap(vaddr_t kva_start)
 	 *
 	 * The kernel pmap's pm_obj is not used for much. However, in user pmaps
 	 * the pm_obj contains the list of active PTPs.
-	 *
-	 * The pm_obj currently does not have a pager. It might be possible to
-	 * add a pager that would allow a process to read-only mmap its own page
-	 * tables (fast user-level vtophys?). This may or may not be useful.
 	 */
 	kpm = pmap_kernel();
 	mutex_init(>pm_lock, MUTEX_DEFAULT, IPL_NONE);
 	rw_init(>pm_dummy_lock);
 	for (i = 0; i < PTP_LEVELS - 1; i++) {
-		uvm_obj_init(>pm_obj[i], NULL, false, 1);
+		uvm_obj_init(>pm_obj[i], _pager, false, 1);
 		uvm_obj_setlock(>pm_obj[i], >pm_dummy_lock);
 		kpm->pm_ptphint[i] = NULL;
 	}
@@ -1930,7 +1929,7 @@ pmap_enter_pv(struct pmap *pmap, struct 
  * => we return the removed pve
  * => caller can optionally supply pve, if looked up already
  */
-static struct pv_entry *
+static void
 pmap_remove_pv(struct pmap *pmap, struct pmap_page *pp, struct vm_page *ptp,
 vaddr_t va, struct pv_entry *pve)
 {
@@ -1947,19 +1946,13 @@ pmap_remove_pv(struct pmap *pmap, struct
 		pp->pp_flags &= ~PP_EMBEDDED;
 		pp->pp_pte.pte_ptp = NULL;
 		pp->pp_pte.pte_va = 0;
-		return NULL;
-	}
-
-	if (pve == NULL) {
-		pve = pmap_pvmap_lookup(pmap, va);
-		KASSERT(pve != NULL);
 	} else {
+		KASSERT(pve != NULL);
 		KASSERT(pve == pmap_pvmap_lookup(pmap, va));
+		KASSERT(pve->pve_pte.pte_ptp == ptp);
+		KASSERT(pve->pve_pte.pte_va == va);
+		LIST_REMOVE(pve, pve_list);
 	}
-	KASSERT(pve->pve_pte.pte_ptp == ptp);
-	KASSERT(pve->pve_pte.pte_va == va);
-	LIST_REMOVE(pve, pve_list);
-	return pve;
 }
 
 /*
@@ -2487,7 +2480,7 @@ pmap_create(void)
 
 	/* init uvm_object */
 	for (i = 0; i < PTP_LEVELS - 1; i++) {
-		uvm_obj_init(>pm_obj[i], NULL, false, 1);
+		uvm_obj_init(>pm_obj[i], _pager, false, 1);
 		uvm_obj_setlock(>pm_obj[i], >pm_dummy_lock);
 		pmap->pm_ptphint[i] = NULL;
 	}
@@ -2545,7 +2538,7 @@ pmap_check_inuse(struct pmap *pmap)
 		for (int i = 0; i < PDIR_SLOT_USERLIM; i++) {
 			if (pmap->pm_pdir[i] != 0 &&
 			ci->ci_kpm_pdir[i] == pmap->pm_pdir[i]) {
-printf("pmap_dropref(%p) pmap_kernel %p "
+printf("pmap_destroy(%p) pmap_kernel %p "
 "curcpu %d cpu %d ci_pmap %p "
 "ci->ci_kpm_pdir[%d]=%" PRIx64
 " pmap->pm_pdir[%d]=%" PRIx64 "\n",
@@ -2562,29 +2555,22 @@ pmap_check_inuse(struct pmap *pmap)
 }
 
 /*
- * pmap_destroy:  pmap is being destroyed by UVM.
- */
-void
-pmap_destroy(struct pmap *pmap)
-{
-
-	/* Undo pmap_remove_all(), then drop the reference. */
-	pmap_update(pmap);
-	pmap_dropref(pmap);
-}
-
-/*
- * pmap_dropref:  drop reference count on pmap.  free pmap if reference
+ * pmap_destroy:  drop reference count on pmap.  free 

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

2020-02-29 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sat Feb 29 20:17:11 UTC 2020

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

Log Message:
PR kern/55033: kernel panics when starting X

Remove the uvm_page_owner_locked_p() assertions in the x86 pmap.  The DRM
code doesn't follow the locking protocol (it's OK though, since pages aren't
changing identity) and having thought about it more we're most likely going
to have to do full PV locking to make progress on concurrent fault handing,
ergo assertions not so important.


To generate a diff of this commit:
cvs rdiff -u -r1.359 -r1.360 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.359 src/sys/arch/x86/x86/pmap.c:1.360
--- src/sys/arch/x86/x86/pmap.c:1.359	Sun Feb 23 22:28:53 2020
+++ src/sys/arch/x86/x86/pmap.c	Sat Feb 29 20:17:11 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.359 2020/02/23 22:28:53 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.360 2020/02/29 20:17:11 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.359 2020/02/23 22:28:53 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.360 2020/02/29 20:17:11 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -230,7 +230,7 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.3
  * - pg->uobject->vmobjlock, pg->uanon->an_lock
  *   These per-object locks are taken by the VM system before calling into
  *   the pmap module.  Holding them prevents concurrent operations on the
- *   given page or set of pages.  Asserted with uvm_page_owner_locked_p().
+ *   given page or set of pages.
  *
  * - pmap->pm_lock (per pmap)
  *   This lock protects the fields in the pmap structure including the
@@ -3588,7 +3588,6 @@ pmap_remove_pte(struct pmap *pmap, struc
 	}
 
 	if ((pg = PHYS_TO_VM_PAGE(pmap_pte2pa(opte))) != NULL) {
-		KASSERT(uvm_page_owner_locked_p(pg, false));
 		pp = VM_PAGE_TO_PP(pg);
 	} else if ((pp = pmap_pv_tracked(pmap_pte2pa(opte))) == NULL) {
 		paddr_t pa = pmap_pte2pa(opte);
@@ -3921,9 +3920,6 @@ pmap_page_remove(struct vm_page *pg)
 	struct pmap_page *pp;
 	paddr_t pa;
 
-	/* Need an exclusive lock to prevent PV list changing behind us. */
-	KASSERT(uvm_page_owner_locked_p(pg, true));
-
 	pp = VM_PAGE_TO_PP(pg);
 	pa = VM_PAGE_TO_PHYS(pg);
 	pmap_pp_remove(pp, pa);
@@ -3963,9 +3959,6 @@ pmap_test_attrs(struct vm_page *pg, unsi
 	u_int result;
 	paddr_t pa;
 
-	/* Need an exclusive lock to prevent PV list changing behind us. */
-	KASSERT(uvm_page_owner_locked_p(pg, true));
-
 	pp = VM_PAGE_TO_PP(pg);
 	if ((pp->pp_attrs & testbits) != 0) {
 		return true;
@@ -4037,9 +4030,6 @@ pmap_clear_attrs(struct vm_page *pg, uns
 	struct pmap_page *pp;
 	paddr_t pa;
 
-	/* Need an exclusive lock to prevent PV list changing behind us. */
-	KASSERT(uvm_page_owner_locked_p(pg, true));
-
 	pp = VM_PAGE_TO_PP(pg);
 	pa = VM_PAGE_TO_PHYS(pg);
 
@@ -4316,7 +4306,6 @@ pmap_enter_ma(struct pmap *pmap, vaddr_t
 		/* This is a managed page */
 		npte |= PTE_PVLIST;
 		new_pp = VM_PAGE_TO_PP(new_pg);
-		KASSERT(uvm_page_owner_locked_p(new_pg, false));
 	} else if ((new_pp = pmap_pv_tracked(pa)) != NULL) {
 		/* This is an unmanaged pv-tracked page */
 		npte |= PTE_PVLIST;
@@ -4448,7 +4437,6 @@ pmap_enter_ma(struct pmap *pmap, vaddr_t
 	 */
 	if ((~opte & (PTE_P | PTE_PVLIST)) == 0) {
 		if ((old_pg = PHYS_TO_VM_PAGE(oldpa)) != NULL) {
-			KASSERT(uvm_page_owner_locked_p(old_pg, false));
 			old_pp = VM_PAGE_TO_PP(old_pg);
 		} else if ((old_pp = pmap_pv_tracked(oldpa)) == NULL) {
 			panic("%s: PTE_PVLIST with pv-untracked page"
@@ -5212,7 +5200,6 @@ pmap_ept_enter(struct pmap *pmap, vaddr_
 		/* This is a managed page */
 		npte |= EPT_PVLIST;
 		new_pp = VM_PAGE_TO_PP(new_pg);
-		KASSERT(uvm_page_owner_locked_p(new_pg, false));
 	} else if ((new_pp = pmap_pv_tracked(pa)) != NULL) {
 		/* This is an unmanaged pv-tracked page */
 		npte |= EPT_PVLIST;
@@ -5329,7 +5316,6 @@ pmap_ept_enter(struct pmap *pmap, vaddr_
 	 */
 	if ((~opte & (EPT_R | EPT_PVLIST)) == 0) {
 		if ((old_pg = PHYS_TO_VM_PAGE(oldpa)) != NULL) {
-			KASSERT(uvm_page_owner_locked_p(old_pg, false));
 			old_pp = VM_PAGE_TO_PP(old_pg);
 		} else if ((old_pp = pmap_pv_tracked(oldpa)) == NULL) {
 			panic("%s: EPT_PVLIST with pv-untracked page"
@@ -5495,7 +5481,6 @@ pmap_ept_remove_pte(struct pmap *pmap, s
 	}
 
 	if ((pg = PHYS_TO_VM_PAGE(pmap_pte2pa(opte))) != NULL) {
-		KASSERT(uvm_page_owner_locked_p(pg, false));
 		pp = VM_PAGE_TO_PP(pg);
 	} else if ((pp = pmap_pv_tracked(pmap_pte2pa(opte))) == NULL) {
 		paddr_t pa = pmap_pte2pa(opte);



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

2020-02-23 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Feb 23 18:57:28 UTC 2020

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

Log Message:
Adjustment to previous: TP_SET_DONE() was wiping out the VA to shoot,
instead of ORing the flag into the array element.  This caused the CPU
initiating the shootdown to occasionally miss an INVLPG.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/x86/x86/x86_tlb.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_tlb.c
diff -u src/sys/arch/x86/x86/x86_tlb.c:1.16 src/sys/arch/x86/x86/x86_tlb.c:1.17
--- src/sys/arch/x86/x86/x86_tlb.c:1.16	Sat Feb 22 20:12:40 2020
+++ src/sys/arch/x86/x86/x86_tlb.c	Sun Feb 23 18:57:28 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_tlb.c,v 1.16 2020/02/22 20:12:40 maxv Exp $	*/
+/*	$NetBSD: x86_tlb.c,v 1.17 2020/02/23 18:57:28 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008-2020 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.16 2020/02/22 20:12:40 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.17 2020/02/23 18:57:28 ad Exp $");
 
 #include 
 #include 
@@ -99,7 +99,11 @@ typedef struct {
 
 #define	TP_SET_USERPMAP(tp)	((tp)->tp_store[TP_USERPMAP] |= 1)
 #define	TP_SET_GLOBAL(tp)	((tp)->tp_store[TP_GLOBAL] |= 1)
-#define	TP_SET_DONE(tp)		atomic_store_relaxed(&(tp)->tp_store[TP_DONE], 1)
+#define	TP_SET_DONE(tp) \
+do { \
+	uintptr_t v = atomic_load_relaxed(&(tp)->tp_store[TP_DONE]); \
+	atomic_store_relaxed(&(tp)->tp_store[TP_DONE], v | 1); \
+} while (/* CONSTCOND */ 0);
 
 #define	TP_CLEAR(tp)		memset(__UNVOLATILE(tp), 0, sizeof(*(tp)));
 



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

2020-02-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Feb 22 20:12:40 UTC 2020

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

Log Message:
add relaxed atomics, ok ad@ riastradh@


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/x86/x86/x86_tlb.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_tlb.c
diff -u src/sys/arch/x86/x86/x86_tlb.c:1.15 src/sys/arch/x86/x86/x86_tlb.c:1.16
--- src/sys/arch/x86/x86/x86_tlb.c:1.15	Wed Jan 15 13:22:03 2020
+++ src/sys/arch/x86/x86/x86_tlb.c	Sat Feb 22 20:12:40 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_tlb.c,v 1.15 2020/01/15 13:22:03 ad Exp $	*/
+/*	$NetBSD: x86_tlb.c,v 1.16 2020/02/22 20:12:40 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008-2020 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.15 2020/01/15 13:22:03 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.16 2020/02/22 20:12:40 maxv Exp $");
 
 #include 
 #include 
@@ -90,7 +90,7 @@ typedef struct {
 #define	TP_GET_COUNT(tp)	((tp)->tp_store[TP_COUNT] & PAGE_MASK)
 #define	TP_GET_USERPMAP(tp)	((tp)->tp_store[TP_USERPMAP] & 1)
 #define	TP_GET_GLOBAL(tp)	((tp)->tp_store[TP_GLOBAL] & 1)
-#define	TP_GET_DONE(tp)		((tp)->tp_store[TP_DONE] & 1)
+#define	TP_GET_DONE(tp)		(atomic_load_relaxed(&(tp)->tp_store[TP_DONE]) & 1)
 #define	TP_GET_VA(tp, i)	((tp)->tp_store[(i)] & ~PAGE_MASK)
 
 #define	TP_INC_COUNT(tp)	((tp)->tp_store[TP_COUNT]++)
@@ -99,7 +99,7 @@ typedef struct {
 
 #define	TP_SET_USERPMAP(tp)	((tp)->tp_store[TP_USERPMAP] |= 1)
 #define	TP_SET_GLOBAL(tp)	((tp)->tp_store[TP_GLOBAL] |= 1)
-#define	TP_SET_DONE(tp)		((tp)->tp_store[TP_DONE] |= 1)
+#define	TP_SET_DONE(tp)		atomic_store_relaxed(&(tp)->tp_store[TP_DONE], 1)
 
 #define	TP_CLEAR(tp)		memset(__UNVOLATILE(tp), 0, sizeof(*(tp)));
 
@@ -409,7 +409,7 @@ pmap_tlb_shootnow(void)
 	KASSERT(TP_GET_DONE(ts) == 0);
 	while (atomic_cas_ptr(_tlb_packet, NULL,
 	__UNVOLATILE(ts)) != NULL) {
-		KASSERT(pmap_tlb_packet != ts);
+		KASSERT(atomic_load_relaxed(_tlb_packet) != ts);
 		/*
 		 * Don't bother with exponentional backoff, as the pointer
 		 * is in a dedicated cache line and only updated twice per
@@ -419,7 +419,7 @@ pmap_tlb_shootnow(void)
 		splx(s);
 		do {
 			x86_pause();
-		} while (pmap_tlb_packet != NULL);
+		} while (atomic_load_relaxed(_tlb_packet) != NULL);
 		s = splvm();
 
 		/*
@@ -509,7 +509,7 @@ pmap_tlb_intr(void)
 	 * seemingly active.
 	 */
 	if (atomic_dec_uint_nv(_tlb_pendcount) == 0) {
-		pmap_tlb_packet = NULL;
+		atomic_store_relaxed(_tlb_packet, NULL);
 		__insn_barrier();
 		TP_SET_DONE(source);
 	}



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

2020-02-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Feb 21 18:34:37 UTC 2020

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

Log Message:
In pmap_changeprot_local(), drop the dirty bit along with the write bit.


To generate a diff of this commit:
cvs rdiff -u -r1.356 -r1.357 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.356 src/sys/arch/x86/x86/pmap.c:1.357
--- src/sys/arch/x86/x86/pmap.c:1.356	Fri Feb 21 18:31:55 2020
+++ src/sys/arch/x86/x86/pmap.c	Fri Feb 21 18:34:37 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.356 2020/02/21 18:31:55 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.357 2020/02/21 18:34:37 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.356 2020/02/21 18:31:55 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.357 2020/02/21 18:34:37 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -868,7 +868,7 @@ pmap_changeprot_local(vaddr_t va, vm_pro
 	if ((prot & VM_PROT_WRITE) != 0)
 		npte |= PTE_W;
 	else
-		npte &= ~PTE_W;
+		npte &= ~(PTE_W|PTE_D);
 
 	if (opte != npte) {
 		pmap_pte_set(pte, npte);



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

2020-02-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Feb 21 18:31:56 UTC 2020

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

Log Message:
Add comments.


To generate a diff of this commit:
cvs rdiff -u -r1.355 -r1.356 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.355 src/sys/arch/x86/x86/pmap.c:1.356
--- src/sys/arch/x86/x86/pmap.c:1.355	Sun Jan 12 13:01:11 2020
+++ src/sys/arch/x86/x86/pmap.c	Fri Feb 21 18:31:55 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.355 2020/01/12 13:01:11 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.356 2020/02/21 18:31:55 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.355 2020/01/12 13:01:11 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.356 2020/02/21 18:31:55 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1258,6 +1258,17 @@ slotspace_rand(int type, size_t sz, size
 	vaddr_t startva, va;
 
 	sz = roundup(sz, align);
+
+	/*
+	 * Take one more slot with +NBPD_L4, because we may end up choosing
+	 * an area that crosses slots:
+	 * +--+--+--+
+	 * | Slot | Slot | Slot |
+	 * +--+--+--+
+	 *[Chosen Area]
+	 * And in that case we must take into account the additional slot
+	 * consumed.
+	 */
 	nslots = roundup(sz+NBPD_L4, NBPD_L4) / NBPD_L4;
 
 	/* Get the holes. */
@@ -1279,16 +1290,20 @@ slotspace_rand(int type, size_t sz, size
 minnslot = slotspace.area[i].nslot;
 			}
 		}
+
+		/* No hole anymore, stop here. */
 		if (minsslot == 512) {
 			break;
 		}
 
+		/* Register the hole. */
 		if (minsslot - curslot >= nslots) {
 			holes[nholes].start = curslot;
 			holes[nholes].end = minsslot;
 			nholes++;
 		}
 
+		/* Skip that hole, and iterate again. */
 		curslot = minsslot + minnslot;
 	}
 



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

2020-01-31 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Jan 31 08:55:38 UTC 2020

Modified Files:
src/sys/arch/x86/x86: dbregs.c fpu.c spectre.c svs.c

Log Message:
'oldlwp' is never NULL now, so remove the NULL checks.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/x86/x86/dbregs.c
cvs rdiff -u -r1.60 -r1.61 src/sys/arch/x86/x86/fpu.c
cvs rdiff -u -r1.32 -r1.33 src/sys/arch/x86/x86/spectre.c
cvs rdiff -u -r1.31 -r1.32 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/dbregs.c
diff -u src/sys/arch/x86/x86/dbregs.c:1.14 src/sys/arch/x86/x86/dbregs.c:1.15
--- src/sys/arch/x86/x86/dbregs.c:1.14	Mon Jan 14 18:54:07 2019
+++ src/sys/arch/x86/x86/dbregs.c	Fri Jan 31 08:55:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dbregs.c,v 1.14 2019/01/14 18:54:07 maxv Exp $	*/
+/*	$NetBSD: dbregs.c,v 1.15 2020/01/31 08:55:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -297,13 +297,10 @@ x86_dbregs_switch(struct lwp *oldlwp, st
 	struct pcb *oldpcb, *newpcb;
 	bool olddb, newdb;
 
-	oldpcb = (oldlwp != NULL) ? lwp_getpcb(oldlwp) : NULL;
+	oldpcb = lwp_getpcb(oldlwp);
 	newpcb = lwp_getpcb(newlwp);
 
-	olddb = false;
-	if (oldpcb) {
-		olddb = (oldpcb->pcb_flags & PCB_DBREGS) != 0;
-	}
+	olddb = (oldpcb->pcb_flags & PCB_DBREGS) != 0;
 	newdb = (newpcb->pcb_flags & PCB_DBREGS) != 0;
 
 	if (__predict_true(!olddb && !newdb)) {

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.60 src/sys/arch/x86/x86/fpu.c:1.61
--- src/sys/arch/x86/x86/fpu.c:1.60	Wed Nov 27 06:24:33 2019
+++ src/sys/arch/x86/x86/fpu.c	Fri Jan 31 08:55:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.60 2019/11/27 06:24:33 maxv Exp $	*/
+/*	$NetBSD: fpu.c,v 1.61 2020/01/31 08:55:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.60 2019/11/27 06:24:33 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.61 2020/01/31 08:55:38 maxv Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -301,7 +301,7 @@ fpu_switch(struct lwp *oldlwp, struct lw
 {
 	struct pcb *pcb;
 
-	if ((oldlwp != NULL) && (oldlwp->l_md.md_flags & MDL_FPU_IN_CPU)) {
+	if (oldlwp->l_md.md_flags & MDL_FPU_IN_CPU) {
 		KASSERT(!(oldlwp->l_flag & LW_SYSTEM));
 		pcb = lwp_getpcb(oldlwp);
 		fpu_area_save(>pcb_savefpu, x86_xsave_features);

Index: src/sys/arch/x86/x86/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.32 src/sys/arch/x86/x86/spectre.c:1.33
--- src/sys/arch/x86/x86/spectre.c:1.32	Thu Dec 12 16:49:20 2019
+++ src/sys/arch/x86/x86/spectre.c	Fri Jan 31 08:55:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.32 2019/12/12 16:49:20 maxv Exp $	*/
+/*	$NetBSD: spectre.c,v 1.33 2020/01/31 08:55:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018-2019 NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.32 2019/12/12 16:49:20 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.33 2020/01/31 08:55:38 maxv Exp $");
 
 #include "opt_spectre.h"
 
@@ -966,8 +966,7 @@ speculation_barrier(struct lwp *oldlwp, 
 	/*
 	 * From kernel thread to kernel thread, no need for a barrier.
 	 */
-	if ((oldlwp != NULL && (oldlwp->l_flag & LW_SYSTEM)) &&
-	(newlwp->l_flag & LW_SYSTEM))
+	if ((oldlwp->l_flag & LW_SYSTEM) && (newlwp->l_flag & LW_SYSTEM))
 		return;
 
 	switch (v2_mitigation_method) {

Index: src/sys/arch/x86/x86/svs.c
diff -u src/sys/arch/x86/x86/svs.c:1.31 src/sys/arch/x86/x86/svs.c:1.32
--- src/sys/arch/x86/x86/svs.c:1.31	Sun Dec  8 20:42:48 2019
+++ src/sys/arch/x86/x86/svs.c	Fri Jan 31 08:55:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: svs.c,v 1.31 2019/12/08 20:42:48 ad Exp $	*/
+/*	$NetBSD: svs.c,v 1.32 2020/01/31 08:55:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.31 2019/12/08 20:42:48 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.32 2020/01/31 08:55:38 maxv Exp $");
 
 #include "opt_svs.h"
 #include "opt_user_ldt.h"
@@ -540,7 +540,7 @@ svs_lwp_switch(struct lwp *oldlwp, struc
 	}
 
 #ifdef DIAGNOSTIC
-	if (oldlwp != NULL && !(oldlwp->l_flag & LW_SYSTEM)) {
+	if (!(oldlwp->l_flag & LW_SYSTEM)) {
 		pcb = lwp_getpcb(oldlwp);
 		rsp0 = pcb->pcb_rsp0;
 		va = rounddown(rsp0, PAGE_SIZE);



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

2020-01-29 Thread Emmanuel Dreyfus
Module Name:src
Committed By:   manu
Date:   Thu Jan 30 01:49:44 UTC 2020

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

Log Message:
Insert memory map with its real size, not the maximum possible.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 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.3 src/sys/arch/x86/x86/multiboot2.c:1.4
--- src/sys/arch/x86/x86/multiboot2.c:1.3	Tue Dec 10 02:06:07 2019
+++ src/sys/arch/x86/x86/multiboot2.c	Thu Jan 30 01:49:44 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: multiboot2.c,v 1.3 2019/12/10 02:06:07 manu Exp $	*/
+/*	$NetBSD: multiboot2.c,v 1.4 2020/01/30 01:49:44 manu Exp $	*/
 
 /*-
  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: multiboot2.c,v 1.3 2019/12/10 02:06:07 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: multiboot2.c,v 1.4 2020/01/30 01:49:44 manu Exp $");
 
 #include "opt_multiboot.h"
 
@@ -565,7 +565,7 @@ mbi_mmap(struct multiboot_tag_mmap *mbt)
 	}
 
 	bootinfo_add((struct btinfo_common *)bim, BTINFO_MEMMAP,
-	sizeof(bimbuf));
+	(char*)>entry[bim->num] - (char *)bim);
 
 	return;
 }



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

2020-01-28 Thread Emmanuel Dreyfus
Module Name:src
Committed By:   manu
Date:   Wed Jan 29 01:54:34 UTC 2020

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

Log Message:
Fix startup crashes caused by wrong memory map handling

init_x86_vm() takes the memory map from BIOS and EFI and selects
regions suitable for memory allocation. This involves removing
areas used by the kernel, but the logic missed some corner cases,
which led to possible allocation in regions for which later memory
access would cause a panic.

The typical panic from this bug in GENERIC is at SVS startup:
cpu_svs_init / uvm_pagealloc_strat / pagezero

We fix the bug by adding logic for the missing cases of memory
regions overlapping with the kernel. While there, add more #idef'ed
debug output.


To generate a diff of this commit:
cvs rdiff -u -r1.134 -r1.135 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.134 src/sys/arch/x86/x86/x86_machdep.c:1.135
--- src/sys/arch/x86/x86/x86_machdep.c:1.134	Sat Dec 28 00:38:08 2019
+++ src/sys/arch/x86/x86/x86_machdep.c	Wed Jan 29 01:54:34 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.134 2019/12/28 00:38:08 pgoyette Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.135 2020/01/29 01:54:34 manu Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.134 2019/12/28 00:38:08 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.135 2020/01/29 01:54:34 manu Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -669,7 +669,7 @@ x86_parse_clusters(struct btinfo_memmap 
 		type = bim->entry[x].type;
 #ifdef DEBUG_MEMLOAD
 		printf("MEMMAP: 0x%016" PRIx64 "-0x%016" PRIx64
-		", size=0x%016" PRIx64 ", type=%d(%s)\n",
+		"\n\tsize=0x%016" PRIx64 ", type=%d(%s)\n",
 		addr, addr + size - 1, size, type,
 		(type == BIM_Memory) ?  "Memory" :
 		(type == BIM_Reserved) ?  "Reserved" :
@@ -909,27 +909,95 @@ init_x86_vm(paddr_t pa_kend)
 		seg_start1 = 0;
 		seg_end1 = 0;
 
+#ifdef DEBUG_MEMLOAD
+		printf("segment %" PRIx64 " - %" PRIx64 "\n",
+		seg_start, seg_end);
+#endif
+
 		/* Skip memory before our available starting point. */
-		if (seg_end <= lowmem_rsvd)
+		if (seg_end <= lowmem_rsvd) {
+#ifdef DEBUG_MEMLOAD
+			printf("discard segment below starting point "
+			"%" PRIx64 " - %" PRIx64 "\n", seg_start, seg_end);
+#endif
 			continue;
+		}
 
 		if (seg_start <= lowmem_rsvd && lowmem_rsvd < seg_end) {
 			seg_start = lowmem_rsvd;
-			if (seg_start == seg_end)
+			if (seg_start == seg_end) {
+#ifdef DEBUG_MEMLOAD
+printf("discard segment below starting point "
+"%" PRIx64 " - %" PRIx64 "\n",
+seg_start, seg_end);
+
+
+#endif
 continue;
+			}
 		}
 
 		/*
 		 * If this segment contains the kernel, split it in two, around
 		 * the kernel.
+		 *  [seg_start   seg_end]
+		 * [pa_kstart  pa_kend]
 		 */
 		if (seg_start <= pa_kstart && pa_kend <= seg_end) {
+#ifdef DEBUG_MEMLOAD
+			printf("split kernel overlapping to "
+			"%" PRIx64 " - %lx and %lx - %" PRIx64 "\n",
+			seg_start, pa_kstart, pa_kend, seg_end);
+#endif
 			seg_start1 = pa_kend;
 			seg_end1 = seg_end;
 			seg_end = pa_kstart;
 			KASSERT(seg_end < seg_end1);
 		}
 
+		/*
+		 * Discard a segment inside the kernel
+		 *  [pa_kstart   pa_kend]
+		 * [seg_start  seg_end]
+		 */
+		if (pa_kstart < seg_start && seg_end < pa_kend) {
+#ifdef DEBUG_MEMLOAD
+			printf("discard complete kernel overlap "
+			"%" PRIx64 " - %" PRIx64 "\n", seg_start, seg_end);
+#endif
+			continue;
+		}
+
+		/*
+		 * Discard leading hunk that overlaps the kernel
+		 *  [pa_kstart pa_kend]
+		 *[seg_startseg_end]
+		 */
+		if (pa_kstart < seg_start &&
+		seg_start < pa_kend &&
+		pa_kend < seg_end) {
+#ifdef DEBUG_MEMLOAD
+			printf("discard leading kernel overlap "
+			"%" PRIx64 " - %lx\n", seg_start, pa_kend);
+#endif
+			seg_start = pa_kend;
+		}
+
+		/*
+		 * Discard trailing hunk that overlaps the kernel
+		 * [pa_kstartpa_kend]
+		 *  [seg_start  seg_end]
+		 */
+		if (seg_start < pa_kstart &&
+		pa_kstart < seg_end &&
+		seg_end < pa_kend) {
+#ifdef DEBUG_MEMLOAD
+			printf("discard trailing kernel overlap "
+			"%lx - %" PRIx64 "\n", pa_kstart, seg_end);
+#endif
+			seg_end = pa_kstart;
+		}
+		
 		/* First hunk */
 		if (seg_start != seg_end) {
 			x86_load_region(seg_start, seg_end);



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

2020-01-25 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sat Jan 25 15:38:24 UTC 2020

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

Log Message:
cpu_lwp_free() can be called with (l != curlwp) in error paths, so don't
detonate.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 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.40 src/sys/arch/x86/x86/vm_machdep.c:1.41
--- src/sys/arch/x86/x86/vm_machdep.c:1.40	Sun Jan 12 13:01:11 2020
+++ src/sys/arch/x86/x86/vm_machdep.c	Sat Jan 25 15:38:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: vm_machdep.c,v 1.40 2020/01/12 13:01:11 ad Exp $	*/
+/*	$NetBSD: vm_machdep.c,v 1.41 2020/01/25 15:38:24 ad 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.40 2020/01/12 13:01:11 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.41 2020/01/25 15:38:24 ad Exp $");
 
 #include "opt_mtrr.h"
 
@@ -237,12 +237,16 @@ cpu_lwp_fork(struct lwp *l1, struct lwp 
 /*
  * cpu_lwp_free is called from exit() to let machine-dependent
  * code free machine-dependent resources.  Note that this routine
- * must not block.
+ * must not block.  NB: this may be called with l != curlwp in
+ * error paths.
  */
 void
 cpu_lwp_free(struct lwp *l, int proc)
 {
 
+	if (l != curlwp)
+		return;
+
 	/* Abandon the FPU state. */
 	fpu_lwp_abandon(l);
 



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

2020-01-19 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Mon Jan 20 06:50:34 UTC 2020

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

Log Message:
assert smt_bits value only after it is computed.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/x86/x86/cpu_topology.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_topology.c
diff -u src/sys/arch/x86/x86/cpu_topology.c:1.17 src/sys/arch/x86/x86/cpu_topology.c:1.18
--- src/sys/arch/x86/x86/cpu_topology.c:1.17	Thu Jan  9 16:35:03 2020
+++ src/sys/arch/x86/x86/cpu_topology.c	Mon Jan 20 06:50:34 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu_topology.c,v 1.17 2020/01/09 16:35:03 ad Exp $	*/
+/*	$NetBSD: cpu_topology.c,v 1.18 2020/01/20 06:50:34 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2009 Mindaugas Rasiukevicius ,
@@ -36,7 +36,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu_topology.c,v 1.17 2020/01/09 16:35:03 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu_topology.c,v 1.18 2020/01/20 06:50:34 mlelstv Exp $");
 
 #include "acpica.h"
 
@@ -190,8 +190,9 @@ x86_cpu_topology(struct cpu_info *ci)
 		x86_cpuid(0x801e, descs);
 		const u_int threads = ((descs[1] >> 8) & 0xff) + 1;
 
-		KASSERT(smt_bits == 0 && smt_bits <= core_bits);
+		KASSERT(smt_bits == 0);
 		smt_bits = ilog2(threads);
+		KASSERT(smt_bits <= core_bits);
 		core_bits -= smt_bits;
 	}
 



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

2020-01-16 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Fri Jan 17 04:48:21 UTC 2020

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

Log Message:
 Add Fast Short Rep Mov(fsrm).


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 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.34 src/sys/arch/x86/x86/procfs_machdep.c:1.35
--- src/sys/arch/x86/x86/procfs_machdep.c:1.34	Thu Oct 17 06:58:48 2019
+++ src/sys/arch/x86/x86/procfs_machdep.c	Fri Jan 17 04:48:21 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.34 2019/10/17 06:58:48 msaitoh Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.35 2020/01/17 04:48:21 msaitoh Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.34 2019/10/17 06:58:48 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.35 2020/01/17 04:48:21 msaitoh Exp $");
 
 #include 
 #include 
@@ -188,7 +188,7 @@ static const char * const x86_features[]
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (18) Intel 0x0007 edx */
-	NULL, NULL, "avx512_4vnniw", "avx512_4fmaps", NULL, NULL, NULL, NULL,
+	NULL, NULL, "avx512_4vnniw", "avx512_4fmaps", "fsrm", NULL, NULL, NULL,
 	NULL, NULL, "md_clear", NULL, NULL, NULL, NULL, NULL,
 	NULL, NULL, "pconfig", NULL, NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL,



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

2020-01-13 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Tue Jan 14 01:41:37 UTC 2020

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

Log Message:
If "application processors" were skipped/disabled at boot time (due to
RB_MD1 being set), don't try to examine the featurebus info, since it
was never retrieved.  Addresses kern/54815

XXX pullup-9


To generate a diff of this commit:
cvs rdiff -u -r1.180 -r1.181 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.180 src/sys/arch/x86/x86/cpu.c:1.181
--- src/sys/arch/x86/x86/cpu.c:1.180	Wed Jan  8 15:47:50 2020
+++ src/sys/arch/x86/x86/cpu.c	Tue Jan 14 01:41:37 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.180 2020/01/08 15:47:50 ad Exp $	*/
+/*	$NetBSD: cpu.c,v 1.181 2020/01/14 01:41:37 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.180 2020/01/08 15:47:50 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.181 2020/01/14 01:41:37 pgoyette Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -540,6 +540,16 @@ cpu_rescan(device_t self, const char *if
 	struct cpufeature_attach_args cfaa;
 	struct cpu_info *ci = sc->sc_info;
 
+	/*
+	 * If we booted with RB_MD1 to disable multiprocessor, the
+	 * auto-configuration data still contains the additional
+	 * CPUs.   But their initialization was mostly bypassed
+	 * during attach, so we have to make sure we don't look at
+	 * their featurebus info, since it wasn't retrieved.
+	 */
+	if (ci == NULL)
+		return 0;
+
 	memset(, 0, sizeof(cfaa));
 	cfaa.ci = ci;
 



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

2020-01-08 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Wed Jan  8 15:47:50 UTC 2020

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

Log Message:
Make "mach cpu" in ddb show the IPL for each cpu.


To generate a diff of this commit:
cvs rdiff -u -r1.179 -r1.180 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.179 src/sys/arch/x86/x86/cpu.c:1.180
--- src/sys/arch/x86/x86/cpu.c:1.179	Fri Dec 20 21:05:34 2019
+++ src/sys/arch/x86/x86/cpu.c	Wed Jan  8 15:47:50 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.179 2019/12/20 21:05:34 ad Exp $	*/
+/*	$NetBSD: cpu.c,v 1.180 2020/01/08 15:47:50 ad Exp $	*/
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.179 2019/12/20 21:05:34 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.180 2020/01/08 15:47:50 ad Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -1014,14 +1014,14 @@ cpu_debug_dump(void)
 #endif
 			   "";
 
-	db_printf("addr		%sdev	id	flags	ipis	curlwp 		"
+	db_printf("addr		%sdev	id	flags	ipis	spl curlwp 		"
 		  "\n", sixtyfour64space);
 	for (CPU_INFO_FOREACH(cii, ci)) {
-		db_printf("%p	%s	%ld	%x	%x	%10p\n",
+		db_printf("%p	%s	%ld	%x	%x	%d  %10p\n",
 		ci,
 		ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
 		(long)ci->ci_cpuid,
-		ci->ci_flags, ci->ci_ipis,
+		ci->ci_flags, ci->ci_ipis, ci->ci_ilevel,
 		ci->ci_curlwp);
 	}
 }



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

2020-01-07 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Tue Jan  7 21:18:24 UTC 2020

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

Log Message:
pmap_ept_enter(): PTE -> EPT in two places.


To generate a diff of this commit:
cvs rdiff -u -r1.353 -r1.354 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.353 src/sys/arch/x86/x86/pmap.c:1.354
--- src/sys/arch/x86/x86/pmap.c:1.353	Sat Jan  4 22:49:20 2020
+++ src/sys/arch/x86/x86/pmap.c	Tue Jan  7 21:18:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.353 2020/01/04 22:49:20 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.354 2020/01/07 21:18:24 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.353 2020/01/04 22:49:20 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.354 2020/01/07 21:18:24 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -5281,7 +5281,7 @@ pmap_ept_enter(struct pmap *pmap, vaddr_
 	 */
 	if (((opte ^ npte) & (PTE_FRAME | EPT_R)) == 0) {
 		KASSERT(((opte ^ npte) & EPT_PVLIST) == 0);
-		if ((opte & PTE_PVLIST) != 0 && pve != NULL) {
+		if ((opte & EPT_PVLIST) != 0 && pve != NULL) {
 			KASSERT(pve->pve_pte.pte_ptp == ptp);
 			KASSERT(pve->pve_pte.pte_va == va);
 		}
@@ -5304,7 +5304,7 @@ pmap_ept_enter(struct pmap *pmap, vaddr_
 		}
 
 		(void)pmap_remove_pv(pmap, old_pp, ptp, va, pve);
-		old_pp->pp_attrs |= pmap_pte_to_pp_attrs(opte);
+		old_pp->pp_attrs |= pmap_ept_to_pp_attrs(opte);
 	}
 
 	/*



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

2020-01-02 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Thu Jan  2 21:17:14 UTC 2020

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

Log Message:
Remove unused argment to pmap_remove_pv().


To generate a diff of this commit:
cvs rdiff -u -r1.350 -r1.351 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.350 src/sys/arch/x86/x86/pmap.c:1.351
--- src/sys/arch/x86/x86/pmap.c:1.350	Thu Jan  2 19:20:02 2020
+++ src/sys/arch/x86/x86/pmap.c	Thu Jan  2 21:17:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.350 2020/01/02 19:20:02 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.351 2020/01/02 21:17:13 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.350 2020/01/02 19:20:02 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.351 2020/01/02 21:17:13 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1972,8 +1972,7 @@ pmap_enter_pv(struct pmap_page *pp, stru
  * => we return the removed pve
  */
 static struct pv_entry *
-pmap_remove_pv(struct pmap *pmap, struct pmap_page *pp, struct vm_page *ptp,
-vaddr_t va)
+pmap_remove_pv(struct pmap_page *pp, struct vm_page *ptp, vaddr_t va)
 {
 	struct pv_entry **hh;
 	struct pv_entry *pve;



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

2019-12-27 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Dec 28 00:38:08 UTC 2019

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

Log Message:
#include "opt_xen.h" so we can tell if we're in a XEN kernel.  We need
to know this in order to set module_machine correctly, which in turn is
needed to set the module_base path from which modules are loaded and
which provides the value of sysctl(8) variable kern.module.path

Thanks to jnemeth@ for pointing out the problem.


To generate a diff of this commit:
cvs rdiff -u -r1.133 -r1.134 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.133 src/sys/arch/x86/x86/x86_machdep.c:1.134
--- src/sys/arch/x86/x86/x86_machdep.c:1.133	Tue Dec  3 15:20:59 2019
+++ src/sys/arch/x86/x86/x86_machdep.c	Sat Dec 28 00:38:08 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.133 2019/12/03 15:20:59 riastradh Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.134 2019/12/28 00:38:08 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,13 +31,14 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.133 2019/12/03 15:20:59 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.134 2019/12/28 00:38:08 pgoyette Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
 #include "opt_splash.h"
 #include "opt_kaslr.h"
 #include "opt_svs.h"
+#include "opt_xen.h"
 
 #include 
 #include 



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

2019-12-22 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Dec 22 16:50:03 UTC 2019

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

Log Message:
Fix compile on !DIAGNOSTIC.


To generate a diff of this commit:
cvs rdiff -u -r1.148 -r1.149 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.148 src/sys/arch/x86/x86/intr.c:1.149
--- src/sys/arch/x86/x86/intr.c:1.148	Sun Dec 22 15:09:39 2019
+++ src/sys/arch/x86/x86/intr.c	Sun Dec 22 16:50:03 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.148 2019/12/22 15:09:39 thorpej Exp $	*/
+/*	$NetBSD: intr.c,v 1.149 2019/12/22 16:50:03 ad Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.148 2019/12/22 15:09:39 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.149 2019/12/22 16:50:03 ad Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1102,8 +1102,7 @@ intr_mask(struct intrhand *ih)
 		 * We can't take the cpu_lock in this case, and we must
 		 * therefore be extra careful.
 		 */
-		struct cpu_info * const ci = ih->ih_cpu;
-		KASSERT(ci == curcpu() || !mp_online);
+		KASSERT(ih->ih_cpu == curcpu() || !mp_online);
 		intr_mask_xcall(ih, (void *)(uintptr_t)true);
 		return;
 	}



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

2019-12-22 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Dec 22 15:15:20 UTC 2019

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

Log Message:
pmap_get_ptp(): the uvm_pagefree() call in the failure case can block too.
Pacify the assertion in pmap_unmap_ptes().

XXX Revisit and solve this chicken-and-egg problem in a more elegant way.

Reported-by: syzbot+24967905b8d173445...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.347 -r1.348 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.347 src/sys/arch/x86/x86/pmap.c:1.348
--- src/sys/arch/x86/x86/pmap.c:1.347	Sat Dec 21 13:00:24 2019
+++ src/sys/arch/x86/x86/pmap.c	Sun Dec 22 15:15:20 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.347 2019/12/21 13:00:24 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.348 2019/12/22 15:15:20 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.347 2019/12/21 13:00:24 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.348 2019/12/22 15:15:20 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -2120,8 +2120,6 @@ pmap_get_ptp(struct pmap *pmap, vaddr_t 
 			pt[i].new = true;
 			if (__predict_false(ncsw != lwp_pctr())) {
 /* uvm_pagealloc can block. */
-/* XXX silence assertion in pmap_unmap_ptes */
-pmap->pm_ncsw = lwp_pctr();
 error = EAGAIN;
 goto fail;
 			}
@@ -2195,9 +2193,10 @@ fail:
 		if (!pt[i].new) {
 			continue;
 		}
-		obj = >pm_obj[i - 2];
 		uvm_pagefree(pt[i].pg);
 	}
+	/* XXX silence assertion in pmap_unmap_ptes */
+	pmap->pm_ncsw = lwp_pctr();
 	return error;
 }
 



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

2019-12-16 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Mon Dec 16 19:20:45 UTC 2019

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

Log Message:
pmap_unmap_ptes(): ci_want_pmapload isn't dependant on TLB state.


To generate a diff of this commit:
cvs rdiff -u -r1.345 -r1.346 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.345 src/sys/arch/x86/x86/pmap.c:1.346
--- src/sys/arch/x86/x86/pmap.c:1.345	Sun Dec 15 20:33:22 2019
+++ src/sys/arch/x86/x86/pmap.c	Mon Dec 16 19:20:45 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.345 2019/12/15 20:33:22 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.346 2019/12/16 19:20:45 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.345 2019/12/15 20:33:22 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.346 2019/12/16 19:20:45 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -707,13 +707,16 @@ pmap_unmap_ptes(struct pmap *pmap, struc
 	 * Mark whatever's on the CPU now as lazy and unlock.
 	 * If the pmap was already installed, we are done.
 	 */
-	if (ci->ci_pmap != mypmap && ci->ci_tlbstate == TLBSTATE_VALID) {
-		ci->ci_tlbstate = TLBSTATE_LAZY;
+	if (ci->ci_pmap != mypmap) {
 		ci->ci_want_pmapload = (mypmap != pmap_kernel());
-	} else {
-		/*
-		 * This can happen when undoing after pmap_get_ptp blocked.
-		 */ 
+		if (ci->ci_tlbstate == TLBSTATE_VALID) {
+			ci->ci_tlbstate = TLBSTATE_LAZY;
+		} else {
+			/*
+			 * This can happen when undoing after pmap_get_ptp
+			 * blocked.
+			 */
+		}
 	}
 
 	/* Now safe to free PTPs, with the pmap still locked. */



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

2019-12-16 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Mon Dec 16 19:17:25 UTC 2019

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

Log Message:
Align the TLB packet precisely on the stack, and do 7 INVLPG since it's
what fits in a single line.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/x86/x86/x86_tlb.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_tlb.c
diff -u src/sys/arch/x86/x86/x86_tlb.c:1.12 src/sys/arch/x86/x86/x86_tlb.c:1.13
--- src/sys/arch/x86/x86/x86_tlb.c:1.12	Mon Dec  2 20:59:56 2019
+++ src/sys/arch/x86/x86/x86_tlb.c	Mon Dec 16 19:17:25 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_tlb.c,v 1.12 2019/12/02 20:59:56 pgoyette Exp $	*/
+/*	$NetBSD: x86_tlb.c,v 1.13 2019/12/16 19:17:25 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008-2019 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.12 2019/12/02 20:59:56 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.13 2019/12/16 19:17:25 ad Exp $");
 
 #include 
 #include 
@@ -66,10 +66,10 @@ __KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 
  * until the request is completed.  This keeps the cache line in the shared
  * state, and bus traffic to a minimum.
  *
- * On i386 the packet is 28 bytes in size.  On amd64 it's 52 bytes.
+ * On i386 the packet is 32 bytes in size.  On amd64 it's 60 bytes.
  */
 typedef struct {
-	uintptr_t		tp_va[6];
+	uintptr_t		tp_va[7];
 	uint8_t			tp_count;
 	uint8_t			tp_userpmap;
 	uint8_t			tp_global;
@@ -77,23 +77,14 @@ typedef struct {
 } pmap_tlb_packet_t;
 
 /*
- * Padded packet stored on the initiator's stack.
- */
-typedef struct {
-	uint8_t			ts_pad1[COHERENCY_UNIT];
-	pmap_tlb_packet_t	ts_tp;
-	uint8_t			ts_pad2[COHERENCY_UNIT];
-} pmap_tlb_stackbuf_t;
-
-/*
  * No more than N separate invlpg.
  *
- * Statistically, a value of six is big enough to cover the requested number
+ * Statistically, a value of 7 is big enough to cover the requested number
  * of pages in ~ 95% of the TLB shootdowns we are getting. We therefore rarely
  * reach the limit, and increasing it can actually reduce the performance due
  * to the high cost of invlpg.
  */
-#define	TP_MAXVA		6	/* for individual mappings */
+#define	TP_MAXVA		7	/* for individual mappings */
 #define	TP_ALLVA		255	/* special: shoot all mappings */
 
 /*
@@ -355,8 +346,8 @@ pmap_tlb_processpacket(volatile pmap_tlb
 void
 pmap_tlb_shootnow(void)
 {
-	volatile pmap_tlb_packet_t *tp;
-	volatile pmap_tlb_stackbuf_t ts;
+	volatile pmap_tlb_packet_t *tp, *ts;
+	volatile uint8_t stackbuf[128];
 	struct cpu_info *ci;
 	kcpuset_t *target;
 	u_int local, rcpucount;
@@ -405,11 +396,13 @@ pmap_tlb_shootnow(void)
 	 * against an interrupt on the current CPU trying the same.
 	 */
 	KASSERT(rcpucount < ncpu);
-	ts.ts_tp = *tp;
-	KASSERT(!ts.ts_tp.tp_done);
+	KASSERT(sizeof(*ts) <= (sizeof(stackbuf) / 2));
+	ts = (void *)roundup2((uintptr_t)stackbuf, (sizeof(stackbuf) / 2));
+	*ts = *tp;
+	KASSERT(!ts->tp_done);
 	while (atomic_cas_ptr(_tlb_packet, NULL,
-	__UNVOLATILE(_tp)) != NULL) {
-		KASSERT(pmap_tlb_packet != _tp);
+	__UNVOLATILE(ts)) != NULL) {
+		KASSERT(pmap_tlb_packet != ts);
 		/*
 		 * Don't bother with exponentional backoff, as the pointer
 		 * is in a dedicated cache line and only updated twice per
@@ -439,7 +432,7 @@ pmap_tlb_shootnow(void)
 	 */
 	pmap_tlb_pendcount = rcpucount;
 	pmap_tlb_evcnt.ev_count++;
-	pmap_tlb_processpacket(_tp, target);
+	pmap_tlb_processpacket(ts, target);
 
 	/*
 	 * Clear out the local CPU's buffer for the next user.  Once done,
@@ -461,7 +454,7 @@ pmap_tlb_shootnow(void)
 	 * perform local shootdown if needed, using our copy of the packet.
 	 */
 	if (local) {
-		pmap_tlb_invalidate(_tp);
+		pmap_tlb_invalidate(ts);
 	}
 
 	/*
@@ -470,7 +463,7 @@ pmap_tlb_shootnow(void)
 	 * CPU out will update it and only we are reading it).  No memory
 	 * barrier required due to prior stores - yay x86.
 	 */
-	while (!ts.ts_tp.tp_done) {
+	while (!ts->tp_done) {
 		x86_pause();
 	}
 }



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

2019-12-15 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Sun Dec 15 20:33:22 UTC 2019

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

Log Message:
- Share common code between pmap_load() and pmap_map_ptes().
- Make pmap_map_ptes() better tolerate recovery from blocking.


To generate a diff of this commit:
cvs rdiff -u -r1.344 -r1.345 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.344 src/sys/arch/x86/x86/pmap.c:1.345
--- src/sys/arch/x86/x86/pmap.c:1.344	Sun Dec 15 19:24:11 2019
+++ src/sys/arch/x86/x86/pmap.c	Sun Dec 15 20:33:22 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.344 2019/12/15 19:24:11 ad Exp $	*/
+/*	$NetBSD: pmap.c,v 1.345 2019/12/15 20:33:22 ad Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.344 2019/12/15 19:24:11 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.345 2019/12/15 20:33:22 ad Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -470,6 +470,7 @@ static void pmap_remove_ptes(struct pmap
 
 static void pmap_alloc_level(struct pmap *, vaddr_t, long *);
 
+static void pmap_load1(struct lwp *, struct pmap *, struct pmap *);
 static void pmap_reactivate(struct pmap *);
 
 /*
@@ -642,20 +643,12 @@ pmap_map_ptes(struct pmap *pmap, struct 
 		pmap_reactivate(pmap);
 	} else {
 		/*
-		 * Toss current pmap from CPU, but keep a reference to it.
-		 * The reference will be dropped by pmap_unmap_ptes().
-		 * Can happen if we block during exit().
+		 * Toss current pmap from CPU and install new pmap, but keep
+		 * a reference to the old one - dropping the reference can
+		 * block, so we'll defer to pmap_unmap_ptes().
 		 */
-		const cpuid_t cid = cpu_index(ci);
-
-		kcpuset_atomic_clear(curpmap->pm_cpus, cid);
-		kcpuset_atomic_clear(curpmap->pm_kernel_cpus, cid);
-		ci->ci_pmap = pmap;
-		ci->ci_tlbstate = TLBSTATE_VALID;
-		ci->ci_want_pmapload = 0;
-		kcpuset_atomic_set(pmap->pm_cpus, cid);
-		kcpuset_atomic_set(pmap->pm_kernel_cpus, cid);
-		cpu_load_pmap(pmap, curpmap);
+		pmap_reference(pmap);
+		pmap_load1(l, pmap, curpmap);
 	}
 	pmap->pm_ncsw = lwp_pctr();
 	*pmap2 = curpmap;
@@ -714,7 +707,7 @@ pmap_unmap_ptes(struct pmap *pmap, struc
 	 * Mark whatever's on the CPU now as lazy and unlock.
 	 * If the pmap was already installed, we are done.
 	 */
-	if (ci->ci_tlbstate == TLBSTATE_VALID) {
+	if (ci->ci_pmap != mypmap && ci->ci_tlbstate == TLBSTATE_VALID) {
 		ci->ci_tlbstate = TLBSTATE_LAZY;
 		ci->ci_want_pmapload = (mypmap != pmap_kernel());
 	} else {
@@ -722,21 +715,17 @@ pmap_unmap_ptes(struct pmap *pmap, struc
 		 * This can happen when undoing after pmap_get_ptp blocked.
 		 */ 
 	}
+
 	/* Now safe to free PTPs, with the pmap still locked. */
 	if (ptp_tofree != NULL) {
 		pmap_freepages(pmap, ptp_tofree);
 	}
 	mutex_exit(>pm_lock);
-	if (pmap == pmap2) {
-		return;
-	}
 
-	/*
-	 * We installed another pmap on the CPU.  Grab a reference to
-	 * it and leave in place.  Toss the evicted pmap (can block).
-	 */
-	pmap_reference(pmap);
-	pmap_destroy(pmap2);
+	/* Toss the pmap we evicted earlier (can block). */
+	if (pmap != pmap2) {
+		pmap_destroy(pmap2);
+	}
 }
 
 inline static void
@@ -2884,8 +2873,6 @@ pmap_load(void)
 	struct cpu_info *ci;
 	struct pmap *pmap, *oldpmap;
 	struct lwp *l;
-	struct pcb *pcb;
-	cpuid_t cid;
 	uint64_t ncsw;
 
 	kpreempt_disable();
@@ -2912,7 +2899,6 @@ pmap_load(void)
 	pmap = vm_map_pmap(>l_proc->p_vmspace->vm_map);
 	KASSERT(pmap != pmap_kernel());
 	oldpmap = ci->ci_pmap;
-	pcb = lwp_getpcb(l);
 
 	if (pmap == oldpmap) {
 		pmap_reactivate(pmap);
@@ -2926,8 +2912,41 @@ pmap_load(void)
 	 */
 
 	pmap_reference(pmap);
+	pmap_load1(l, pmap, oldpmap);
+	ci->ci_want_pmapload = 0;
+
+	/*
+	 * we're now running with the new pmap.  drop the reference
+	 * to the old pmap.  if we block, we need to go around again.
+	 */
+
+	pmap_destroy(oldpmap);
+	__insn_barrier();
+	if (l->l_ncsw != ncsw) {
+		goto retry;
+	}
+
+	kpreempt_enable();
+}
 
+/*
+ * pmap_load1: the guts of pmap load, shared by pmap_map_ptes() and
+ * pmap_load().  It's critically important that this function does not
+ * block.
+ */
+static void
+pmap_load1(struct lwp *l, struct pmap *pmap, struct pmap *oldpmap)
+{
+	struct cpu_info *ci;
+	struct pcb *pcb;
+	cpuid_t cid;
+
+	KASSERT(kpreempt_disabled());
+
+	pcb = lwp_getpcb(l);
+	ci = l->l_cpu;
 	cid = cpu_index(ci);
+
 	kcpuset_atomic_clear(oldpmap->pm_cpus, cid);
 	kcpuset_atomic_clear(oldpmap->pm_kernel_cpus, cid);
 
@@ -2970,21 +2989,6 @@ pmap_load(void)
 	lldt(pmap->pm_ldt_sel);
 
 	cpu_load_pmap(pmap, oldpmap);
-
-	ci->ci_want_pmapload = 0;
-
-	/*
-	 * we're now running with the new pmap.  drop the reference
-	 * to the old pmap.  if we block, we need to go around again.
-	 */
-
-	

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

2019-12-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Dec 12 16:49:20 UTC 2019

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

Log Message:
Check CPUID.IBRS in addition to ARCH_CAP.IBRS_ALL. For clarity, and also
because VirtualBox clears the former but forgets to clear the latter (which
makes us hit a #GP on RDMSR).


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/sys/arch/x86/x86/spectre.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/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.31 src/sys/arch/x86/x86/spectre.c:1.32
--- src/sys/arch/x86/x86/spectre.c:1.31	Tue Nov 12 18:00:13 2019
+++ src/sys/arch/x86/x86/spectre.c	Thu Dec 12 16:49:20 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.31 2019/11/12 18:00:13 maxv Exp $	*/
+/*	$NetBSD: spectre.c,v 1.32 2019/12/12 16:49:20 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018-2019 NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.31 2019/11/12 18:00:13 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.32 2019/12/12 16:49:20 maxv Exp $");
 
 #include "opt_spectre.h"
 
@@ -126,20 +126,20 @@ v2_detect_method(void)
 		if (cpuid_level >= 7) {
 			x86_cpuid(7, descs);
 
-			if (descs[3] & CPUID_SEF_ARCH_CAP) {
-msr = rdmsr(MSR_IA32_ARCH_CAPABILITIES);
-if (msr & IA32_ARCH_IBRS_ALL) {
-	v2_mitigation_method =
-	V2_MITIGATION_INTEL_ENHANCED_IBRS;
-	return;
+			if (descs[3] & CPUID_SEF_IBRS) {
+if (descs[3] & CPUID_SEF_ARCH_CAP) {
+	msr = rdmsr(MSR_IA32_ARCH_CAPABILITIES);
+	if (msr & IA32_ARCH_IBRS_ALL) {
+		v2_mitigation_method =
+		V2_MITIGATION_INTEL_ENHANCED_IBRS;
+		return;
+	}
 }
-			}
 #ifdef __x86_64__
-			if (descs[3] & CPUID_SEF_IBRS) {
 v2_mitigation_method = V2_MITIGATION_INTEL_IBRS;
 return;
-			}
 #endif
+			}
 		}
 		v2_mitigation_method = V2_MITIGATION_NONE;
 	} else if (cpu_vendor == CPUVENDOR_AMD) {



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

2019-12-03 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Tue Dec  3 11:50:45 UTC 2019

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

Log Message:
Make sure the assignment to "idepth" is done inside the loop to prevent
preemption between loop end and dereference of "l_cpu->ci_depth".


To generate a diff of this commit:
cvs rdiff -u -r1.131 -r1.132 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.131 src/sys/arch/x86/x86/x86_machdep.c:1.132
--- src/sys/arch/x86/x86/x86_machdep.c:1.131	Tue Dec  3 11:50:16 2019
+++ src/sys/arch/x86/x86/x86_machdep.c	Tue Dec  3 11:50:45 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.131 2019/12/03 11:50:16 hannken Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.132 2019/12/03 11:50:45 hannken Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.131 2019/12/03 11:50:16 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.132 2019/12/03 11:50:45 hannken Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -350,7 +350,7 @@ bool
 cpu_intr_p(void)
 {
 	uint64_t ncsw;
-	int idepth;
+	volatile int idepth;
 	lwp_t *l;
 
 	l = curlwp;



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

2019-12-03 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Tue Dec  3 11:50:16 UTC 2019

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

Log Message:
Make cpu_intr_p() work with "curlwp->l_cpu == NULL" and
assert "curlwp == " in this case.

Prevents crash during early boot with "options LOCKDEBUG".


To generate a diff of this commit:
cvs rdiff -u -r1.130 -r1.131 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.130 src/sys/arch/x86/x86/x86_machdep.c:1.131
--- src/sys/arch/x86/x86/x86_machdep.c:1.130	Sun Dec  1 14:52:14 2019
+++ src/sys/arch/x86/x86/x86_machdep.c	Tue Dec  3 11:50:16 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.130 2019/12/01 14:52:14 ad Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.131 2019/12/03 11:50:16 hannken Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.130 2019/12/01 14:52:14 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.131 2019/12/03 11:50:16 hannken Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -354,6 +354,10 @@ cpu_intr_p(void)
 	lwp_t *l;
 
 	l = curlwp;
+	if (__predict_false(l->l_cpu == NULL)) {
+		KASSERT(l == );
+		return false;
+	}
 	do {
 		ncsw = l->l_ncsw;
 		idepth = l->l_cpu->ci_idepth;



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

2019-12-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Dec  3 04:57:25 UTC 2019

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

Log Message:
Skip fences in bus_space_barrier on I/O space.

I/O operations are issued in program order.  Not that I/O operations
are usually a performance bottleneck anyway, but maybe it is slightly
cheaper to avoid stalling on store buffers or pending loads, and
there's very little cost to the skipping criterion here.


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

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

Modified files:

Index: src/sys/arch/x86/x86/bus_space.c
diff -u src/sys/arch/x86/x86/bus_space.c:1.42 src/sys/arch/x86/x86/bus_space.c:1.43
--- src/sys/arch/x86/x86/bus_space.c:1.42	Mon Dec  2 08:33:52 2019
+++ src/sys/arch/x86/x86/bus_space.c	Tue Dec  3 04:57:25 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_space.c,v 1.42 2019/12/02 08:33:52 riastradh Exp $	*/
+/*	$NetBSD: bus_space.c,v 1.43 2019/12/03 04:57:25 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.42 2019/12/02 08:33:52 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.43 2019/12/03 04:57:25 riastradh Exp $");
 
 #include 
 #include 
@@ -878,6 +878,10 @@ bus_space_barrier(bus_space_tag_t tag, b
 		  bus_size_t offset, bus_size_t len, int flags)
 {
 
+	/* I/O instructions always happen in program order.  */
+	if (x86_bus_space_is_io(tag))
+		return;
+
 	/*
 	 * For default mappings, which are mapped with UC-type memory
 	 * regions, all loads and stores are issued in program order.



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

2019-12-02 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Mon Dec  2 20:59:56 UTC 2019

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

Log Message:
Fix tyo in comment


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/x86/x86/x86_tlb.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_tlb.c
diff -u src/sys/arch/x86/x86/x86_tlb.c:1.11 src/sys/arch/x86/x86/x86_tlb.c:1.12
--- src/sys/arch/x86/x86/x86_tlb.c:1.11	Mon Dec  2 19:49:12 2019
+++ src/sys/arch/x86/x86/x86_tlb.c	Mon Dec  2 20:59:56 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_tlb.c,v 1.11 2019/12/02 19:49:12 ad Exp $	*/
+/*	$NetBSD: x86_tlb.c,v 1.12 2019/12/02 20:59:56 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2008-2019 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.11 2019/12/02 19:49:12 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.12 2019/12/02 20:59:56 pgoyette Exp $");
 
 #include 
 #include 
@@ -435,7 +435,7 @@ pmap_tlb_shootnow(void)
 	/*
 	 * Ownership of the global pointer provides serialization of the
 	 * update to the count and the event counter.  With those values
-	 * upated, start shootdowns on remote CPUs.
+	 * updated, start shootdowns on remote CPUs.
 	 */
 	pmap_tlb_pendcount = rcpucount;
 	pmap_tlb_evcnt.ev_count++;



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

2019-12-02 Thread Andrew Doran
Module Name:src
Committed By:   ad
Date:   Mon Dec  2 19:49:12 UTC 2019

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

Log Message:
Fix a hard hang with Xen MP.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/x86/x86/x86_tlb.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_tlb.c
diff -u src/sys/arch/x86/x86/x86_tlb.c:1.10 src/sys/arch/x86/x86/x86_tlb.c:1.11
--- src/sys/arch/x86/x86/x86_tlb.c:1.10	Fri Nov 22 20:04:03 2019
+++ src/sys/arch/x86/x86/x86_tlb.c	Mon Dec  2 19:49:12 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_tlb.c,v 1.10 2019/11/22 20:04:03 ad Exp $	*/
+/*	$NetBSD: x86_tlb.c,v 1.11 2019/12/02 19:49:12 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008-2019 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.10 2019/11/22 20:04:03 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.11 2019/12/02 19:49:12 ad Exp $");
 
 #include 
 #include 
@@ -309,6 +309,8 @@ pmap_tlb_processpacket(volatile pmap_tlb
 
 	/* Remote CPUs have been synchronously flushed. */
 	pmap_tlb_pendcount = 0;
+	pmap_tlb_packet = NULL;
+	tp->tp_done = 1;
 #endif /* MULTIPROCESSOR */
 }
 
@@ -437,7 +439,7 @@ pmap_tlb_shootnow(void)
 	 */
 	pmap_tlb_pendcount = rcpucount;
 	pmap_tlb_evcnt.ev_count++;
-	pmap_tlb_processpacket(tp, target);
+	pmap_tlb_processpacket(_tp, target);
 
 	/*
 	 * Clear out the local CPU's buffer for the next user.  Once done,



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

2019-12-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Dec  2 08:33:52 UTC 2019

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

Log Message:
Use LFENCE/SFENCE/MFENCE in x86 bus_space_barrier.

These are needed for BUS_SPACE_MAP_PREFETCHABLE mappings.  On x86,
these are WC-type memory regions, which means -- unlike normal
WB-type memory regions -- loads can be reordered with loads,
requiring LFENCE, and stores can be reordered with stores, requiring
SFENCE.

Reference: AMD64 Architecture Programmer's Manual, Volume 2: System
Programming, Sec. 7.4.1 `Memory Barrier Interaction with Memory
Types', Table 7-3 `Memory Access Ordering Rules'.


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

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

Modified files:

Index: src/sys/arch/x86/x86/bus_space.c
diff -u src/sys/arch/x86/x86/bus_space.c:1.41 src/sys/arch/x86/x86/bus_space.c:1.42
--- src/sys/arch/x86/x86/bus_space.c:1.41	Mon Feb 11 14:59:33 2019
+++ src/sys/arch/x86/x86/bus_space.c	Mon Dec  2 08:33:52 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_space.c,v 1.41 2019/02/11 14:59:33 cherry Exp $	*/
+/*	$NetBSD: bus_space.c,v 1.42 2019/12/02 08:33:52 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.41 2019/02/11 14:59:33 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.42 2019/12/02 08:33:52 riastradh Exp $");
 
 #include 
 #include 
@@ -878,7 +878,39 @@ bus_space_barrier(bus_space_tag_t tag, b
 		  bus_size_t offset, bus_size_t len, int flags)
 {
 
-	/* Function call is enough to prevent reordering of loads. */
+	/*
+	 * For default mappings, which are mapped with UC-type memory
+	 * regions, all loads and stores are issued in program order.
+	 *
+	 * For BUS_SPACE_MAP_PREFETCHABLE mappings, which are mapped
+	 * with WC-type memory regions, loads and stores may be issued
+	 * out of order, potentially requiring any of the three x86
+	 * fences -- LFENCE, SFENCE, MFENCE.
+	 *
+	 * For BUS_SPACE_MAP_CACHEABLE mappings, which are mapped with
+	 * WB-type memory regions (like normal memory), store/load may
+	 * be reordered to load/store, potentially requiring MFENCE.
+	 *
+	 * We can't easily tell here how the region was mapped (without
+	 * consulting the page tables), so just issue the fence
+	 * unconditionally.  Chances are either it's necessary or the
+	 * cost is small in comparison to device register I/O.
+	 */
+	switch (flags) {
+	case 0:
+		break;
+	case BUS_SPACE_BARRIER_READ:
+		x86_lfence();
+		break;
+	case BUS_SPACE_BARRIER_WRITE:
+		x86_sfence();
+		break;
+	case BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE:
+		x86_mfence();
+		break;
+	default:
+		panic("unknown bus space barrier: 0x%x", (unsigned)flags);
+	}
 }
 
 void *



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

2019-05-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 10 18:21:01 UTC 2019

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

Log Message:
Clean up, and add sanity checks on the microcode lengths.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/x86/x86/cpu_ucode_intel.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_ucode_intel.c
diff -u src/sys/arch/x86/x86/cpu_ucode_intel.c:1.16 src/sys/arch/x86/x86/cpu_ucode_intel.c:1.17
--- src/sys/arch/x86/x86/cpu_ucode_intel.c:1.16	Thu May  9 18:53:14 2019
+++ src/sys/arch/x86/x86/cpu_ucode_intel.c	Fri May 10 18:21:01 2019
@@ -1,10 +1,11 @@
-/* $NetBSD: cpu_ucode_intel.c,v 1.16 2019/05/09 18:53:14 maxv Exp $ */
+/* $NetBSD: cpu_ucode_intel.c,v 1.17 2019/05/10 18:21:01 maxv Exp $ */
+
 /*
- * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * Copyright (c) 2012, 2019 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by Matthias Drochner.
+ * by Matthias Drochner and Maxime Villard.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.16 2019/05/09 18:53:14 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.17 2019/05/10 18:21:01 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_xen.h"
@@ -88,8 +89,8 @@ cpu_ucode_intel_firmware_open(firmware_h
 {
 	const char *fw_path = "cpu_x86_intel1";
 	uint32_t ucodeversion, cpu_signature;
-	int platformid;
 	char cpuspec[11];
+	int platformid;
 
 	if (fwname != NULL && fwname[0] != '\0')
 		return firmware_open(fw_path, fwname, fwh);
@@ -106,71 +107,68 @@ cpu_ucode_intel_firmware_open(firmware_h
 }
 
 #ifndef XEN
-/* Check header version and checksum */
 static int
-cpu_ucode_intel_verify(struct intel1_ucode_header *buf)
+cpu_ucode_intel_verify(struct cpu_ucode_softc *sc,
+struct intel1_ucode_header *buf)
 {
-	uint32_t data_size, total_size, payload_size, extended_table_size;
-#if 0 /* not yet */
-	struct intel1_ucode_ext_table *ext_table;
-	struct intel1_ucode_proc_signature *ext_psig;
-#endif
+	uint32_t data_size, total_size, payload_size, ext_size;
 	uint32_t sum;
 	int i;
-	
+
 	if ((buf->uh_header_ver != 1) || (buf->uh_loader_rev != 1))
 		return EINVAL;
 
-	/* Data size */
-	if (buf->uh_data_size == 0)
+	/*
+	 * Data size.
+	 */
+	if (buf->uh_data_size == 0) {
 		data_size = 2000;
-	else
+	} else {
 		data_size = buf->uh_data_size;
-
-	if ((data_size % 4) != 0) {
-		/* Wrong size */
-		return EINVAL;
 	}
+	if ((data_size % 4) != 0)
+		return EINVAL;
+	if (data_size > sc->sc_blobsize)
+		return EINVAL;
 
-	/* Total size */
-	if (buf->uh_total_size == 0)
+	/*
+	 * Total size.
+	 */
+	if (buf->uh_total_size == 0) {
 		total_size = data_size + 48;
-	else
+	} else {
 		total_size = buf->uh_total_size;
-
-	if ((total_size % 1024) != 0) {
-		/* Wrong size */
-		return EINVAL;
 	}
+	if ((total_size % 1024) != 0)
+		return EINVAL;
+	if (total_size > sc->sc_blobsize)
+		return EINVAL;
 
+	/*
+	 * Payload size.
+	 */
 	payload_size = data_size + 48;
-
-	/* Extended table size */
-	extended_table_size = total_size - payload_size;
+	if (payload_size > sc->sc_blobsize)
+		return EINVAL;
 
 	/*
-	 * Verify checksum of update data and header
-	 * (exclude extended signature).
+	 * Verify checksum of update data and header. Exclude extended
+	 * signature.
 	 */
 	sum = 0;
-	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
+	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) {
 		sum += *((uint32_t *)buf + i);
-	if (sum != 0) {
-		/* Checksum mismatch */
-		return EINVAL;
 	}
+	if (sum != 0)
+		return EINVAL;
 
-	if (extended_table_size == 0)
-		return 0;
-
-#if 0
-	/* Verify extended signature's checksum */
-	ext_table = (void *)buf + payload_size;
-	ext_psig = (void *)ext_table + sizeof(struct intel1_ucode_ext_table);
-	printf("ext_table = %p, extsig = %p\n", ext_table, ext_psig);
-#else
-	printf("This image has extended signature table.");
-#endif
+	/*
+	 * Extended table size. Ignored for now.
+	 */
+	ext_size = total_size - payload_size;
+	if (ext_size > 0) {
+		printf("This image has extended signature table.");
+	}
 
 	return 0;
 }
@@ -179,29 +177,28 @@ int
 cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
 {
 	uint32_t ucodetarget, oucodeversion, nucodeversion;
-	int platformid, cpuid;
 	struct intel1_ucode_header *uh;
-	void *uha;
+	int platformid, cpuid, error;
 	size_t newbufsize = 0;
-	int rv = 0;
+	void *uha;
 
-	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
-	|| cpuno != CPU_UCODE_CURRENT_CPU)
+	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1 ||
+	cpuno != CPU_UCODE_CURRENT_CPU)
 		return EINVAL;
 
-	uh = (struct intel1_ucode_header 

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

2019-05-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May  9 18:53:14 UTC 2019

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

Log Message:
Invalidate the cache before updating the microcode. Some platforms require
this. Seen in Illumos and FreeBSD.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/x86/x86/cpu_ucode_intel.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_ucode_intel.c
diff -u src/sys/arch/x86/x86/cpu_ucode_intel.c:1.15 src/sys/arch/x86/x86/cpu_ucode_intel.c:1.16
--- src/sys/arch/x86/x86/cpu_ucode_intel.c:1.15	Sun Jan 27 02:08:39 2019
+++ src/sys/arch/x86/x86/cpu_ucode_intel.c	Thu May  9 18:53:14 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_ucode_intel.c,v 1.15 2019/01/27 02:08:39 pgoyette Exp $ */
+/* $NetBSD: cpu_ucode_intel.c,v 1.16 2019/05/09 18:53:14 maxv Exp $ */
 /*
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.15 2019/01/27 02:08:39 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.16 2019/05/09 18:53:14 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_xen.h"
@@ -213,7 +213,14 @@ cpu_ucode_intel_apply(struct cpu_ucode_s
 		rv = EEXIST; /* ??? */
 		goto out;
 	}
+
+	/*
+	 * Perform update. On some platforms a cache invalidation is
+	 * required.
+	 */
+	wbinvd();
 	wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
+
 	intel_getcurrentucode(, );
 	cpuid = curcpu()->ci_index;
 



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

2019-04-27 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Apr 27 10:40:17 UTC 2019

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

Log Message:
Add support for EnhancedIBRS, a more performant mitigation for SpectreV2,
available on future CPUs (or maybe they already exist now...).


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/sys/arch/x86/x86/spectre.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/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.25 src/sys/arch/x86/x86/spectre.c:1.26
--- src/sys/arch/x86/x86/spectre.c:1.25	Sat Mar 23 10:02:05 2019
+++ src/sys/arch/x86/x86/spectre.c	Sat Apr 27 10:40:17 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.25 2019/03/23 10:02:05 maxv Exp $	*/
+/*	$NetBSD: spectre.c,v 1.26 2019/04/27 10:40:17 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.25 2019/03/23 10:02:05 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.26 2019/04/27 10:40:17 maxv Exp $");
 
 #include "opt_spectre.h"
 
@@ -54,7 +54,8 @@ __KERNEL_RCSID(0, "$NetBSD: spectre.c,v 
 enum v2_mitigation {
 	V2_MITIGATION_NONE,
 	V2_MITIGATION_AMD_DIS_IND,
-	V2_MITIGATION_INTEL_IBRS
+	V2_MITIGATION_INTEL_IBRS,
+	V2_MITIGATION_INTEL_ENHANCED_IBRS
 };
 
 enum v4_mitigation {
@@ -102,6 +103,9 @@ v2_set_name(void)
 		case V2_MITIGATION_INTEL_IBRS:
 			strlcat(name, "[Intel IBRS]", sizeof(name));
 			break;
+		case V2_MITIGATION_INTEL_ENHANCED_IBRS:
+			strlcat(name, "[Intel Enhanced IBRS]", sizeof(name));
+			break;
 		default:
 			panic("%s: impossible", __func__);
 		}
@@ -116,20 +120,26 @@ v2_detect_method(void)
 {
 	struct cpu_info *ci = curcpu();
 	u_int descs[4];
+	uint64_t msr;
 
 	if (cpu_vendor == CPUVENDOR_INTEL) {
 		if (cpuid_level >= 7) {
 			x86_cpuid(7, descs);
-			if (descs[3] & CPUID_SEF_IBRS) {
-/* descs[3] = %edx */
+
+			if (descs[3] & CPUID_SEF_ARCH_CAP) {
+msr = rdmsr(MSR_IA32_ARCH_CAPABILITIES);
+if (msr & IA32_ARCH_IBRS_ALL) {
+	v2_mitigation_method =
+	V2_MITIGATION_INTEL_ENHANCED_IBRS;
+	return;
+}
+			}
 #ifdef __x86_64__
+			if (descs[3] & CPUID_SEF_IBRS) {
 v2_mitigation_method = V2_MITIGATION_INTEL_IBRS;
-#else
-/* IBRS not supported on i386. */
-v2_mitigation_method = V2_MITIGATION_NONE;
-#endif
 return;
 			}
+#endif
 		}
 		v2_mitigation_method = V2_MITIGATION_NONE;
 	} else if (cpu_vendor == CPUVENDOR_AMD) {
@@ -239,6 +249,15 @@ mitigation_v2_apply_cpu(struct cpu_info 
 			wrmsr(MSR_IA32_SPEC_CTRL, 0);
 		}
 		break;
+	case V2_MITIGATION_INTEL_ENHANCED_IBRS:
+		msr = rdmsr(MSR_IA32_SPEC_CTRL);
+		if (enabled) {
+			msr |= IA32_SPEC_CTRL_IBRS;
+		} else {
+			msr &= ~IA32_SPEC_CTRL_IBRS;
+		}
+		wrmsr(MSR_IA32_SPEC_CTRL, msr);
+		break;
 	case V2_MITIGATION_AMD_DIS_IND:
 		msr = rdmsr(MSR_IC_CFG);
 		if (enabled) {
@@ -302,6 +321,7 @@ mitigation_v2_change(bool enabled)
 		return EOPNOTSUPP;
 	case V2_MITIGATION_AMD_DIS_IND:
 	case V2_MITIGATION_INTEL_IBRS:
+	case V2_MITIGATION_INTEL_ENHANCED_IBRS:
 		/* Initialize the barriers */
 		ibrs_cpu_barrier1 = ncpu;
 		ibrs_cpu_barrier2 = ncpu;



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

2019-04-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Apr 21 06:37:21 UTC 2019

Modified Files:
src/sys/arch/x86/x86: bus_dma.c db_memrw.c kgdb_machdep.c svs.c
x86_tlb.c

Log Message:
Rename the PTE bits.


To generate a diff of this commit:
cvs rdiff -u -r1.77 -r1.78 src/sys/arch/x86/x86/bus_dma.c
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/x86/x86/db_memrw.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/x86/kgdb_machdep.c
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/x86/x86/svs.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/x86/x86/x86_tlb.c

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

Modified files:

Index: src/sys/arch/x86/x86/bus_dma.c
diff -u src/sys/arch/x86/x86/bus_dma.c:1.77 src/sys/arch/x86/x86/bus_dma.c:1.78
--- src/sys/arch/x86/x86/bus_dma.c:1.77	Mon Jul 31 19:29:19 2017
+++ src/sys/arch/x86/x86/bus_dma.c	Sun Apr 21 06:37:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.77 2017/07/31 19:29:19 jdolecek Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.78 2019/04/21 06:37:21 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2007 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.77 2017/07/31 19:29:19 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.78 2019/04/21 06:37:21 maxv Exp $");
 
 /*
  * The following is included because _bus_dma_uiomove is derived from
@@ -1176,8 +1176,8 @@ _bus_dmamem_unmap(bus_dma_tag_t t, void 
 	for (va = sva; va < eva; va += PAGE_SIZE) {
 		pte = kvtopte(va);
 		opte = *pte;
-		if ((opte & PG_N) != 0)
-			pmap_pte_clearbits(pte, PG_N);
+		if ((opte & PTE_PCD) != 0)
+			pmap_pte_clearbits(pte, PTE_PCD);
 	}
 	pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
 	pmap_update(pmap_kernel());

Index: src/sys/arch/x86/x86/db_memrw.c
diff -u src/sys/arch/x86/x86/db_memrw.c:1.10 src/sys/arch/x86/x86/db_memrw.c:1.11
--- src/sys/arch/x86/x86/db_memrw.c:1.10	Sat Mar  9 08:42:26 2019
+++ src/sys/arch/x86/x86/db_memrw.c	Sun Apr 21 06:37:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_memrw.c,v 1.10 2019/03/09 08:42:26 maxv Exp $	*/
+/*	$NetBSD: db_memrw.c,v 1.11 2019/04/21 06:37:21 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.10 2019/03/09 08:42:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.11 2019/04/21 06:37:21 maxv Exp $");
 
 #include 
 #include 
@@ -159,7 +159,7 @@ db_write_text(vaddr_t addr, size_t size,
 		 * with this mapping and subtract it from the
 		 * total size.
 		 */
-		if (pte & PG_PS)
+		if (pte & PTE_PS)
 			limit = NBPD_L2 - (addr & (NBPD_L2 - 1));
 		else
 			limit = PAGE_SIZE - (addr & PGOFSET);

Index: src/sys/arch/x86/x86/kgdb_machdep.c
diff -u src/sys/arch/x86/x86/kgdb_machdep.c:1.4 src/sys/arch/x86/x86/kgdb_machdep.c:1.5
--- src/sys/arch/x86/x86/kgdb_machdep.c:1.4	Sat Mar  9 08:42:26 2019
+++ src/sys/arch/x86/x86/kgdb_machdep.c	Sun Apr 21 06:37:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kgdb_machdep.c,v 1.4 2019/03/09 08:42:26 maxv Exp $	*/
+/*	$NetBSD: kgdb_machdep.c,v 1.5 2019/04/21 06:37:21 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997, 2017 The NetBSD Foundation, Inc.
@@ -56,7 +56,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kgdb_machdep.c,v 1.4 2019/03/09 08:42:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kgdb_machdep.c,v 1.5 2019/04/21 06:37:21 maxv Exp $");
 
 #include "opt_ddb.h"
 
@@ -93,8 +93,8 @@ kgdb_acc(vaddr_t va, size_t len)
 			pte = kvtopte(va);
 		if ((*pte & PTE_P) == 0)
 			return 0;
-		if (*pte & PG_PS)
-			va = (va & PG_LGFRAME) + NBPD_L2;
+		if (*pte & PTE_PS)
+			va = (va & PTE_LGFRAME) + NBPD_L2;
 		else
 			va += PAGE_SIZE;
 	} while (va < last_va);

Index: src/sys/arch/x86/x86/svs.c
diff -u src/sys/arch/x86/x86/svs.c:1.24 src/sys/arch/x86/x86/svs.c:1.25
--- src/sys/arch/x86/x86/svs.c:1.24	Sat Mar 23 10:02:05 2019
+++ src/sys/arch/x86/x86/svs.c	Sun Apr 21 06:37:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: svs.c,v 1.24 2019/03/23 10:02:05 maxv Exp $	*/
+/*	$NetBSD: svs.c,v 1.25 2019/04/21 06:37:21 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.24 2019/03/23 10:02:05 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.25 2019/04/21 06:37:21 maxv Exp $");
 
 #include "opt_svs.h"
 
@@ -259,7 +259,7 @@ svs_tree_add(struct cpu_info *ci, vaddr_
 			dstpde[pidx] = PTE_P | PTE_W | pa;
 		}
 
-		pa = (paddr_t)(dstpde[pidx] & PG_FRAME);
+		pa = (paddr_t)(dstpde[pidx] & PTE_FRAME);
 		dstpde = (pd_entry_t *)PMAP_DIRECT_MAP(pa);
 	}
 
@@ -287,10 +287,10 @@ svs_page_add(struct cpu_info *ci, vaddr_
 	if (!pmap_valid_entry(srcpde[idx])) {
 		panic("%s: L2 page not mapped", __func__);
 	}
-	if (srcpde[idx] & PG_PS) {
-		pa = srcpde[idx] & PG_2MFRAME;
+	if (srcpde[idx] & PTE_PS) {
+		pa = srcpde[idx] & PTE_2MFRAME;
 		pa += (paddr_t)(va % NBPD_L2);
-		pde = (srcpde[idx] & 

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

2019-03-23 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Mar 23 10:02:05 UTC 2019

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

Log Message:
In fact, xc_broadcast also applies to offline CPUs, so we don't need to
make sure each CPU is online. Remove the checks, I suspect they weren't
totally correct by the way.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/x86/x86/spectre.c
cvs rdiff -u -r1.23 -r1.24 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/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.24 src/sys/arch/x86/x86/spectre.c:1.25
--- src/sys/arch/x86/x86/spectre.c:1.24	Sun Jan 27 05:08:05 2019
+++ src/sys/arch/x86/x86/spectre.c	Sat Mar 23 10:02:05 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.24 2019/01/27 05:08:05 dholland Exp $	*/
+/*	$NetBSD: spectre.c,v 1.25 2019/03/23 10:02:05 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.24 2019/01/27 05:08:05 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.25 2019/03/23 10:02:05 maxv Exp $");
 
 #include "opt_spectre.h"
 
@@ -292,31 +292,13 @@ mitigation_v2_change_cpu(void *arg1, voi
 static int
 mitigation_v2_change(bool enabled)
 {
-	struct cpu_info *ci = NULL;
-	CPU_INFO_ITERATOR cii;
 	uint64_t xc;
 
 	v2_detect_method();
 
-	mutex_enter(_lock);
-
-	/*
-	 * We expect all the CPUs to be online.
-	 */
-	for (CPU_INFO_FOREACH(cii, ci)) {
-		struct schedstate_percpu *spc = >ci_schedstate;
-		if (spc->spc_flags & SPCF_OFFLINE) {
-			printf("[!] cpu%d offline, SpectreV2 not changed\n",
-			cpu_index(ci));
-			mutex_exit(_lock);
-			return EOPNOTSUPP;
-		}
-	}
-
 	switch (v2_mitigation_method) {
 	case V2_MITIGATION_NONE:
 		printf("[!] No mitigation available\n");
-		mutex_exit(_lock);
 		return EOPNOTSUPP;
 	case V2_MITIGATION_AMD_DIS_IND:
 	case V2_MITIGATION_INTEL_IBRS:
@@ -331,7 +313,6 @@ mitigation_v2_change(bool enabled)
 		xc_wait(xc);
 		printf(" done!\n");
 		v2_mitigation_enabled = enabled;
-		mutex_exit(_lock);
 		v2_set_name();
 		return 0;
 	default:
@@ -495,31 +476,13 @@ mitigation_v4_change_cpu(void *arg1, voi
 static int
 mitigation_v4_change(bool enabled)
 {
-	struct cpu_info *ci = NULL;
-	CPU_INFO_ITERATOR cii;
 	uint64_t xc;
 
 	v4_detect_method();
 
-	mutex_enter(_lock);
-
-	/*
-	 * We expect all the CPUs to be online.
-	 */
-	for (CPU_INFO_FOREACH(cii, ci)) {
-		struct schedstate_percpu *spc = >ci_schedstate;
-		if (spc->spc_flags & SPCF_OFFLINE) {
-			printf("[!] cpu%d offline, SpectreV4 not changed\n",
-			cpu_index(ci));
-			mutex_exit(_lock);
-			return EOPNOTSUPP;
-		}
-	}
-
 	switch (v4_mitigation_method) {
 	case V4_MITIGATION_NONE:
 		printf("[!] No mitigation available\n");
-		mutex_exit(_lock);
 		return EOPNOTSUPP;
 	case V4_MITIGATION_INTEL_SSBD:
 	case V4_MITIGATION_AMD_NONARCH_F15H:
@@ -532,13 +495,11 @@ mitigation_v4_change(bool enabled)
 		xc_wait(xc);
 		printf(" done!\n");
 		v4_mitigation_enabled = enabled;
-		mutex_exit(_lock);
 		v4_set_name();
 		return 0;
 	case V4_MITIGATION_INTEL_SSB_NO:
 	case V4_MITIGATION_AMD_SSB_NO:
 		printf("[+] The CPU is not affected by SpectreV4\n");
-		mutex_exit(_lock);
 		return 0;
 	default:
 		panic("impossible");

Index: src/sys/arch/x86/x86/svs.c
diff -u src/sys/arch/x86/x86/svs.c:1.23 src/sys/arch/x86/x86/svs.c:1.24
--- src/sys/arch/x86/x86/svs.c:1.23	Sat Mar  9 08:42:26 2019
+++ src/sys/arch/x86/x86/svs.c	Sat Mar 23 10:02:05 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: svs.c,v 1.23 2019/03/09 08:42:26 maxv Exp $	*/
+/*	$NetBSD: svs.c,v 1.24 2019/03/23 10:02:05 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.23 2019/03/09 08:42:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.24 2019/03/23 10:02:05 maxv Exp $");
 
 #include "opt_svs.h"
 
@@ -704,25 +704,8 @@ svs_disable_cpu(void *arg1, void *arg2)
 static int
 svs_disable(void)
 {
-	struct cpu_info *ci = NULL;
-	CPU_INFO_ITERATOR cii;
 	uint64_t xc;
 
-	mutex_enter(_lock);
-
-	/*
-	 * We expect all the CPUs to be online.
-	 */
-	for (CPU_INFO_FOREACH(cii, ci)) {
-		struct schedstate_percpu *spc = >ci_schedstate;
-		if (spc->spc_flags & SPCF_OFFLINE) {
-			printf("[!] cpu%d offline, SVS not disabled\n",
-			cpu_index(ci));
-			mutex_exit(_lock);
-			return EOPNOTSUPP;
-		}
-	}
-
 	svs_cpu_barrier1 = ncpu;
 	svs_cpu_barrier2 = ncpu;
 
@@ -731,8 +714,6 @@ svs_disable(void)
 	xc_wait(xc);
 	printf(" done!\n");
 
-	mutex_exit(_lock);
-
 	return 0;
 }
 



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

2019-03-12 Thread Andreas Gustafsson
Module Name:src
Committed By:   gson
Date:   Tue Mar 12 08:29:52 UTC 2019

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

Log Message:
Add missing space in "wiring for pmap .. did not change" message


To generate a diff of this commit:
cvs rdiff -u -r1.330 -r1.331 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.330 src/sys/arch/x86/x86/pmap.c:1.331
--- src/sys/arch/x86/x86/pmap.c:1.330	Sun Mar 10 16:30:01 2019
+++ src/sys/arch/x86/x86/pmap.c	Tue Mar 12 08:29:52 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.330 2019/03/10 16:30:01 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.331 2019/03/12 08:29:52 gson Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.330 2019/03/10 16:30:01 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.331 2019/03/12 08:29:52 gson Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -4117,7 +4117,7 @@ pmap_unwire(struct pmap *pmap, vaddr_t v
 		pmap_stats_update_bypte(pmap, npte, opte);
 	} else {
 		printf("%s: wiring for pmap %p va %#" PRIxVADDR
-		"did not change!\n", __func__, pmap, va);
+		" did not change!\n", __func__, pmap, va);
 	}
 
 	/* Release pmap. */



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

2019-02-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Feb 21 14:31:55 UTC 2019

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

Log Message:
Remove wrong KASSERT in EPT, and reorder the code to reduce duplication.


To generate a diff of this commit:
cvs rdiff -u -r1.324 -r1.325 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.324 src/sys/arch/x86/x86/pmap.c:1.325
--- src/sys/arch/x86/x86/pmap.c:1.324	Mon Feb 18 19:03:12 2019
+++ src/sys/arch/x86/x86/pmap.c	Thu Feb 21 14:31:54 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.324 2019/02/18 19:03:12 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.325 2019/02/21 14:31:54 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.324 2019/02/18 19:03:12 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.325 2019/02/21 14:31:54 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3643,25 +3643,25 @@ pmap_sync_pv(struct pv_pte *pvpte, paddr
 	pt_entry_t expect;
 	bool need_shootdown;
 
-	expect = pmap_pa2pte(pa) | PG_V;
 	ptp = pvpte->pte_ptp;
 	va = pvpte->pte_va;
 	KASSERT(ptp == NULL || ptp->uobject != NULL);
 	KASSERT(ptp == NULL || ptp_va2o(va, 1) == ptp->offset);
 	pmap = ptp_to_pmap(ptp);
+	KASSERT(kpreempt_disabled());
 
 	if (__predict_false(pmap->pm_sync_pv != NULL)) {
 		return (*pmap->pm_sync_pv)(ptp, va, pa, clearbits, oattrs,
 		optep);
 	}
 
+	expect = pmap_pa2pte(pa) | PG_V;
+
 	if (clearbits != ~0) {
 		KASSERT((clearbits & ~(PP_ATTRS_M|PP_ATTRS_U|PP_ATTRS_W)) == 0);
 		clearbits = pmap_pp_attrs_to_pte(clearbits);
 	}
 
-	KASSERT(kpreempt_disabled());
-
 	ptep = pmap_map_pte(pmap, ptp, va);
 	do {
 		opte = *ptep;
@@ -5487,13 +5487,8 @@ pmap_ept_sync_pv(struct vm_page *ptp, va
 	bool need_shootdown;
 
 	expect = pmap_pa2pte(pa) | EPT_R;
-	KASSERT(ptp == NULL || ptp->uobject != NULL);
-	KASSERT(ptp == NULL || ptp_va2o(va, 1) == ptp->offset);
 	pmap = ptp_to_pmap(ptp);
 
-	KASSERT(clearbits == ~0 || (clearbits & ~(EPT_D | EPT_A | EPT_W)) == 0);
-	KASSERT(kpreempt_disabled());
-
 	if (clearbits != ~0) {
 		KASSERT((clearbits & ~(PP_ATTRS_M|PP_ATTRS_U|PP_ATTRS_W)) == 0);
 		clearbits = pmap_pp_attrs_to_ept(clearbits);



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

2019-02-18 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Feb 18 19:03:12 UTC 2019

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

Log Message:
Fix stupid mistake, I didn't reflect correctly the behavior of pmap_sync_pv
in the EPT callback, 'optep' can be NULL.


To generate a diff of this commit:
cvs rdiff -u -r1.323 -r1.324 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.323 src/sys/arch/x86/x86/pmap.c:1.324
--- src/sys/arch/x86/x86/pmap.c:1.323	Thu Feb 14 08:18:25 2019
+++ src/sys/arch/x86/x86/pmap.c	Mon Feb 18 19:03:12 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.323 2019/02/14 08:18:25 cherry Exp $	*/
+/*	$NetBSD: pmap.c,v 1.324 2019/02/18 19:03:12 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.323 2019/02/14 08:18:25 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.324 2019/02/18 19:03:12 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -5557,7 +5557,8 @@ pmap_ept_sync_pv(struct vm_page *ptp, va
 	pmap_unmap_pte();
 
 	*oattrs = pmap_ept_to_pp_attrs(opte);
-	*optep = opte;
+	if (optep != NULL)
+		*optep = opte;
 	return 0;
 }
 



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

2019-02-17 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Mon Feb 18 05:41:20 UTC 2019

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

Log Message:
- Add wbnoinvd, virt_ssbd, tme, cldemote, movdiri, movdir64b and pconfig.
- Move AMD 0x8008 ebx's ibpb, ibrs and stibp to x86_features[8] linux
  mapping.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 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.27 src/sys/arch/x86/x86/procfs_machdep.c:1.28
--- src/sys/arch/x86/x86/procfs_machdep.c:1.27	Sun Jan  6 15:37:17 2019
+++ src/sys/arch/x86/x86/procfs_machdep.c	Mon Feb 18 05:41:20 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.27 2019/01/06 15:37:17 christos Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.28 2019/02/18 05:41:20 msaitoh Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.27 2019/01/06 15:37:17 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.28 2019/02/18 05:41:20 msaitoh Exp $");
 
 #include 
 #include 
@@ -115,14 +115,14 @@ static const char * const x86_features[]
 	"hw_pstate", "proc_feedback", "sme", NULL,
 	NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
+	NULL, "ibrs", "ibpb", "stibp", NULL, NULL, NULL, NULL},
 
 	{ /* (8) Linux mapping */
 	"tpr_shadow", "vnmi", "flexpriority", "ept",
 	"vpid", "npt", "lbrv", "svm_lock",
 	"nrip_save", "tsc_scale", "vmcb_clean", "flushbyasid",
 	"decodeassists", "pausefilter", "pfthreshold", "vmmcall",
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+	NULL, "ept_ad", NULL, NULL, NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (9) Intel-defined: 0007 ebx */
@@ -153,9 +153,9 @@ static const char * const x86_features[]
 
 	{ /* (13) AMD 0x8008 ebx */
 	"clzero", "irperf", "xsaveerptr", NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, "ibpb", NULL, "ibrs", "stibp",
+	NULL, "wbnoinvd", NULL, NULL, NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
+	NULL, "virt_ssbd", NULL, NULL, NULL, NULL, NULL, NULL},
 
 	{ /* (14) 0x0006 eax */
 	"dtherm", "ida", "arat", NULL, "pln", NULL, "pts", "hwp",
@@ -176,9 +176,9 @@ static const char * const x86_features[]
 	NULL, "avx512vbmi", "umip", "pku",
 	"ospke", NULL, "avx512_vbmi2", NULL,
 	"gfni", "vaes", "vpclmulqdq", "avx512_vnni",
-	"avx512_bitalg", NULL, "avx512_vpopcntdq", NULL,
+	"avx512_bitalg", "tme", "avx512_vpopcntdq", NULL,
 	"la57", NULL, NULL, NULL, NULL, NULL, "rdpid", NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
+	NULL, "cldemote", NULL, "movdiri", "movdir64b", NULL, NULL, NULL},
 
 	{ /* (17) 0x8007 ebx */
 	"overflow_recov", "succor", NULL, "smca", NULL, NULL, NULL, NULL,
@@ -189,7 +189,7 @@ static const char * const x86_features[]
 	{ /* (18) Intel 0x0007 edx */
 	NULL, NULL, "avx512_4vnniw", "avx512_4fmaps", NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+	NULL, NULL, "pconfig", NULL, NULL, NULL, NULL, NULL,
 	NULL, NULL, NULL, NULL,
 	"flush_l1d", "arch_capabilities", NULL, "ssbd"},
 };



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

2019-02-16 Thread NONAKA Kimihiro
Module Name:src
Committed By:   nonaka
Date:   Sun Feb 17 05:06:16 UTC 2019

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

Log Message:
PR/53984: Partial revert of modify lapic_calibrate_timer() in lapic.c r1.69.


To generate a diff of this commit:
cvs rdiff -u -r1.69 -r1.70 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.69 src/sys/arch/x86/x86/lapic.c:1.70
--- src/sys/arch/x86/x86/lapic.c:1.69	Fri Feb 15 08:54:01 2019
+++ src/sys/arch/x86/x86/lapic.c	Sun Feb 17 05:06:16 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: lapic.c,v 1.69 2019/02/15 08:54:01 nonaka Exp $	*/
+/*	$NetBSD: lapic.c,v 1.70 2019/02/17 05:06:16 nonaka Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2008 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.69 2019/02/15 08:54:01 nonaka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.70 2019/02/17 05:06:16 nonaka Exp $");
 
 #include "acpica.h"
 #include "ioapic.h"
@@ -604,11 +604,9 @@ extern void (*initclock_func)(void); /* 
 void
 lapic_calibrate_timer(struct cpu_info *ci)
 {
-	struct timecounter *tc;
-	timecounter_get_t *tick_func;
-	unsigned int tval, mask, delta, initial_counter, initial_lapic;
-	unsigned int cur_counter, cur_lapic;
-	uint64_t seen, end, tmp, freq;
+	unsigned int seen, delta, initial_i8254, initial_lapic;
+	unsigned int cur_i8254, cur_lapic;
+	uint64_t tmp;
 	int i;
 	char tbuf[9];
 
@@ -617,19 +615,6 @@ lapic_calibrate_timer(struct cpu_info *c
 
 	aprint_debug_dev(ci->ci_dev, "calibrating local timer\n");
 
-	tc = timecounter;
-	if (tc->tc_quality <= 0) {
-		tick_func = (timecounter_get_t *)gettick;
-		tval = rtclock_tval;
-		mask = ~0u;
-		freq = TIMER_FREQ;
-	} else {
-		tick_func = tc->tc_get_timecount;
-		tval = mask = tc->tc_counter_mask;
-		freq = tc->tc_frequency;
-	}
-	end = freq / 100;
-
 	/*
 	 * Configure timer to one-shot, interrupt masked,
 	 * large positive number.
@@ -641,22 +626,22 @@ lapic_calibrate_timer(struct cpu_info *c
 	x86_disable_intr();
 
 	initial_lapic = lapic_gettick();
-	initial_counter = tick_func(tc) & mask;
+	initial_i8254 = gettick();
 
-	for (seen = 0; seen < end; seen += delta) {
-		cur_counter = tick_func(tc) & mask;
-		if (cur_counter > initial_counter)
-			delta = tval - (cur_counter - initial_counter);
+	for (seen = 0; seen < TIMER_FREQ / 100; seen += delta) {
+		cur_i8254 = gettick();
+		if (cur_i8254 > initial_i8254)
+			delta = rtclock_tval - (cur_i8254 - initial_i8254);
 		else
-			delta = initial_counter - cur_counter;
-		initial_counter = cur_counter;
+			delta = initial_i8254 - cur_i8254;
+		initial_i8254 = cur_i8254;
 	}
 	cur_lapic = lapic_gettick();
 
 	x86_enable_intr();
 
 	tmp = initial_lapic - cur_lapic;
-	lapic_per_second = (tmp * freq + seen / 2) / seen;
+	lapic_per_second = (tmp * TIMER_FREQ + seen / 2) / seen;
 
 calibrate_done:
 	humanize_number(tbuf, sizeof(tbuf), lapic_per_second, "Hz", 1000);



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

2019-02-11 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Tue Feb 12 03:13:50 UTC 2019

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

Log Message:
Fix typo: Parameters are seperated by ','.


To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 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.141 src/sys/arch/x86/x86/intr.c:1.142
--- src/sys/arch/x86/x86/intr.c:1.141	Tue Dec 25 06:50:12 2018
+++ src/sys/arch/x86/x86/intr.c	Tue Feb 12 03:13:50 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.141 2018/12/25 06:50:12 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.142 2019/02/12 03:13:50 cherry Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.141 2018/12/25 06:50:12 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.142 2019/02/12 03:13:50 cherry Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1131,7 +1131,7 @@ intr_string(intr_handle_t ih, char *buf,
 		snprintf(buf, len, "irq %d", APIC_IRQ_LEGACY_IRQ(ih));
 
 #elif NLAPIC > 0
-	snprintf(buf, len, "irq %d" APIC_IRQ_LEGACY_IRQ(ih));
+	snprintf(buf, len, "irq %d", APIC_IRQ_LEGACY_IRQ(ih));
 #else
 	snprintf(buf, len, "irq %d", (int) ih);
 #endif



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

2019-02-11 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Mon Feb 11 18:50:15 UTC 2019

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

Log Message:
Detect and report running in a XEN hvm container.

This allows the lapic code to apply its x2apic probe logic while
running in a XEN hvm container.


To generate a diff of this commit:
cvs rdiff -u -r1.87 -r1.88 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.87 src/sys/arch/x86/x86/identcpu.c:1.88
--- src/sys/arch/x86/x86/identcpu.c:1.87	Mon Feb 11 14:59:33 2019
+++ src/sys/arch/x86/x86/identcpu.c	Mon Feb 11 18:50:15 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.87 2019/02/11 14:59:33 cherry Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.88 2019/02/11 18:50:15 cherry 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.87 2019/02/11 14:59:33 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.88 2019/02/11 18:50:15 cherry Exp $");
 
 #include "opt_xen.h"
 
@@ -1125,6 +1125,8 @@ identify_hypervisor(void)
 vm_guest = VM_GUEST_HV;
 			else if (memcmp(hv_vendor, "KVMKVMKVM\0\0\0", 12) == 0)
 vm_guest = VM_GUEST_KVM;
+			else if (memcmp(hv_vendor, "XenVMMXenVMM", 12) == 0)
+vm_guest = VM_GUEST_XEN;
 			/* FreeBSD bhyve: "bhyve bhyve " */
 			/* OpenBSD vmm:   "OpenBSDVMM58" */
 			/* NetBSD nvmm:   "___ NVMM ___" */



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

2019-01-31 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Jan 31 20:42:31 UTC 2019

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

Log Message:
Move some code into a separate function, no functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.316 -r1.317 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.316 src/sys/arch/x86/x86/pmap.c:1.317
--- src/sys/arch/x86/x86/pmap.c:1.316	Thu Jan 17 14:24:51 2019
+++ src/sys/arch/x86/x86/pmap.c	Thu Jan 31 20:42:31 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.316 2019/01/17 14:24:51 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.317 2019/01/31 20:42:31 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.316 2019/01/17 14:24:51 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.317 2019/01/31 20:42:31 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3662,6 +3662,23 @@ pmap_sync_pv(struct pv_pte *pvpte, paddr
 	return 0;
 }
 
+static inline void
+pmap_pp_remove_ent(struct pmap *pmap, struct vm_page *ptp, pt_entry_t opte,
+vaddr_t va)
+{
+	struct pmap *pmap2;
+	pt_entry_t *ptes;
+	pd_entry_t * const *pdes;
+
+	pmap_map_ptes(pmap, , , );
+	pmap_stats_update_bypte(pmap, 0, opte);
+	ptp->wire_count--;
+	if (ptp->wire_count <= 1) {
+		pmap_free_ptp(pmap, ptp, va, ptes, pdes);
+	}
+	pmap_unmap_ptes(pmap, pmap2);
+}
+
 static void
 pmap_pp_remove(struct pmap_page *pp, paddr_t pa)
 {
@@ -3708,20 +3725,9 @@ startover:
 
 		/* Update the PTP reference count. Free if last reference. */
 		if (ptp != NULL) {
-			struct pmap *pmap2;
-			pt_entry_t *ptes;
-			pd_entry_t * const *pdes;
-
 			KASSERT(pmap != pmap_kernel());
-
 			pmap_tlb_shootnow();
-			pmap_map_ptes(pmap, , , );
-			pmap_stats_update_bypte(pmap, 0, opte);
-			ptp->wire_count--;
-			if (ptp->wire_count <= 1) {
-pmap_free_ptp(pmap, ptp, va, ptes, pdes);
-			}
-			pmap_unmap_ptes(pmap, pmap2);
+			pmap_pp_remove_ent(pmap, ptp, opte, va);
 			pmap_destroy(pmap);
 		} else {
 			KASSERT(pmap == pmap_kernel());



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

2019-01-26 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Sun Jan 27 05:08:05 UTC 2019

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

Log Message:
fix duplicated chunk from merge


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/arch/x86/x86/spectre.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/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.23 src/sys/arch/x86/x86/spectre.c:1.24
--- src/sys/arch/x86/x86/spectre.c:1.23	Sun Jan 27 02:08:39 2019
+++ src/sys/arch/x86/x86/spectre.c	Sun Jan 27 05:08:05 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.23 2019/01/27 02:08:39 pgoyette Exp $	*/
+/*	$NetBSD: spectre.c,v 1.24 2019/01/27 05:08:05 dholland Exp $	*/
 
 /*
  * Copyright (c) 2018 NetBSD Foundation, Inc.
@@ -34,9 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.23 2019/01/27 02:08:39 pgoyette Exp $");
-
-#include "opt_spectre.h"
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.24 2019/01/27 05:08:05 dholland Exp $");
 
 #include "opt_spectre.h"
 



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

2019-01-19 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sat Jan 19 13:17:33 UTC 2019

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

Log Message:
PR kern/53893: add missing #ifdef DDB around db_printf calls.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/x86/x86/db_memrw.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/db_memrw.c
diff -u src/sys/arch/x86/x86/db_memrw.c:1.7 src/sys/arch/x86/x86/db_memrw.c:1.8
--- src/sys/arch/x86/x86/db_memrw.c:1.7	Sun Nov 18 14:16:13 2018
+++ src/sys/arch/x86/x86/db_memrw.c	Sat Jan 19 13:17:32 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_memrw.c,v 1.7 2018/11/18 14:16:13 christos Exp $	*/
+/*	$NetBSD: db_memrw.c,v 1.8 2019/01/19 13:17:32 martin Exp $	*/
 
 /*-
  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.7 2018/11/18 14:16:13 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.8 2019/01/19 13:17:32 martin Exp $");
 
 #include 
 #include 
@@ -90,7 +90,9 @@ db_read_bytes(vaddr_t addr, size_t size,
 	src = (char *)addr;
 
 	if (db_validate_address((vaddr_t)src)) {
+#ifdef DDB
 		db_printf("address %p is invalid\n", src);
+#endif
 		return;
 	}
 
@@ -111,7 +113,9 @@ db_read_bytes(vaddr_t addr, size_t size,
 
 	while (size-- > 0) {
 		if (db_validate_address((vaddr_t)src)) {
+#ifdef DDB
 			db_printf("address %p is invalid\n", src);
+#endif
 			return;
 		}
 
@@ -144,7 +148,9 @@ db_write_text(vaddr_t addr, size_t size,
 		pte = *ppte;
 
 		if ((pte & PG_V) == 0) {
+#ifdef DDB
 			db_printf(" address %p not a valid page\n", dst);
+#endif
 			return;
 		}
 



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

2019-01-17 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Jan 17 14:24:51 UTC 2019

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

Log Message:
Simplify pmap_sync_pv: just pass a pa, and build the pte inside.


To generate a diff of this commit:
cvs rdiff -u -r1.315 -r1.316 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.315 src/sys/arch/x86/x86/pmap.c:1.316
--- src/sys/arch/x86/x86/pmap.c:1.315	Mon Dec 17 07:10:07 2018
+++ src/sys/arch/x86/x86/pmap.c	Thu Jan 17 14:24:51 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.315 2018/12/17 07:10:07 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.316 2019/01/17 14:24:51 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.315 2018/12/17 07:10:07 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.316 2019/01/17 14:24:51 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3579,8 +3579,7 @@ pmap_remove(struct pmap *pmap, vaddr_t s
  * => issues tlb shootdowns if necessary.
  */
 static int
-pmap_sync_pv(struct pv_pte *pvpte, pt_entry_t expect, int clearbits,
-pt_entry_t *optep)
+pmap_sync_pv(struct pv_pte *pvpte, paddr_t pa, int clearbits, pt_entry_t *optep)
 {
 	struct pmap *pmap;
 	struct vm_page *ptp;
@@ -3588,16 +3587,16 @@ pmap_sync_pv(struct pv_pte *pvpte, pt_en
 	pt_entry_t *ptep;
 	pt_entry_t opte;
 	pt_entry_t npte;
+	pt_entry_t expect;
 	bool need_shootdown;
 
+	expect = pmap_pa2pte(pa) | PG_V;
 	ptp = pvpte->pte_ptp;
 	va = pvpte->pte_va;
 	KASSERT(ptp == NULL || ptp->uobject != NULL);
 	KASSERT(ptp == NULL || ptp_va2o(va, 1) == ptp->offset);
 	pmap = ptp_to_pmap(ptp);
 
-	KASSERT((expect & ~(PG_FRAME | PG_V)) == 0);
-	KASSERT((expect & PG_V) != 0);
 	KASSERT(clearbits == ~0 || (clearbits & ~(PG_M | PG_U | PG_RW)) == 0);
 	KASSERT(kpreempt_disabled());
 
@@ -3608,16 +3607,14 @@ pmap_sync_pv(struct pv_pte *pvpte, pt_en
 		KASSERT((opte & (PG_U | PG_V)) != PG_U);
 		KASSERT(opte == 0 || (opte & PG_V) != 0);
 		if ((opte & (PG_FRAME | PG_V)) != expect) {
-
 			/*
-			 * we lost a race with a V->P operation like
-			 * pmap_remove().  wait for the competitor
+			 * We lost a race with a V->P operation like
+			 * pmap_remove().  Wait for the competitor
 			 * reflecting pte bits into mp_attrs.
 			 *
-			 * issue a redundant TLB shootdown so that
+			 * Issue a redundant TLB shootdown so that
 			 * we can wait for its completion.
 			 */
-
 			pmap_unmap_pte();
 			if (clearbits != 0) {
 pmap_tlb_shootdown(pmap, va,
@@ -3628,30 +3625,26 @@ pmap_sync_pv(struct pv_pte *pvpte, pt_en
 		}
 
 		/*
-		 * check if there's anything to do on this pte.
+		 * Check if there's anything to do on this PTE.
 		 */
-
 		if ((opte & clearbits) == 0) {
 			need_shootdown = false;
 			break;
 		}
 
 		/*
-		 * we need a shootdown if the pte is cached. (PG_U)
-		 *
-		 * ...unless we are clearing only the PG_RW bit and
-		 * it isn't cached as RW. (PG_M)
+		 * We need a shootdown if the PTE is cached (PG_U) ...
+		 * ... Unless we are clearing only the PG_RW bit and
+		 * it isn't cached as RW (PG_M).
 		 */
-
 		need_shootdown = (opte & PG_U) != 0 &&
 		!(clearbits == PG_RW && (opte & PG_M) == 0);
 
 		npte = opte & ~clearbits;
 
 		/*
-		 * if we need a shootdown anyway, clear PG_U and PG_M.
+		 * If we need a shootdown anyway, clear PG_U and PG_M.
 		 */
-
 		if (need_shootdown) {
 			npte &= ~(PG_U | PG_M);
 		}
@@ -3675,10 +3668,8 @@ pmap_pp_remove(struct pmap_page *pp, pad
 	struct pv_pte *pvpte;
 	struct pv_entry *killlist = NULL;
 	struct vm_page *ptp;
-	pt_entry_t expect;
 	int count;
 
-	expect = pmap_pa2pte(pa) | PG_V;
 	count = SPINLOCK_BACKOFF_MIN;
 	kpreempt_disable();
 startover:
@@ -3690,17 +3681,16 @@ startover:
 		int error;
 
 		/*
-		 * add a reference to the pmap before clearing the pte.
-		 * otherwise the pmap can disappear behind us.
+		 * Add a reference to the pmap before clearing the pte.
+		 * Otherwise the pmap can disappear behind us.
 		 */
-
 		ptp = pvpte->pte_ptp;
 		pmap = ptp_to_pmap(ptp);
 		if (ptp != NULL) {
 			pmap_reference(pmap);
 		}
 
-		error = pmap_sync_pv(pvpte, expect, ~0, );
+		error = pmap_sync_pv(pvpte, pa, ~0, );
 		if (error == EAGAIN) {
 			int hold_count;
 			KERNEL_UNLOCK_ALL(curlwp, _count);
@@ -3716,7 +3706,7 @@ startover:
 		va = pvpte->pte_va;
 		pve = pmap_remove_pv(pp, ptp, va);
 
-		/* update the PTP reference count.  free if last reference. */
+		/* Update the PTP reference count. Free if last reference. */
 		if (ptp != NULL) {
 			struct pmap *pmap2;
 			pt_entry_t *ptes;
@@ -3798,8 +3788,8 @@ pmap_test_attrs(struct vm_page *pg, unsi
 {
 	struct pmap_page *pp;
 	struct pv_pte *pvpte;
-	pt_entry_t expect;
 	u_int result;
+	paddr_t pa;
 
 	KASSERT(uvm_page_locked_p(pg));
 
@@ -3807,7 

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

2019-01-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Jan 14 18:54:07 UTC 2019

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

Log Message:
Add #ifndef i386, the dbregs are 32bit in this case anyway.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/x86/x86/dbregs.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/dbregs.c
diff -u src/sys/arch/x86/x86/dbregs.c:1.13 src/sys/arch/x86/x86/dbregs.c:1.14
--- src/sys/arch/x86/x86/dbregs.c:1.13	Sun Jan 13 10:01:07 2019
+++ src/sys/arch/x86/x86/dbregs.c	Mon Jan 14 18:54:07 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: dbregs.c,v 1.13 2019/01/13 10:01:07 maxv Exp $	*/
+/*	$NetBSD: dbregs.c,v 1.14 2019/01/14 18:54:07 maxv Exp $	*/
 
 /*
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -256,12 +256,14 @@ x86_dbregs_validate(const struct dbreg *
 			return EINVAL;
 	}
 
+#ifndef i386
 	if (regs->dr[6] & X86_DR6_MBZ) {
 		return EINVAL;
 	}
 	if (regs->dr[7] & X86_DR7_MBZ) {
 		return EINVAL;
 	}
+#endif
 	if (regs->dr[7] & X86_DR7_GENERAL_DETECT_ENABLE) {
 		return EINVAL;
 	}



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

2019-01-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jan 13 12:16:58 UTC 2019

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

Log Message:
On certain AMD f10h CPUs (like mine), the BIOS does not enable WC+. It
means that the guest pages that are WC+ become CD, and this degrades
performance of the guests.

Explicitly enable WC+.

While here clarify the AMD identification code.


To generate a diff of this commit:
cvs rdiff -u -r1.85 -r1.86 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.85 src/sys/arch/x86/x86/identcpu.c:1.86
--- src/sys/arch/x86/x86/identcpu.c:1.85	Sun Jan  6 16:13:51 2019
+++ src/sys/arch/x86/x86/identcpu.c	Sun Jan 13 12:16:58 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.85 2019/01/06 16:13:51 maxv Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.86 2019/01/13 12:16:58 maxv 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.85 2019/01/06 16:13:51 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.86 2019/01/13 12:16:58 maxv Exp $");
 
 #include "opt_xen.h"
 
@@ -354,41 +354,51 @@ cpu_probe_amd_cache(struct cpu_info *ci)
 }
 
 static void
-cpu_probe_k5(struct cpu_info *ci)
+cpu_probe_amd(struct cpu_info *ci)
 {
+	uint64_t val;
 	int flag;
 
-	if (cpu_vendor != CPUVENDOR_AMD ||
-	CPUID_TO_FAMILY(ci->ci_signature) != 5)
+	if (cpu_vendor != CPUVENDOR_AMD)
+		return;
+	if (CPUID_TO_FAMILY(ci->ci_signature) < 5)
 		return;
 
-	if (CPUID_TO_MODEL(ci->ci_signature) == 0) {
+	switch (CPUID_TO_FAMILY(ci->ci_signature)) {
+	case 0x05: /* K5 */
+		if (CPUID_TO_MODEL(ci->ci_signature) == 0) {
+			/*
+			 * According to the AMD Processor Recognition App Note,
+			 * the AMD-K5 Model 0 uses the wrong bit to indicate
+			 * support for global PTEs, instead using bit 9 (APIC)
+			 * rather than bit 13 (i.e. "0x200" vs. 0x2000").
+			 */
+			flag = ci->ci_feat_val[0];
+			if ((flag & CPUID_APIC) != 0)
+flag = (flag & ~CPUID_APIC) | CPUID_PGE;
+			ci->ci_feat_val[0] = flag;
+		}
+		break;
+
+	case 0x10: /* Family 10h */
 		/*
-		 * According to the AMD Processor Recognition App Note,
-		 * the AMD-K5 Model 0 uses the wrong bit to indicate
-		 * support for global PTEs, instead using bit 9 (APIC)
-		 * rather than bit 13 (i.e. "0x200" vs. 0x2000".  Oops!).
+		 * On Family 10h, certain BIOSes do not enable WC+ support.
+		 * This causes WC+ to become CD, and degrades guest
+		 * performance at the NPT level.
+		 *
+		 * Explicitly enable WC+ if we're not a guest.
 		 */
-		flag = ci->ci_feat_val[0];
-		if ((flag & CPUID_APIC) != 0)
-			flag = (flag & ~CPUID_APIC) | CPUID_PGE;
-		ci->ci_feat_val[0] = flag;
+		if (!ISSET(ci->ci_feat_val[1], CPUID2_RAZ)) {
+			val = rdmsr(MSR_BU_CFG2);
+			val &= ~BU_CFG2_CWPLUS_DIS;
+			wrmsr(MSR_BU_CFG2, val);
+		}
+		break;
 	}
 
 	cpu_probe_amd_cache(ci);
 }
 
-static void
-cpu_probe_k678(struct cpu_info *ci)
-{
-
-	if (cpu_vendor != CPUVENDOR_AMD ||
-	CPUID_TO_FAMILY(ci->ci_signature) < 6)
-		return;
-
-	cpu_probe_amd_cache(ci);
-}
-
 static inline uint8_t
 cyrix_read_reg(uint8_t reg)
 {
@@ -956,8 +966,7 @@ cpu_probe(struct cpu_info *ci)
 	}
 
 	cpu_probe_intel(ci);
-	cpu_probe_k5(ci);
-	cpu_probe_k678(ci);
+	cpu_probe_amd(ci);
 	cpu_probe_cyrix(ci);
 	cpu_probe_winchip(ci);
 	cpu_probe_c3(ci);



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

2019-01-06 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jan  6 16:19:12 UTC 2019

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

Log Message:
Flush the host TLB too when dealing with a guest pmap. The pmap is not
active on the host so the pages aren't cached; but the recursive PTE
entries may have been cached by our pmap code.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/x86/x86/x86_tlb.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_tlb.c
diff -u src/sys/arch/x86/x86/x86_tlb.c:1.3 src/sys/arch/x86/x86/x86_tlb.c:1.4
--- src/sys/arch/x86/x86/x86_tlb.c:1.3	Wed Nov  7 07:14:51 2018
+++ src/sys/arch/x86/x86/x86_tlb.c	Sun Jan  6 16:19:12 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_tlb.c,v 1.3 2018/11/07 07:14:51 maxv Exp $	*/
+/*	$NetBSD: x86_tlb.c,v 1.4 2019/01/06 16:19:12 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008-2012 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.3 2018/11/07 07:14:51 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_tlb.c,v 1.4 2019/01/06 16:19:12 maxv Exp $");
 
 #include 
 #include 
@@ -229,7 +229,6 @@ pmap_tlb_shootdown(struct pmap *pm, vadd
 
 	if (__predict_false(pm->pm_tlb_flush != NULL)) {
 		(*pm->pm_tlb_flush)(pm);
-		return;
 	}
 
 	/*



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

2019-01-06 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Jan  6 15:37:18 UTC 2019

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

Log Message:
restore original now that weak symbols are gone


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 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.26 src/sys/arch/x86/x86/procfs_machdep.c:1.27
--- src/sys/arch/x86/x86/procfs_machdep.c:1.26	Sat Jan  5 15:32:02 2019
+++ src/sys/arch/x86/x86/procfs_machdep.c	Sun Jan  6 10:37:17 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.26 2019/01/05 20:32:02 christos Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.27 2019/01/06 15:37:17 christos Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.26 2019/01/05 20:32:02 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.27 2019/01/06 15:37:17 christos Exp $");
 
 #include 
 #include 
@@ -447,12 +447,7 @@ procfs_getonecpu(int xcpu, struct cpu_in
 	i386_fpu_fdivbug ? "yes" : "no",	/* an old pentium */
 #endif
 	ci->ci_max_cpuid,
-#ifdef notyet
-	// XXX: Fixme Weak symbols are not supported in modules
 	(rcr0() & CR0_WP) ? "yes" : "no",
-#else
-	"no",
-#endif
 	featurebuf,
 	ci->ci_cflush_lsize
 	);



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

2019-01-05 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Jan  5 20:32:02 UTC 2019

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

Log Message:
Comment out rcr0 use until the weak symbol mess is undone.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 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.25 src/sys/arch/x86/x86/procfs_machdep.c:1.26
--- src/sys/arch/x86/x86/procfs_machdep.c:1.25	Wed Nov 14 23:53:54 2018
+++ src/sys/arch/x86/x86/procfs_machdep.c	Sat Jan  5 15:32:02 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.25 2018/11/15 04:53:54 msaitoh Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.26 2019/01/05 20:32:02 christos Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.25 2018/11/15 04:53:54 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.26 2019/01/05 20:32:02 christos Exp $");
 
 #include 
 #include 
@@ -447,7 +447,12 @@ procfs_getonecpu(int xcpu, struct cpu_in
 	i386_fpu_fdivbug ? "yes" : "no",	/* an old pentium */
 #endif
 	ci->ci_max_cpuid,
+#ifdef notyet
+	// XXX: Fixme Weak symbols are not supported in modules
 	(rcr0() & CR0_WP) ? "yes" : "no",
+#else
+	"no",
+#endif
 	featurebuf,
 	ci->ci_cflush_lsize
 	);



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

2018-12-24 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Mon Dec 24 22:05:45 UTC 2018

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

Log Message:
Towards bifurcating XEN and native interrupt related functions,
this is a preliminary cleanup sweep.

Move functions related to MP bus probe and scanning to x86/mp.c

Move generic platform pic search function to x86/x86_machdep.c


To generate a diff of this commit:
cvs rdiff -u -r1.139 -r1.140 src/sys/arch/x86/x86/intr.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/x86/x86/mp.c
cvs rdiff -u -r1.120 -r1.121 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/intr.c
diff -u src/sys/arch/x86/x86/intr.c:1.139 src/sys/arch/x86/x86/intr.c:1.140
--- src/sys/arch/x86/x86/intr.c:1.139	Mon Dec 24 14:55:41 2018
+++ src/sys/arch/x86/x86/intr.c	Mon Dec 24 22:05:45 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.139 2018/12/24 14:55:41 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.140 2018/12/24 22:05:45 cherry Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.139 2018/12/24 14:55:41 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.140 2018/12/24 22:05:45 cherry Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -220,13 +220,6 @@ static SIMPLEQ_HEAD(, intrsource) io_int
 
 static kmutex_t intr_distribute_lock;
 
-#if NIOAPIC > 0 || NACPICA > 0
-static int intr_scan_bus(int, int, intr_handle_t *);
-#if NPCI > 0
-static int intr_find_pcibridge(int, pcitag_t *, pci_chipset_tag_t *);
-#endif
-#endif
-
 #if !defined(XEN)
 static int intr_allocate_slot_cpu(struct cpu_info *, struct pic *, int, int *,
   struct intrsource *);
@@ -374,123 +367,6 @@ intr_calculatemasks(struct cpu_info *ci)
 }
 
 /*
- * List to keep track of PCI buses that are probed but not known
- * to the firmware. Used to
- *
- * XXX should maintain one list, not an array and a linked list.
- */
-#if (NPCI > 0) && ((NIOAPIC > 0) || NACPICA > 0)
-struct intr_extra_bus {
-	int bus;
-	pcitag_t *pci_bridge_tag;
-	pci_chipset_tag_t pci_chipset_tag;
-	LIST_ENTRY(intr_extra_bus) list;
-};
-
-LIST_HEAD(, intr_extra_bus) intr_extra_buses =
-LIST_HEAD_INITIALIZER(intr_extra_buses);
-
-
-void
-intr_add_pcibus(struct pcibus_attach_args *pba)
-{
-	struct intr_extra_bus *iebp;
-
-	iebp = kmem_alloc(sizeof(*iebp), KM_SLEEP);
-	iebp->bus = pba->pba_bus;
-	iebp->pci_chipset_tag = pba->pba_pc;
-	iebp->pci_bridge_tag = pba->pba_bridgetag;
-	LIST_INSERT_HEAD(_extra_buses, iebp, list);
-}
-
-static int
-intr_find_pcibridge(int bus, pcitag_t *pci_bridge_tag,
-		pci_chipset_tag_t *pc)
-{
-	struct intr_extra_bus *iebp;
-	struct mp_bus *mpb;
-
-	if (bus < 0)
-		return ENOENT;
-
-	if (bus < mp_nbus) {
-		mpb = _busses[bus];
-		if (mpb->mb_pci_bridge_tag == NULL)
-			return ENOENT;
-		*pci_bridge_tag = *mpb->mb_pci_bridge_tag;
-		*pc = mpb->mb_pci_chipset_tag;
-		return 0;
-	}
-
-	LIST_FOREACH(iebp, _extra_buses, list) {
-		if (iebp->bus == bus) {
-			if (iebp->pci_bridge_tag == NULL)
-return ENOENT;
-			*pci_bridge_tag = *iebp->pci_bridge_tag;
-			*pc = iebp->pci_chipset_tag;
-			return 0;
-		}
-	}
-	return ENOENT;
-}
-#endif
-
-#if NIOAPIC > 0 || NACPICA > 0
-/*
- * 'pin' argument pci bus_pin encoding of a device/pin combination.
- */
-int
-intr_find_mpmapping(int bus, int pin, intr_handle_t *handle)
-{
-
-#if NPCI > 0
-	while (intr_scan_bus(bus, pin, handle) != 0) {
-		int dev, func;
-		pcitag_t pci_bridge_tag;
-		pci_chipset_tag_t pc;
-
-		if (intr_find_pcibridge(bus, _bridge_tag, ) != 0)
-			return ENOENT;
-		dev = pin >> 2;
-		pin = pin & 3;
-		pin = PPB_INTERRUPT_SWIZZLE(pin + 1, dev) - 1;
-		pci_decompose_tag(pc, pci_bridge_tag, , , );
-		pin |= (dev << 2);
-	}
-	return 0;
-#else
-	return intr_scan_bus(bus, pin, handle);
-#endif
-}
-
-static int
-intr_scan_bus(int bus, int pin, intr_handle_t *handle)
-{
-	struct mp_intr_map *mip, *intrs;
-
-	if (bus < 0 || bus >= mp_nbus)
-		return ENOENT;
-
-	intrs = mp_busses[bus].mb_intrs;
-	if (intrs == NULL)
-		return ENOENT;
-
-	for (mip = intrs; mip != NULL; mip = mip->next) {
-		if (mip->bus_pin == pin) {
-#if NACPICA > 0
-			if (mip->linkdev != NULL)
-if (mpacpi_findintr_linkdev(mip) != 0)
-	continue;
-#endif
-			*handle = mip->ioapic_ih;
-			return 0;
-		}
-	}
-	return ENOENT;
-}
-#endif
-
-/*
  * Create an interrupt id such as "ioapic0 pin 9". This interrupt id is used
  * by MI code and intrctl(8).
  */
@@ -852,23 +728,6 @@ intr_biglock_wrapper(void *vp)
 #endif /* MULTIPROCESSOR */
 #endif /* XEN */
 
-#if defined(DOM0OPS) || !defined(XEN)
-struct pic *
-intr_findpic(int num)
-{
-#if NIOAPIC > 0
-	struct ioapic_softc *pic;
-
-	pic = ioapic_find_bybase(num);
-	if (pic != NULL)
-		return >sc_pic;
-#endif
-	if (num < NUM_LEGACY_IRQS)
-		return _pic;
-
-	return NULL;
-}
-#endif
 
 

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

2018-12-23 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Sun Dec 23 12:11:41 UTC 2018

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

Log Message:
whitespace, NFC


To generate a diff of this commit:
cvs rdiff -u -r1.137 -r1.138 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.137 src/sys/arch/x86/x86/intr.c:1.138
--- src/sys/arch/x86/x86/intr.c:1.137	Tue Dec  4 19:27:22 2018
+++ src/sys/arch/x86/x86/intr.c	Sun Dec 23 12:11:40 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.137 2018/12/04 19:27:22 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.138 2018/12/23 12:11:40 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.137 2018/12/04 19:27:22 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.138 2018/12/23 12:11:40 jdolecek Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -2127,7 +2127,7 @@ intr_set_affinity(struct intrsource *isp
 
 	pin = isp->is_pin;
 	(*pic->pic_hwmask)(pic, pin); /* for ci_ipending check */
-	while(oldci->ci_ipending & (1 << oldslot))
+	while (oldci->ci_ipending & (1 << oldslot))
 		(void)kpause("intrdist", false, 1, _lock);
 
 	kpreempt_disable();



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

2018-12-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec 22 10:00:39 UTC 2018

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

Log Message:
In the end, disable the supposed architectural SpectreV2 mitigation on
AMD f12h and f16h. The SDMs of these CPUs haven't been updated since, and
we shouldn't assume the position of the bits, we just can't know where
they are.

Initially I included f12h and f16h because f10h is actually documented
to have a bit to disable the indirect branch predictor, and there were
patches available in SuSE and CentOS that were treating f10h/f12h/f16h
all the same. Knowing that SuSE has ties with AMD, it seemed safe to
assume that these patches were correct and that f12h and f16h could
indeed be treated the same way as f10h.

But these patches have now disappeared, and the main Linux branch
doesn't have them, without clear explanation. Therefore, I prefer to
roll-back.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/x86/x86/spectre.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/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.21 src/sys/arch/x86/x86/spectre.c:1.22
--- src/sys/arch/x86/x86/spectre.c:1.21	Sat Dec 22 09:20:30 2018
+++ src/sys/arch/x86/x86/spectre.c	Sat Dec 22 10:00:39 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.21 2018/12/22 09:20:30 maxv Exp $	*/
+/*	$NetBSD: spectre.c,v 1.22 2018/12/22 10:00:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.21 2018/12/22 09:20:30 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.22 2018/12/22 10:00:39 maxv Exp $");
 
 #include "opt_spectre.h"
 
@@ -142,8 +142,6 @@ v2_detect_method(void)
 		 */
 		switch (CPUID_TO_FAMILY(ci->ci_signature)) {
 		case 0x10:
-		case 0x12:
-		case 0x16:
 			v2_mitigation_method = V2_MITIGATION_AMD_DIS_IND;
 			break;
 		default:



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

2018-12-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec 22 09:20:30 UTC 2018

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

Log Message:
Add AMD_SSB_NO, so that we explicitly say than an AMD CPU is not affected
when it's not affected.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/x86/x86/spectre.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/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.20 src/sys/arch/x86/x86/spectre.c:1.21
--- src/sys/arch/x86/x86/spectre.c:1.20	Sat Dec 22 08:59:44 2018
+++ src/sys/arch/x86/x86/spectre.c	Sat Dec 22 09:20:30 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.20 2018/12/22 08:59:44 maxv Exp $	*/
+/*	$NetBSD: spectre.c,v 1.21 2018/12/22 09:20:30 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.20 2018/12/22 08:59:44 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.21 2018/12/22 09:20:30 maxv Exp $");
 
 #include "opt_spectre.h"
 
@@ -61,6 +61,7 @@ enum v4_mitigation {
 	V4_MITIGATION_NONE,
 	V4_MITIGATION_INTEL_SSBD,
 	V4_MITIGATION_INTEL_SSB_NO,
+	V4_MITIGATION_AMD_SSB_NO,
 	V4_MITIGATION_AMD_NONARCH_F15H,
 	V4_MITIGATION_AMD_NONARCH_F16H,
 	V4_MITIGATION_AMD_NONARCH_F17H
@@ -380,6 +381,9 @@ v4_set_name(void)
 		case V4_MITIGATION_INTEL_SSB_NO:
 			strlcat(name, "[Intel SSB_NO]", sizeof(name));
 			break;
+		case V4_MITIGATION_AMD_SSB_NO:
+			strlcat(name, "[AMD SSB_NO]", sizeof(name));
+			break;
 		case V4_MITIGATION_AMD_NONARCH_F15H:
 		case V4_MITIGATION_AMD_NONARCH_F16H:
 		case V4_MITIGATION_AMD_NONARCH_F17H:
@@ -403,11 +407,7 @@ v4_detect_method(void)
 		if (cpu_info_primary.ci_feat_val[7] & CPUID_SEF_ARCH_CAP) {
 			msr = rdmsr(MSR_IA32_ARCH_CAPABILITIES);
 			if (msr & IA32_ARCH_SSB_NO) {
-/*
- * The processor indicates it is not vulnerable
- * to the Speculative Store Bypass (SpectreV4)
- * flaw.
- */
+/* Not vulnerable to SpectreV4. */
 v4_mitigation_method = V4_MITIGATION_INTEL_SSB_NO;
 return;
 			}
@@ -432,6 +432,16 @@ v4_detect_method(void)
 			v4_mitigation_method = V4_MITIGATION_AMD_NONARCH_F17H;
 			return;
 		default:
+			if (cpu_info_primary.ci_max_ext_cpuid < 0x8008) {
+break;
+			}
+	 		x86_cpuid(0x8008, descs);
+			if (descs[1] & __BIT(26)) {
+/* Not vulnerable to SpectreV4. */
+v4_mitigation_method = V4_MITIGATION_AMD_SSB_NO;
+return;
+			}
+
 			break;
 		}
 	}
@@ -447,6 +457,7 @@ mitigation_v4_apply_cpu(bool enabled)
 	switch (v4_mitigation_method) {
 	case V4_MITIGATION_NONE:
 	case V4_MITIGATION_INTEL_SSB_NO:
+	case V4_MITIGATION_AMD_SSB_NO:
 		panic("impossible");
 	case V4_MITIGATION_INTEL_SSBD:
 		msrval = MSR_IA32_SPEC_CTRL;
@@ -527,6 +538,7 @@ mitigation_v4_change(bool enabled)
 		v4_set_name();
 		return 0;
 	case V4_MITIGATION_INTEL_SSB_NO:
+	case V4_MITIGATION_AMD_SSB_NO:
 		printf("[+] The CPU is not affected by SpectreV4\n");
 		mutex_exit(_lock);
 		return 0;
@@ -623,13 +635,15 @@ cpu_speculation_init(struct cpu_info *ci
 		v4_set_name();
 	}
 	if (v4_mitigation_method != V4_MITIGATION_NONE &&
-	v4_mitigation_method != V4_MITIGATION_INTEL_SSB_NO) {
+	v4_mitigation_method != V4_MITIGATION_INTEL_SSB_NO &&
+	v4_mitigation_method != V4_MITIGATION_AMD_SSB_NO) {
 		mitigation_v4_apply_cpu(ci, true);
 	}
 #else
 	if (ci == _info_primary) {
 		v4_detect_method();
-		if (v4_mitigation_method == V4_MITIGATION_INTEL_SSB_NO) {
+		if (v4_mitigation_method == V4_MITIGATION_INTEL_SSB_NO ||
+		v4_mitigation_method == V4_MITIGATION_AMD_SSB_NO) {
 			v4_mitigation_enabled = true;
 			v4_set_name();
 		}



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

2018-12-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec 22 08:59:45 UTC 2018

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

Log Message:
If the CPU is not vulnerable to SpectreV4, say it in the sysctl by default.
Apply some minor style while here.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/x86/x86/spectre.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/spectre.c
diff -u src/sys/arch/x86/x86/spectre.c:1.19 src/sys/arch/x86/x86/spectre.c:1.20
--- src/sys/arch/x86/x86/spectre.c:1.19	Mon May 28 20:18:58 2018
+++ src/sys/arch/x86/x86/spectre.c	Sat Dec 22 08:59:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: spectre.c,v 1.19 2018/05/28 20:18:58 maxv Exp $	*/
+/*	$NetBSD: spectre.c,v 1.20 2018/12/22 08:59:44 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.19 2018/05/28 20:18:58 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spectre.c,v 1.20 2018/12/22 08:59:44 maxv Exp $");
 
 #include "opt_spectre.h"
 
@@ -483,7 +483,8 @@ mitigation_v4_change_cpu(void *arg1, voi
 	mitigation_v4_apply_cpu(enabled);
 }
 
-static int mitigation_v4_change(bool enabled)
+static int
+mitigation_v4_change(bool enabled)
 {
 	struct cpu_info *ci = NULL;
 	CPU_INFO_ITERATOR cii;
@@ -609,6 +610,10 @@ cpu_speculation_init(struct cpu_info *ci
 	 *
 	 * cpu0 is the one that detects the method and sets the global
 	 * variable.
+	 *
+	 * Disabled by default, as recommended by AMD, but can be enabled
+	 * dynamically. We only detect if the CPU is not vulnerable, to
+	 * mark it as 'mitigated' in the sysctl.
 	 */
 #if 0
 	if (ci == _info_primary) {
@@ -617,9 +622,18 @@ cpu_speculation_init(struct cpu_info *ci
 		(v4_mitigation_method != V4_MITIGATION_NONE);
 		v4_set_name();
 	}
-	if (v4_mitigation_method != V4_MITIGATION_NONE) {
+	if (v4_mitigation_method != V4_MITIGATION_NONE &&
+	v4_mitigation_method != V4_MITIGATION_INTEL_SSB_NO) {
 		mitigation_v4_apply_cpu(ci, true);
 	}
+#else
+	if (ci == _info_primary) {
+		v4_detect_method();
+		if (v4_mitigation_method == V4_MITIGATION_INTEL_SSB_NO) {
+			v4_mitigation_enabled = true;
+			v4_set_name();
+		}
+	}
 #endif
 }
 



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

2018-12-17 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Dec 17 16:26:03 UTC 2018

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

Log Message:
Back to using aprint_error() and get more info about the error so we can
figure out why we can't map the registers.


To generate a diff of this commit:
cvs rdiff -u -r1.69 -r1.70 src/sys/arch/x86/x86/ipmi.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/ipmi.c
diff -u src/sys/arch/x86/x86/ipmi.c:1.69 src/sys/arch/x86/x86/ipmi.c:1.70
--- src/sys/arch/x86/x86/ipmi.c:1.69	Mon Dec 17 10:12:52 2018
+++ src/sys/arch/x86/x86/ipmi.c	Mon Dec 17 11:26:03 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: ipmi.c,v 1.69 2018/12/17 15:12:52 gson Exp $ */
+/*	$NetBSD: ipmi.c,v 1.70 2018/12/17 16:26:03 christos Exp $ */
 
 /*
  * Copyright (c) 2006 Manuel Bouyer.
@@ -52,7 +52,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ipmi.c,v 1.69 2018/12/17 15:12:52 gson Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ipmi.c,v 1.70 2018/12/17 16:26:03 christos Exp $");
 
 #include 
 #include 
@@ -1923,6 +1923,8 @@ ipmi_refresh_sensors(struct ipmi_softc *
 int
 ipmi_map_regs(struct ipmi_softc *sc, struct ipmi_attach_args *ia)
 {
+	int error;
+
 	sc->sc_if = ipmi_get_if(ia->iaa_if_type);
 	if (sc->sc_if == NULL)
 		return -1;
@@ -1934,12 +1936,14 @@ ipmi_map_regs(struct ipmi_softc *sc, str
 
 	sc->sc_if_rev = ia->iaa_if_rev;
 	sc->sc_if_iospacing = ia->iaa_if_iospacing;
-	if (bus_space_map(sc->sc_iot, ia->iaa_if_iobase,
-	sc->sc_if->nregs * sc->sc_if_iospacing,
-	0, >sc_ioh)) {
-		printf("%s: bus_space_map(..., %x, %x, 0, %p) failed\n",
-		__func__, ia->iaa_if_iobase,
-		sc->sc_if->nregs * sc->sc_if_iospacing, >sc_ioh);
+	if ((error = bus_space_map(sc->sc_iot, ia->iaa_if_iobase,
+	sc->sc_if->nregs * sc->sc_if_iospacing, 0, >sc_ioh)) != 0) {
+		const char *xname = sc->sc_dev ? device_xname(sc->sc_dev) :
+		"ipmi0";
+		aprint_error("%s: %s:bus_space_map(..., %x, %x, 0, %p)"
+		" type %c failed %d\n", xname, __func__, ia->iaa_if_iobase,
+		sc->sc_if->nregs * sc->sc_if_iospacing, >sc_ioh,
+		ia->iaa_if_iotype, error);
 		return -1;
 	}
 #if 0



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

2018-12-17 Thread Andreas Gustafsson
Module Name:src
Committed By:   gson
Date:   Mon Dec 17 15:12:52 UTC 2018

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

Log Message:
Don't call aprint_error_dev() with a NULL dev.  Fixes PR port-amd64/53789.


To generate a diff of this commit:
cvs rdiff -u -r1.68 -r1.69 src/sys/arch/x86/x86/ipmi.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/ipmi.c
diff -u src/sys/arch/x86/x86/ipmi.c:1.68 src/sys/arch/x86/x86/ipmi.c:1.69
--- src/sys/arch/x86/x86/ipmi.c:1.68	Sat Dec  1 01:56:30 2018
+++ src/sys/arch/x86/x86/ipmi.c	Mon Dec 17 15:12:52 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: ipmi.c,v 1.68 2018/12/01 01:56:30 msaitoh Exp $ */
+/*	$NetBSD: ipmi.c,v 1.69 2018/12/17 15:12:52 gson Exp $ */
 
 /*
  * Copyright (c) 2006 Manuel Bouyer.
@@ -52,7 +52,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ipmi.c,v 1.68 2018/12/01 01:56:30 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ipmi.c,v 1.69 2018/12/17 15:12:52 gson Exp $");
 
 #include 
 #include 
@@ -1937,8 +1937,7 @@ ipmi_map_regs(struct ipmi_softc *sc, str
 	if (bus_space_map(sc->sc_iot, ia->iaa_if_iobase,
 	sc->sc_if->nregs * sc->sc_if_iospacing,
 	0, >sc_ioh)) {
-		aprint_error_dev(sc->sc_dev,
-		"%s: bus_space_map(..., %x, %x, 0, %p) failed\n",
+		printf("%s: bus_space_map(..., %x, %x, 0, %p) failed\n",
 		__func__, ia->iaa_if_iobase,
 		sc->sc_if->nregs * sc->sc_if_iospacing, >sc_ioh);
 		return -1;



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

2018-12-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Dec 17 07:10:07 UTC 2018

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

Log Message:
Remove dead checks, they were already pointless when I fixed them a few
years ago, and now they are wrong because the PTE space is randomized.


To generate a diff of this commit:
cvs rdiff -u -r1.314 -r1.315 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.314 src/sys/arch/x86/x86/pmap.c:1.315
--- src/sys/arch/x86/x86/pmap.c:1.314	Mon Dec 17 06:58:54 2018
+++ src/sys/arch/x86/x86/pmap.c	Mon Dec 17 07:10:07 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.314 2018/12/17 06:58:54 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.315 2018/12/17 07:10:07 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.314 2018/12/17 06:58:54 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.315 2018/12/17 07:10:07 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -3484,7 +3484,6 @@ pmap_remove(struct pmap *pmap, vaddr_t s
 	pd_entry_t * const *pdes;
 	struct pv_entry *pv_tofree = NULL;
 	bool result;
-	int i;
 	paddr_t ptppa;
 	vaddr_t blkendva, va = sva;
 	struct vm_page *ptp;
@@ -3537,20 +3536,6 @@ pmap_remove(struct pmap *pmap, vaddr_t s
 		if (blkendva > eva)
 			blkendva = eva;
 
-		/*
-		 * Our PTE mappings should never be removed with pmap_remove.
-		 *
-		 * XXXmaxv: still needed?
-		 *
-		 * A long term solution is to move the PTEs out of user address
-		 * space, and into kernel address space. Then we can set
-		 * VM_MAXUSER_ADDRESS to be VM_MAX_ADDRESS.
-		 */
-		for (i = 0; i < PDP_SIZE; i++) {
-			if (pl_i(va, PTP_LEVELS) == PDIR_SLOT_PTE+i)
-panic("PTE space accessed");
-		}
-
 		lvl = pmap_pdes_invalid(va, pdes, );
 		if (lvl != 0) {
 			/* Skip a range corresponding to an invalid pde. */
@@ -3983,26 +3968,11 @@ pmap_write_protect(struct pmap *pmap, va
 
 	for (va = sva ; va < eva; va = blockend) {
 		pt_entry_t *spte, *epte;
-		int i;
 
 		blockend = x86_round_pdr(va + 1);
 		if (blockend > eva)
 			blockend = eva;
 
-		/*
-		 * Our PTE mappings should never be write-protected.
-		 *
-		 * XXXmaxv: still needed?
-		 *
-		 * A long term solution is to move the PTEs out of user address
-		 * space, and into kernel address space. Then we can set
-		 * VM_MAXUSER_ADDRESS to be VM_MAX_ADDRESS.
-		 */
-		for (i = 0; i < PDP_SIZE; i++) {
-			if (pl_i(va, PTP_LEVELS) == PDIR_SLOT_PTE+i)
-panic("PTE space accessed");
-		}
-
 		/* Is it a valid block? */
 		if (!pmap_pdes_valid(va, pdes, NULL)) {
 			continue;
@@ -4142,8 +4112,6 @@ pmap_enter_ma(struct pmap *pmap, vaddr_t
 	npte |= PG_W;
 	if (va < VM_MAXUSER_ADDRESS)
 		npte |= PG_u;
-	else if (va < VM_MAX_ADDRESS)
-		panic("PTE space accessed");	/* XXXmaxv: no longer needed? */
 
 	if (pmap == pmap_kernel())
 		npte |= pmap_pg_g;



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

2018-12-16 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Sun Dec 16 20:58:00 UTC 2018

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

Log Message:
use ci_ipending instead of ci_istate.ipending, NFC


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 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.67 src/sys/arch/x86/x86/lapic.c:1.68
--- src/sys/arch/x86/x86/lapic.c:1.67	Sun Sep 23 00:59:59 2018
+++ src/sys/arch/x86/x86/lapic.c	Sun Dec 16 20:58:00 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: lapic.c,v 1.67 2018/09/23 00:59:59 cherry Exp $	*/
+/*	$NetBSD: lapic.c,v 1.68 2018/12/16 20:58:00 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2008 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.67 2018/09/23 00:59:59 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.68 2018/12/16 20:58:00 jdolecek Exp $");
 
 #include "acpica.h"
 #include "ioapic.h"
@@ -535,7 +535,7 @@ lapic_get_timecount(struct timecounter *
 		if (lapic_readreg(reg) & (1 << (LAPIC_TIMER_VECTOR % 32))) {
 			cur_timer -= lapic_tval;
 		}
-	} else if (ci->ci_istate.ipending & (1 << LIR_TIMER))
+	} else if (ci->ci_ipending & (1 << LIR_TIMER))
 		cur_timer = lapic_gettick() - lapic_tval;
 	cur_timer = ci->ci_lapic_counter - cur_timer;
 	splx(s);



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

2018-12-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Dec 16 10:42:32 UTC 2018

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

Log Message:
Explicitly disable ALTINST on VIA, in case it isn't disabled by default
already (the 'VIA cpu backdoor').


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 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.83 src/sys/arch/x86/x86/identcpu.c:1.84
--- src/sys/arch/x86/x86/identcpu.c:1.83	Mon Nov 19 22:21:32 2018
+++ src/sys/arch/x86/x86/identcpu.c	Sun Dec 16 10:42:32 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.83 2018/11/19 22:21:32 jdolecek Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.84 2018/12/16 10:42:32 maxv 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.83 2018/11/19 22:21:32 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.84 2018/12/16 10:42:32 maxv Exp $");
 
 #include "opt_xen.h"
 
@@ -601,6 +601,12 @@ cpu_probe_c3(struct cpu_info *ci)
 		}
 	}
 
+	/* Explicitly disable unsafe ALTINST mode. */
+	if (ci->ci_feat_val[4] & CPUID_VIA_DO_ACE) {
+		msr = rdmsr(MSR_VIA_ACE);
+		wrmsr(MSR_VIA_ACE, msr & ~VIA_ACE_ALTINST);
+	} 
+
 	/*
 	 * Determine L1 cache/TLB info.
 	 */



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

2018-12-06 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Dec  6 17:44:28 UTC 2018

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

Log Message:
Simplify, use _pi instead of modulos, no real functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 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.21 src/sys/arch/x86/x86/svs.c:1.22
--- src/sys/arch/x86/x86/svs.c:1.21	Mon Nov 19 20:28:01 2018
+++ src/sys/arch/x86/x86/svs.c	Thu Dec  6 17:44:28 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: svs.c,v 1.21 2018/11/19 20:28:01 maxv Exp $	*/
+/*	$NetBSD: svs.c,v 1.22 2018/12/06 17:44:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.21 2018/11/19 20:28:01 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svs.c,v 1.22 2018/12/06 17:44:28 maxv Exp $");
 
 #include "opt_svs.h"
 
@@ -237,19 +237,17 @@ struct svs_utls {
 static pd_entry_t *
 svs_tree_add(struct cpu_info *ci, vaddr_t va)
 {
-	extern const vaddr_t ptp_frames[];
+	extern const vaddr_t ptp_masks[];
 	extern const int ptp_shifts[];
-	extern const long nbpd[];
 	pd_entry_t *dstpde;
-	size_t i, pidx, mod;
 	struct vm_page *pg;
+	size_t i, pidx;
 	paddr_t pa;
 
 	dstpde = ci->ci_svs_updir;
-	mod = (size_t)-1;
 
 	for (i = PTP_LEVELS; i > 1; i--) {
-		pidx = pl_i(va % mod, i);
+		pidx = pl_pi(va, i);
 
 		if (!pmap_valid_entry(dstpde[pidx])) {
 			pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
@@ -263,7 +261,6 @@ svs_tree_add(struct cpu_info *ci, vaddr_
 
 		pa = (paddr_t)(dstpde[pidx] & PG_FRAME);
 		dstpde = (pd_entry_t *)PMAP_DIRECT_MAP(pa);
-		mod = nbpd[i-1];
 	}
 
 	return dstpde;
@@ -279,7 +276,7 @@ svs_page_add(struct cpu_info *ci, vaddr_
 	/* Create levels L4, L3 and L2. */
 	dstpde = svs_tree_add(ci, va);
 
-	pidx = pl1_i(va % NBPD_L2);
+	pidx = pl1_pi(va);
 
 	/*
 	 * If 'va' is in a large page, we need to compute its physical
@@ -369,7 +366,7 @@ svs_utls_init(struct cpu_info *ci)
 	if (pmap_valid_entry(L1_BASE[pl1_i(utlsva)])) {
 		panic("%s: local page already mapped", __func__);
 	}
-	pidx = pl1_i(utlsva % NBPD_L2);
+	pidx = pl1_pi(utlsva);
 	if (pmap_valid_entry(pd[pidx])) {
 		panic("%s: L1 page already mapped", __func__);
 	}



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

2018-12-04 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Tue Dec  4 19:27:22 UTC 2018

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

Log Message:
Hypothetically speaking, if one were to want to compile a

'no options MULTIPROCESSOR'

kernel, these files may trip up the build.

Fix them by moving around the #defines as originally intended.

No Functional Changes.


To generate a diff of this commit:
cvs rdiff -u -r1.163 -r1.164 src/sys/arch/x86/x86/cpu.c
cvs rdiff -u -r1.136 -r1.137 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/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.163 src/sys/arch/x86/x86/cpu.c:1.164
--- src/sys/arch/x86/x86/cpu.c:1.163	Tue Dec  4 19:22:42 2018
+++ src/sys/arch/x86/x86/cpu.c	Tue Dec  4 19:27:22 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.163 2018/12/04 19:22:42 cherry Exp $	*/
+/*	$NetBSD: cpu.c,v 1.164 2018/12/04 19:27:22 cherry Exp $	*/
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.163 2018/12/04 19:22:42 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.164 2018/12/04 19:27:22 cherry Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -188,10 +188,10 @@ struct cpu_info *cpu_starting;
 void	cpu_hatch(void *);
 static void	cpu_boot_secondary(struct cpu_info *ci);
 static void	cpu_start_secondary(struct cpu_info *ci);
-#endif
 #if NLAPIC > 0
 static void	cpu_copy_trampoline(paddr_t);
 #endif
+#endif /* MULTIPROCESSOR */
 
 /*
  * Runs once per boot once multiprocessor goo has been detected and
@@ -987,6 +987,7 @@ cpu_debug_dump(void)
 }
 #endif
 
+#ifdef MULTIPROCESSOR
 #if NLAPIC > 0
 static void
 cpu_copy_trampoline(paddr_t pdir_pa)
@@ -1028,7 +1029,6 @@ cpu_copy_trampoline(paddr_t pdir_pa)
 }
 #endif
 
-#ifdef MULTIPROCESSOR
 int
 mp_cpu_start(struct cpu_info *ci, paddr_t target)
 {

Index: src/sys/arch/x86/x86/intr.c
diff -u src/sys/arch/x86/x86/intr.c:1.136 src/sys/arch/x86/x86/intr.c:1.137
--- src/sys/arch/x86/x86/intr.c:1.136	Sun Dec  2 08:19:44 2018
+++ src/sys/arch/x86/x86/intr.c	Tue Dec  4 19:27:22 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.136 2018/12/02 08:19:44 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.137 2018/12/04 19:27:22 cherry Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.136 2018/12/02 08:19:44 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.137 2018/12/04 19:27:22 cherry Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1493,7 +1493,9 @@ void
 cpu_intr_init(struct cpu_info *ci)
 {
 #if !defined(XEN)
+#if (NLAPIC > 0) || defined(MULTIPROCESSOR) || defined(__HAVE_PREEMPTION)
 	struct intrsource *isp;
+#endif
 #if NLAPIC > 0
 	static int first = 1;
 #if defined(MULTIPROCESSOR)



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

2018-12-04 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Tue Dec  4 19:22:42 UTC 2018

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

Log Message:
Stop panic()ing on a UP system.

The reason for the panic is that the cpu_attach() doesn't run to
completion because it thinks it's run past maxcpus (which in the case
of UP), is 1.

This is because on x86 at least, mi_cpu_attach() is called *before*
configure() (and thus the cpu_match()/cpu_attach() pair). Thus ncpu
has already been incremented by the time MD cpu_attach() is called.

Fix this.


To generate a diff of this commit:
cvs rdiff -u -r1.162 -r1.163 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.162 src/sys/arch/x86/x86/cpu.c:1.163
--- src/sys/arch/x86/x86/cpu.c:1.162	Mon Nov 12 18:10:36 2018
+++ src/sys/arch/x86/x86/cpu.c	Tue Dec  4 19:22:42 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.162 2018/11/12 18:10:36 maxv Exp $	*/
+/*	$NetBSD: cpu.c,v 1.163 2018/12/04 19:22:42 cherry Exp $	*/
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.162 2018/11/12 18:10:36 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.163 2018/12/04 19:22:42 cherry Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -320,7 +320,7 @@ cpu_attach(device_t parent, device_t sel
 
 	sc->sc_dev = self;
 
-	if (ncpu == maxcpus) {
+	if (ncpu > maxcpus) {
 #ifndef _LP64
 		aprint_error(": too many CPUs, please use NetBSD/amd64\n");
 #else



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

2018-12-03 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Mon Dec  3 19:46:43 UTC 2018

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

Log Message:
Do not assume that all uses of efi are pci aware.

Allow efi.c to compile in the case where pci is not enabled.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/x86/x86/efi.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/efi.c
diff -u src/sys/arch/x86/x86/efi.c:1.18 src/sys/arch/x86/x86/efi.c:1.19
--- src/sys/arch/x86/x86/efi.c:1.18	Thu Nov 15 16:58:56 2018
+++ src/sys/arch/x86/x86/efi.c	Mon Dec  3 19:46:43 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: efi.c,v 1.18 2018/11/15 16:58:56 riastradh Exp $	*/
+/*	$NetBSD: efi.c,v 1.19 2018/12/03 19:46:43 cherry Exp $	*/
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.18 2018/11/15 16:58:56 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.19 2018/12/03 19:46:43 cherry Exp $");
 
 #include 
 #include 
@@ -40,7 +40,9 @@ __KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.18
 #include 
 
 #include 
+#if NPCI > 0
 #include  /* for pci_mapreg_map_enable_decode */
+#endif
 
 const struct uuid EFI_UUID_ACPI20 = EFI_TABLE_ACPI20;
 const struct uuid EFI_UUID_ACPI10 = EFI_TABLE_ACPI10;
@@ -419,7 +421,9 @@ efi_init(void)
 		return;
 	}
 	bootmethod_efi = true;
+#if NPCI > 0	
 	pci_mapreg_map_enable_decode = true; /* PR port-amd64/53286 */
+#endif
 }
 
 bool



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

2018-11-19 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Mon Nov 19 22:21:33 UTC 2018

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

Log Message:
enable XSAVE (and hence AVX) under XEN by default, fixes PR kern/50332

okayed by maxv@


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 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.82 src/sys/arch/x86/x86/identcpu.c:1.83
--- src/sys/arch/x86/x86/identcpu.c:1.82	Sat Nov 10 11:08:54 2018
+++ src/sys/arch/x86/x86/identcpu.c	Mon Nov 19 22:21:32 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.82 2018/11/10 11:08:54 maxv Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.83 2018/11/19 22:21:32 jdolecek 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.82 2018/11/10 11:08:54 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.83 2018/11/19 22:21:32 jdolecek Exp $");
 
 #include "opt_xen.h"
 
@@ -822,9 +822,7 @@ cpu_probe_fpu(struct cpu_info *ci)
 	 * supervisor trap. OSXSAVE flag seems to be reliably set according
 	 * to whether XSAVE is actually available.
 	 */
-#ifdef XEN_USE_XSAVE
 	if ((ci->ci_feat_val[1] & CPUID2_OSXSAVE) == 0)
-#endif
 		return;
 #endif
 



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

2018-11-18 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Nov 18 14:16:13 UTC 2018

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

Log Message:
fix whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/x86/x86/db_memrw.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/db_memrw.c
diff -u src/sys/arch/x86/x86/db_memrw.c:1.6 src/sys/arch/x86/x86/db_memrw.c:1.7
--- src/sys/arch/x86/x86/db_memrw.c:1.6	Fri Mar 16 00:48:19 2018
+++ src/sys/arch/x86/x86/db_memrw.c	Sun Nov 18 09:16:13 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_memrw.c,v 1.6 2018/03/16 04:48:19 ozaki-r Exp $	*/
+/*	$NetBSD: db_memrw.c,v 1.7 2018/11/18 14:16:13 christos Exp $	*/
 
 /*-
  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.6 2018/03/16 04:48:19 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.7 2018/11/18 14:16:13 christos Exp $");
 
 #include 
 #include 
@@ -71,8 +71,7 @@ db_validate_address(vaddr_t addr)
 	struct pmap *pmap;
 
 	if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap ||
-	addr >= VM_MIN_KERNEL_ADDRESS
-	   )
+	addr >= VM_MIN_KERNEL_ADDRESS)
 		pmap = pmap_kernel();
 	else
 		pmap = p->p_vmspace->vm_map.pmap;



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

2018-11-15 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Nov 15 16:58:56 UTC 2018

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

Log Message:
No need to write any initializer here, casted or otherwise.

(Sorry about the build breakage; thanks, kre!)


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/x86/x86/efi.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/efi.c
diff -u src/sys/arch/x86/x86/efi.c:1.17 src/sys/arch/x86/x86/efi.c:1.18
--- src/sys/arch/x86/x86/efi.c:1.17	Thu Nov 15 11:20:59 2018
+++ src/sys/arch/x86/x86/efi.c	Thu Nov 15 16:58:56 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: efi.c,v 1.17 2018/11/15 11:20:59 kre Exp $	*/
+/*	$NetBSD: efi.c,v 1.18 2018/11/15 16:58:56 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.17 2018/11/15 11:20:59 kre Exp $");
+__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.18 2018/11/15 16:58:56 riastradh Exp $");
 
 #include 
 #include 
@@ -55,7 +55,7 @@ void 		efi_aprintuuid(const struct uuid 
 bool 		efi_uuideq(const struct uuid *, const struct uuid *);
 
 static bool efi_is32x64 = false;
-static paddr_t efi_systbl_pa = (paddr_t)NULL;
+static paddr_t efi_systbl_pa;
 static struct efi_systbl *efi_systbl_va = NULL;
 static struct efi_cfgtbl *efi_cfgtblhead_va = NULL;
 static struct efi_e820memmap {



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

2018-11-15 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Thu Nov 15 11:20:59 UTC 2018

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

Log Message:
Update signature in prototype of efi_relva() to match
change in definition in previous, and explicitly cast
NULL to paddr_t to avoid gcc noise.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/x86/x86/efi.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/efi.c
diff -u src/sys/arch/x86/x86/efi.c:1.16 src/sys/arch/x86/x86/efi.c:1.17
--- src/sys/arch/x86/x86/efi.c:1.16	Thu Nov 15 04:59:02 2018
+++ src/sys/arch/x86/x86/efi.c	Thu Nov 15 11:20:59 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: efi.c,v 1.16 2018/11/15 04:59:02 riastradh Exp $	*/
+/*	$NetBSD: efi.c,v 1.17 2018/11/15 11:20:59 kre Exp $	*/
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.16 2018/11/15 04:59:02 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.17 2018/11/15 11:20:59 kre Exp $");
 
 #include 
 #include 
@@ -48,14 +48,14 @@ const struct uuid EFI_UUID_SMBIOS = EFI_
 const struct uuid EFI_UUID_SMBIOS3 = EFI_TABLE_SMBIOS3;
 
 static vaddr_t 	efi_getva(paddr_t);
-static void 	efi_relva(vaddr_t);
+static void 	efi_relva(paddr_t, vaddr_t);
 struct efi_cfgtbl *efi_getcfgtblhead(void);
 void 		efi_aprintcfgtbl(void);
 void 		efi_aprintuuid(const struct uuid *);
 bool 		efi_uuideq(const struct uuid *, const struct uuid *);
 
 static bool efi_is32x64 = false;
-static paddr_t efi_systbl_pa = NULL;
+static paddr_t efi_systbl_pa = (paddr_t)NULL;
 static struct efi_systbl *efi_systbl_va = NULL;
 static struct efi_cfgtbl *efi_cfgtblhead_va = NULL;
 static struct efi_e820memmap {



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

2018-11-14 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Thu Nov 15 04:53:54 UTC 2018

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

Log Message:
- I misread ci_acpiid as ci_apicid... LAPIC ID is in ci_cpuid.
  Print it correctly.
- ci_initapicid(Initial APIC ID) is uint32_t, so use %u.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 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.24 src/sys/arch/x86/x86/procfs_machdep.c:1.25
--- src/sys/arch/x86/x86/procfs_machdep.c:1.24	Mon Aug 20 08:53:48 2018
+++ src/sys/arch/x86/x86/procfs_machdep.c	Thu Nov 15 04:53:54 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_machdep.c,v 1.24 2018/08/20 08:53:48 msaitoh Exp $ */
+/*	$NetBSD: procfs_machdep.c,v 1.25 2018/11/15 04:53:54 msaitoh Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.24 2018/08/20 08:53:48 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.25 2018/11/15 04:53:54 msaitoh Exp $");
 
 #include 
 #include 
@@ -421,9 +421,9 @@ procfs_getonecpu(int xcpu, struct cpu_in
 		left = 0;
 
 	l = snprintf(p, left,
-	"apicid\t\t: %d\n"
-	"initial apicid\t: %d\n",
-	ci->ci_acpiid,
+	"apicid\t\t: %lu\n"
+	"initial apicid\t: %u\n",
+	ci->ci_cpuid,
 	ci->ci_initapicid
 	);
 	size += l;



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

2018-11-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 12 18:10:37 UTC 2018

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

Log Message:
Add a comment explaining an important rule. Just to better highlight that
this rule is actually not respected.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 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.161 src/sys/arch/x86/x86/cpu.c:1.162
--- src/sys/arch/x86/x86/cpu.c:1.161	Mon Sep  3 16:29:29 2018
+++ src/sys/arch/x86/x86/cpu.c	Mon Nov 12 18:10:36 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.161 2018/09/03 16:29:29 riastradh Exp $	*/
+/*	$NetBSD: cpu.c,v 1.162 2018/11/12 18:10:36 maxv Exp $	*/
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.161 2018/09/03 16:29:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.162 2018/11/12 18:10:36 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -846,6 +846,13 @@ cpu_hatch(void *v)
 	struct pcb *pcb;
 	int s, i;
 
+	/* - */
+
+	/*
+	 * This section of code must be compiled with SSP disabled, to
+	 * prevent a race against cpu0. See sys/conf/ssp.mk.
+	 */
+
 	cpu_init_msrs(ci, true);
 	cpu_probe(ci);
 	cpu_speculation_init(ci);
@@ -863,6 +870,8 @@ cpu_hatch(void *v)
 	atomic_or_32(>ci_flags, CPUF_PRESENT);
 	tsc_sync_ap(ci);
 
+	/* - */
+
 	/*
 	 * Wait to be brought online.
 	 *



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

2018-11-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Nov 10 11:08:54 UTC 2018

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

Log Message:
Merge the VIA detection code into cpu_probe_c3.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 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.81 src/sys/arch/x86/x86/identcpu.c:1.82
--- src/sys/arch/x86/x86/identcpu.c:1.81	Sat Nov 10 10:52:52 2018
+++ src/sys/arch/x86/x86/identcpu.c	Sat Nov 10 11:08:54 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.81 2018/11/10 10:52:52 maxv Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.82 2018/11/10 11:08:54 maxv 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.81 2018/11/10 10:52:52 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.82 2018/11/10 11:08:54 maxv Exp $");
 
 #include "opt_xen.h"
 
@@ -482,32 +482,13 @@ static void
 cpu_probe_winchip(struct cpu_info *ci)
 {
 
-	if (cpu_vendor != CPUVENDOR_IDT)
+	if (cpu_vendor != CPUVENDOR_IDT ||
+	CPUID_TO_FAMILY(ci->ci_signature) != 5)
 		return;
 
-	switch (CPUID_TO_FAMILY(ci->ci_signature)) {
-	case 5:
-		/* WinChip C6 */
-		if (CPUID_TO_MODEL(ci->ci_signature) == 4)
-			ci->ci_feat_val[0] &= ~CPUID_TSC;
-		break;
-	case 6:
-		/*
-		 * VIA Eden ESP 
-		 *
-		 * Quoting from page 3-4 of: "VIA Eden ESP Processor Datasheet"
-		 * http://www.via.com.tw/download/mainboards/6/14/Eden20v115.pdf
-		 * 
-		 * 1. The CMPXCHG8B instruction is provided and always enabled,
-		 *however, it appears disabled in the corresponding CPUID
-		 *function bit 0 to avoid a bug in an early version of
-		 *Windows NT. However, this default can be changed via a
-		 *bit in the FCR MSR.
-		 */
-		ci->ci_feat_val[0] |= CPUID_CX8;
-		wrmsr(MSR_VIA_FCR, rdmsr(MSR_VIA_FCR) | VIA_ACE_ECX8);
-		break;
-	}
+	/* WinChip C6 */
+	if (CPUID_TO_MODEL(ci->ci_signature) == 4)
+		ci->ci_feat_val[0] &= ~CPUID_TSC;
 }
 
 static void
@@ -528,8 +509,25 @@ cpu_probe_c3(struct cpu_info *ci)
 	x86_cpuid(0x8000, descs);
 	lfunc = descs[0];
 
+	if (family == 6) {
+		/*
+		 * VIA Eden ESP.
+		 *
+		 * Quoting from page 3-4 of: "VIA Eden ESP Processor Datasheet"
+		 * http://www.via.com.tw/download/mainboards/6/14/Eden20v115.pdf
+		 * 
+		 * 1. The CMPXCHG8B instruction is provided and always enabled,
+		 *however, it appears disabled in the corresponding CPUID
+		 *function bit 0 to avoid a bug in an early version of
+		 *Windows NT. However, this default can be changed via a
+		 *bit in the FCR MSR.
+		 */
+		ci->ci_feat_val[0] |= CPUID_CX8;
+		wrmsr(MSR_VIA_FCR, rdmsr(MSR_VIA_FCR) | VIA_ACE_ECX8);
+	}
+
 	if (family > 6 || model > 0x9 || (model == 0x9 && stepping >= 3)) {
-		/* Nehemiah or Esther */
+		/* VIA Nehemiah or Esther. */
 		x86_cpuid(0xc000, descs);
 		lfunc = descs[0];
 		if (lfunc >= 0xc001) {	/* has ACE, RNG */



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

2018-11-08 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Nov  8 10:55:41 UTC 2018

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

Log Message:
Simplify the ifdefs, and error out if XEN and USER_LDT are both defined.


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

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

Modified files:

Index: src/sys/arch/x86/x86/sys_machdep.c
diff -u src/sys/arch/x86/x86/sys_machdep.c:1.49 src/sys/arch/x86/x86/sys_machdep.c:1.50
--- src/sys/arch/x86/x86/sys_machdep.c:1.49	Mon Sep  3 16:29:29 2018
+++ src/sys/arch/x86/x86/sys_machdep.c	Thu Nov  8 10:55:41 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_machdep.c,v 1.49 2018/09/03 16:29:29 riastradh Exp $	*/
+/*	$NetBSD: sys_machdep.c,v 1.50 2018/11/08 10:55:41 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.49 2018/09/03 16:29:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.50 2018/11/08 10:55:41 maxv Exp $");
 
 #include "opt_mtrr.h"
 #include "opt_user_ldt.h"
@@ -63,18 +63,14 @@ __KERNEL_RCSID(0, "$NetBSD: sys_machdep.
 #include 
 #include 
 
-#ifdef __x86_64__
+#if defined(__x86_64__) || defined(XEN)
 #undef	IOPERM	/* not implemented */
 #else
-#if defined(XEN)
-#undef	IOPERM
-#else /* defined(XEN) */
 #define	IOPERM
-#endif /* defined(XEN) */
 #endif
 
-#ifdef XEN
-#undef	USER_LDT
+#if defined(XEN) && defined(USER_LDT)
+#error "USER_LDT not supported on XEN"
 #endif
 
 extern struct vm_map *kernel_map;



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

2018-10-23 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Wed Oct 24 03:51:21 UTC 2018

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

Log Message:
When returning a cached shared irq event value, DTRT


To generate a diff of this commit:
cvs rdiff -u -r1.134 -r1.135 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.134 src/sys/arch/x86/x86/intr.c:1.135
--- src/sys/arch/x86/x86/intr.c:1.134	Mon Oct  8 08:05:08 2018
+++ src/sys/arch/x86/x86/intr.c	Wed Oct 24 03:51:21 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.134 2018/10/08 08:05:08 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.135 2018/10/24 03:51:21 cherry Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.134 2018/10/08 08:05:08 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.135 2018/10/24 03:51:21 cherry Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1294,7 +1294,7 @@ intr_establish_xname(int legacy_irq, str
 		 * Shared interrupt - we can't rebind.
 		 * The port is shared instead.
 		 */
-		evtchn = irq2port[gsi];
+		evtchn = irq2port[gsi] - 1;
 	}
 
 	pih = pirq_establish(gsi, evtchn, handler, arg, level,



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

2018-10-17 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Thu Oct 18 04:14:07 UTC 2018

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

Log Message:
Make compile-time type differentiation more explicit.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 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.8 src/sys/arch/x86/x86/idt.c:1.9
--- src/sys/arch/x86/x86/idt.c:1.8	Sun Sep 23 15:28:49 2018
+++ src/sys/arch/x86/x86/idt.c	Thu Oct 18 04:14:07 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: idt.c,v 1.8 2018/09/23 15:28:49 cherry Exp $	*/
+/*	$NetBSD: idt.c,v 1.9 2018/10/18 04:14:07 cherry 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.8 2018/09/23 15:28:49 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.9 2018/10/18 04:14:07 cherry Exp $");
 
 #include 
 #include 
@@ -89,7 +89,7 @@ static char idt_allocmap[NIDT];
 #if defined(XEN)
 
 void
-set_idtgate(idt_descriptor_t *xen_idd, void *function, int ist,
+set_idtgate(struct trap_info *xen_idd, void *function, int ist,
 	int type, int dpl, int sel)
 {
 	/* 
@@ -120,7 +120,7 @@ set_idtgate(idt_descriptor_t *xen_idd, v
 	 * implicitly part of an idt, which we infer as
 	 * xen_idt_vaddr. (See above).
 	 */
-	xen_idd->vector = xen_idd - (idt_descriptor_t *)xen_idt_vaddr;
+	xen_idd->vector = xen_idd - (struct trap_info *)xen_idt_vaddr;
 
 	/* Back to read-only, as it should be. */
 #if defined(__x86_64__)
@@ -129,7 +129,7 @@ set_idtgate(idt_descriptor_t *xen_idd, v
 	//kpreempt_enable();
 }
 void
-unset_idtgate(idt_descriptor_t *xen_idd)
+unset_idtgate(struct trap_info *xen_idd)
 {
 #if defined(__x86_64__)
 	vaddr_t xen_idt_vaddr = ((vaddr_t) xen_idd) & PAGE_MASK;
@@ -148,12 +148,12 @@ unset_idtgate(idt_descriptor_t *xen_idd)
 }
 #else /* XEN */
 void
-set_idtgate(idt_descriptor_t *idd, void *function, int ist, int type, int dpl, int sel)
+set_idtgate(struct gate_descriptor *idd, void *function, int ist, int type, int dpl, int sel)
 {
 	setgate(idd, function, ist, type, dpl,	sel);
 }
 void
-unset_idtgate(idt_descriptor_t *idd)
+unset_idtgate(struct gate_descriptor *idd)
 {
 	unsetgate(idd);
 }



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

2018-10-08 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Mon Oct  8 08:05:08 UTC 2018

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

Log Message:
Clean up XEN specific stuff from the apic code, and move to intr.c

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/x86/x86/i8259.c
cvs rdiff -u -r1.133 -r1.134 src/sys/arch/x86/x86/intr.c
cvs rdiff -u -r1.58 -r1.59 src/sys/arch/x86/x86/ioapic.c

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

Modified files:

Index: src/sys/arch/x86/x86/i8259.c
diff -u src/sys/arch/x86/x86/i8259.c:1.20 src/sys/arch/x86/x86/i8259.c:1.21
--- src/sys/arch/x86/x86/i8259.c:1.20	Sun Oct  7 16:36:36 2018
+++ src/sys/arch/x86/x86/i8259.c	Mon Oct  8 08:05:08 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: i8259.c,v 1.20 2018/10/07 16:36:36 cherry Exp $	*/
+/*	$NetBSD: i8259.c,v 1.21 2018/10/08 08:05:08 cherry Exp $	*/
 
 /*
  * Copyright 2002 (c) Wasabi Systems, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.20 2018/10/07 16:36:36 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.21 2018/10/08 08:05:08 cherry Exp $");
 
 #include  
 #include 
@@ -88,9 +88,6 @@ __KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.
 #include 
 #include 
 
-#ifdef XEN
-#include 
-#endif
 
 #ifndef __x86_64__
 #include "mca.h"
@@ -102,7 +99,6 @@ __KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.
 static void i8259_hwmask(struct pic *, int);
 static void i8259_hwunmask(struct pic *, int);
 static void i8259_setup(struct pic *, struct cpu_info *, int, int, int);
-static void i8259_unsetup(struct pic *, struct cpu_info *, int, int, int);
 static void i8259_reinit_irqs(void);
 
 unsigned i8259_imen;
@@ -119,7 +115,7 @@ struct pic i8259_pic = {
 	.pic_hwmask = i8259_hwmask,
 	.pic_hwunmask = i8259_hwunmask,
 	.pic_addroute = i8259_setup,
-	.pic_delroute = i8259_unsetup,
+	.pic_delroute = i8259_setup,
 	.pic_level_stubs = legacy_stubs,
 	.pic_edge_stubs = legacy_stubs,
 };
@@ -256,62 +252,10 @@ static void
 i8259_setup(struct pic *pic, struct cpu_info *ci,
 int pin, int idtvec, int type)
 {
-#if defined(XEN)
-	/*
-	 * This is kludgy, and not the right place, but we can't bind
-	 * before the routing has been set to the appropriate 'vector'.
-	 * in x86/intr.c, this is done after idt_vec_set(), where this
-	 * would have been more appropriate to put this.
-	 */
-
-	int port, irq;
-	irq = vect2irq[idtvec];
-	KASSERT(irq != 0);
-	if (irq2port[irq] != 0) {
-		/* 
-		 * Shared interrupt - we can't rebind.
-		 *  The port is shared instead.
-		 */
-		return;
-	}
-	
-	port = bind_pirq_to_evtch(irq);
-	KASSERT(port < NR_EVENT_CHANNELS);
-	KASSERT(port >= 0);
-
-	KASSERT(irq2port[irq] == 0);
-	irq2port[irq] = port + 1;
-
-	xen_atomic_set_bit(>ci_evtmask[0], port);
-#else
 	if (CPU_IS_PRIMARY(ci))
 		i8259_reinit_irqs();
-#endif
 }
 
-static void
-i8259_unsetup(struct pic *pic, struct cpu_info *ci,
-int pin, int idtvec, int type)
-{
-#if defined(XEN)
-	int port, irq;
-	irq = vect2irq[idtvec];
-	port = unbind_pirq_from_evtch(irq);
-
-	KASSERT(port < NR_EVENT_CHANNELS);
-
-	/* XXX: This is problematic for shared interrupts */
-	KASSERT(irq2port[irq] != 0);
-	irq2port[irq] = 0;
-
-	xen_atomic_clear_bit(>ci_evtmask[0], port);
-#else
-	if (CPU_IS_PRIMARY(ci))
-		i8259_reinit_irqs();
-#endif
-}
-
-
 void
 i8259_reinit(void)
 {

Index: src/sys/arch/x86/x86/intr.c
diff -u src/sys/arch/x86/x86/intr.c:1.133 src/sys/arch/x86/x86/intr.c:1.134
--- src/sys/arch/x86/x86/intr.c:1.133	Sun Oct  7 05:23:01 2018
+++ src/sys/arch/x86/x86/intr.c	Mon Oct  8 08:05:08 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.133 2018/10/07 05:23:01 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.134 2018/10/08 08:05:08 cherry Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.133 2018/10/07 05:23:01 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.134 2018/10/08 08:05:08 cherry Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1278,12 +1278,24 @@ intr_establish_xname(int legacy_irq, str
 
 	vector = xen_vec_alloc(gsi);
 
-	extern struct cpu_info phycpu_info_primary; /* XXX */
-	struct cpu_info *ci = _info_primary;
-	pic->pic_addroute(pic, ci, pin, vector, type);
-
-	evtchn = irq2port[vect2irq[vector]];
-	KASSERT(evtchn > 0);
+	if (irq2port[gsi] == 0) {
+		extern struct cpu_info phycpu_info_primary; /* XXX */
+		struct cpu_info *ci = _info_primary;
+
+		pic->pic_addroute(pic, ci, pin, vector, type);
+
+		evtchn = bind_pirq_to_evtch(gsi);
+		KASSERT(evtchn > 0);
+		KASSERT(evtchn < NR_EVENT_CHANNELS);
+		irq2port[gsi] = evtchn + 1;
+		xen_atomic_set_bit(>ci_evtmask[0], evtchn);
+	} else {
+		/*
+		 * Shared interrupt - we can't rebind.
+		 * The port is shared instead.
+		 */
+		evtchn = irq2port[gsi];
+	}
 
 	pih = pirq_establish(gsi, evtchn, handler, arg, level,
 			 intrstr, 

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

2018-10-07 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Sun Oct  7 16:36:36 UTC 2018

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

Log Message:
In the case of a shared GSI, bind will fail, so we do not attempt this.
The sharing is accomplished by demultiplexing the port event of the first
bind. This is accomplished in intr.c:intr_establish_xname()

Note that the pic_delroute() is buggy (commented suitably) for the shared
gsi case, since it will unbind reset it unconditionally, leaving the other
shared callbacks stranded.

This problem will go awaywhen we unify further with native code, as this
case is taken care of appropriately in that case.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/x86/x86/i8259.c
cvs rdiff -u -r1.57 -r1.58 src/sys/arch/x86/x86/ioapic.c

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

Modified files:

Index: src/sys/arch/x86/x86/i8259.c
diff -u src/sys/arch/x86/x86/i8259.c:1.19 src/sys/arch/x86/x86/i8259.c:1.20
--- src/sys/arch/x86/x86/i8259.c:1.19	Sun Oct  7 05:28:51 2018
+++ src/sys/arch/x86/x86/i8259.c	Sun Oct  7 16:36:36 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: i8259.c,v 1.19 2018/10/07 05:28:51 cherry Exp $	*/
+/*	$NetBSD: i8259.c,v 1.20 2018/10/07 16:36:36 cherry Exp $	*/
 
 /*
  * Copyright 2002 (c) Wasabi Systems, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.19 2018/10/07 05:28:51 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.20 2018/10/07 16:36:36 cherry Exp $");
 
 #include  
 #include 
@@ -267,6 +267,14 @@ i8259_setup(struct pic *pic, struct cpu_
 	int port, irq;
 	irq = vect2irq[idtvec];
 	KASSERT(irq != 0);
+	if (irq2port[irq] != 0) {
+		/* 
+		 * Shared interrupt - we can't rebind.
+		 *  The port is shared instead.
+		 */
+		return;
+	}
+	
 	port = bind_pirq_to_evtch(irq);
 	KASSERT(port < NR_EVENT_CHANNELS);
 	KASSERT(port >= 0);
@@ -292,6 +300,7 @@ i8259_unsetup(struct pic *pic, struct cp
 
 	KASSERT(port < NR_EVENT_CHANNELS);
 
+	/* XXX: This is problematic for shared interrupts */
 	KASSERT(irq2port[irq] != 0);
 	irq2port[irq] = 0;
 

Index: src/sys/arch/x86/x86/ioapic.c
diff -u src/sys/arch/x86/x86/ioapic.c:1.57 src/sys/arch/x86/x86/ioapic.c:1.58
--- src/sys/arch/x86/x86/ioapic.c:1.57	Sun Oct  7 05:28:51 2018
+++ src/sys/arch/x86/x86/ioapic.c	Sun Oct  7 16:36:36 2018
@@ -1,4 +1,4 @@
-/* 	$NetBSD: ioapic.c,v 1.57 2018/10/07 05:28:51 cherry Exp $	*/
+/* 	$NetBSD: ioapic.c,v 1.58 2018/10/07 16:36:36 cherry Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2009 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ioapic.c,v 1.57 2018/10/07 05:28:51 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ioapic.c,v 1.58 2018/10/07 16:36:36 cherry Exp $");
 
 #include "opt_ddb.h"
 
@@ -576,6 +576,15 @@ ioapic_addroute(struct pic *pic, struct 
 	int port, irq;
 	irq = vect2irq[idtvec];
 	KASSERT(irq != 0);
+
+	if (irq2port[irq] != 0) {
+		/* 
+		 * Shared interrupt - we can't rebind.
+		 *  The port is shared instead.
+		 */
+		return;
+	}
+
 	port = bind_pirq_to_evtch(irq);
 	KASSERT(port < NR_EVENT_CHANNELS);
 	KASSERT(port >= 0);
@@ -602,6 +611,7 @@ ioapic_delroute(struct pic *pic, struct 
 
 	KASSERT(port < NR_EVENT_CHANNELS);
 
+	/* XXX: This is problematic for shared interrupts */
 	KASSERT(irq2port[irq] != 0);
 	irq2port[irq] = 0;
 



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

2018-10-06 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Sun Oct  7 05:28:51 UTC 2018

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

Log Message:
While we're here, fix pic->pic_delroute() to DTRT on XEN and
cleanup after itself.


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

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

Modified files:

Index: src/sys/arch/x86/x86/i8259.c
diff -u src/sys/arch/x86/x86/i8259.c:1.18 src/sys/arch/x86/x86/i8259.c:1.19
--- src/sys/arch/x86/x86/i8259.c:1.18	Sun Oct  7 05:23:01 2018
+++ src/sys/arch/x86/x86/i8259.c	Sun Oct  7 05:28:51 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: i8259.c,v 1.18 2018/10/07 05:23:01 cherry Exp $	*/
+/*	$NetBSD: i8259.c,v 1.19 2018/10/07 05:28:51 cherry Exp $	*/
 
 /*
  * Copyright 2002 (c) Wasabi Systems, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.18 2018/10/07 05:23:01 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.19 2018/10/07 05:28:51 cherry Exp $");
 
 #include  
 #include 
@@ -291,6 +291,11 @@ i8259_unsetup(struct pic *pic, struct cp
 	port = unbind_pirq_from_evtch(irq);
 
 	KASSERT(port < NR_EVENT_CHANNELS);
+
+	KASSERT(irq2port[irq] != 0);
+	irq2port[irq] = 0;
+
+	xen_atomic_clear_bit(>ci_evtmask[0], port);
 #else
 	if (CPU_IS_PRIMARY(ci))
 		i8259_reinit_irqs();

Index: src/sys/arch/x86/x86/ioapic.c
diff -u src/sys/arch/x86/x86/ioapic.c:1.56 src/sys/arch/x86/x86/ioapic.c:1.57
--- src/sys/arch/x86/x86/ioapic.c:1.56	Wed Dec 13 16:30:18 2017
+++ src/sys/arch/x86/x86/ioapic.c	Sun Oct  7 05:28:51 2018
@@ -1,4 +1,4 @@
-/* 	$NetBSD: ioapic.c,v 1.56 2017/12/13 16:30:18 bouyer Exp $	*/
+/* 	$NetBSD: ioapic.c,v 1.57 2018/10/07 05:28:51 cherry Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2009 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ioapic.c,v 1.56 2017/12/13 16:30:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ioapic.c,v 1.57 2018/10/07 05:28:51 cherry Exp $");
 
 #include "opt_ddb.h"
 
@@ -601,6 +601,12 @@ ioapic_delroute(struct pic *pic, struct 
 	port = unbind_pirq_from_evtch(irq);
 
 	KASSERT(port < NR_EVENT_CHANNELS);
+
+	KASSERT(irq2port[irq] != 0);
+	irq2port[irq] = 0;
+
+	xen_atomic_clear_bit(>ci_evtmask[0], port);
+
 #endif
 
 }



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

2018-09-29 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Sat Sep 29 13:19:38 UTC 2018

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

Log Message:
For i386 XEN3PAE_DOM0, use the "native" idt registration
infrastructure by removing the #ifndef XEN clause.

This will hopefully be the last commit to "fix" boot
breakage of XEN3PAE_DOM0

Thanks to bouyer@ to focussed bug reports with

# xl dmesg
and relevant ddb> bt


To generate a diff of this commit:
cvs rdiff -u -r1.307 -r1.308 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.307 src/sys/arch/x86/x86/pmap.c:1.308
--- src/sys/arch/x86/x86/pmap.c:1.307	Wed Aug 29 16:26:25 2018
+++ src/sys/arch/x86/x86/pmap.c	Sat Sep 29 13:19:38 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.307 2018/08/29 16:26:25 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.308 2018/09/29 13:19:38 cherry Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.307 2018/08/29 16:26:25 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.308 2018/09/29 13:19:38 cherry Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1225,7 +1225,7 @@ pmap_bootstrap(vaddr_t kva_start)
 #endif
 	ldt_paddr = pmap_bootstrap_palloc(1);
 
-#if !defined(__x86_64__) && !defined(XEN)
+#if !defined(__x86_64__)
 	/* pentium f00f bug stuff */
 	pentium_idt_vaddr = pmap_bootstrap_valloc(1);
 #endif



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

2018-09-19 Thread Cherry G. Mathew
Module Name:src
Committed By:   cherry
Date:   Thu Sep 20 05:08:45 UTC 2018

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

Log Message:
When we removed the XEN special case from isa/isa_machdep.c
there was a corner case that was missed in
x86/intr.c:intr_establish_xname()

In isa_machdep.c:isa_intr_establish_xname() the legacy_irq parameter
is never set to -1. It is also incorrect to call
isa_intr_establish_xname() with a legacy_irq parameter of -1.

Thus we infer that whenever we see (legacy_irq == -1) in
intr_establish_xname() which is downstream, we were *NOT* called from
isa_machdep.c:isa_intr_establish_xname()

Given that there are no other users of intr_establish_xnam() which
pass a valid legacy_irq != -1, we assume therefore that we *WERE*
called from isa_machdep.c:isa_intr_establish_xname() in this case.

This is an important distinction in the case where a valid
legacy_irq > NUM_LEGACY_IRQS was passed down from
isa_intr_establish_xname() but was ignored by xen_pirq_alloc() and
overwritten with the "pseudo" irq which is then passed back. We thus
pass the incorrect "legacy" irq value to pirq_establish().

Even though non ISA (ie; PCI and MSI(X) cases), this is the correct
behaviour, we need to maintain (bug?) compatibility with the isa
case.

Thus the one liner diff.

CVS: --
CVS: CVSROOT  cvs.NetBSD.org:/cvsroot
CVS: please use "PR category/123" to have the commitmsg appended to PR 123
CVS:
CVS: Please evaluate your changes and consider the following.
CVS: Abort checkin if you answer no.
CVS: => For all changes:
CVS: Do the changed files compile?
CVS: Has the change been tested?
CVS: => If you are not completely familiar with the changed components:
CVS: Has the change been posted for review?
CVS: Have you allowed enough time for feedback?
CVS: => If the change is major:
CVS: => If the change adds files to, or removes files from $DESTDIR:
CVS: => If you are changing a library or kernel interface:
CVS: Have you successfully run "./build.sh release"?


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 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.129 src/sys/arch/x86/x86/intr.c:1.130
--- src/sys/arch/x86/x86/intr.c:1.129	Fri Sep 14 01:50:51 2018
+++ src/sys/arch/x86/x86/intr.c	Thu Sep 20 05:08:45 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.129 2018/09/14 01:50:51 mrg Exp $	*/
+/*	$NetBSD: intr.c,v 1.130 2018/09/20 05:08:45 cherry Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.129 2018/09/14 01:50:51 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.130 2018/09/20 05:08:45 cherry Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1291,6 +1291,7 @@ intr_establish_xname(int legacy_irq, str
 	sizeof(intrstr_buf));
 
 	evtchn = xen_pirq_alloc(, type);
+	irq = (legacy_irq == -1) ? irq : legacy_irq; /* ISA compat */	
 	pih = pirq_establish(irq & 0xff, evtchn, handler, arg, level,
 	intrstr, xname);
 	pih->pic_type = pic->pic_type;



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

2018-09-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Sep 19 16:23:05 UTC 2018

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

Log Message:
i386 xen is pae


To generate a diff of this commit:
cvs rdiff -u -r1.119 -r1.120 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.119 src/sys/arch/x86/x86/x86_machdep.c:1.120
--- src/sys/arch/x86/x86/x86_machdep.c:1.119	Fri Jul 13 09:37:32 2018
+++ src/sys/arch/x86/x86/x86_machdep.c	Wed Sep 19 16:23:05 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.119 2018/07/13 09:37:32 maxv Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.120 2018/09/19 16:23:05 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.119 2018/07/13 09:37:32 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.120 2018/09/19 16:23:05 maxv Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -89,7 +89,6 @@ static char x86_cpu_idle_text[16];
 
 #ifdef XEN
 char module_machine_amd64_xen[] = "amd64-xen";
-char module_machine_i386_xen[] = "i386-xen";
 char module_machine_i386pae_xen[] = "i386pae-xen";
 #endif
 
@@ -206,14 +205,10 @@ module_init_md(void)
 
 	/* setup module path for XEN kernels */
 #ifdef XEN
-#if defined(amd64)
+#ifdef __x86_64__
 	module_machine = module_machine_amd64_xen;
-#elif defined(i386)
-#ifdef PAE
-	module_machine = module_machine_i386pae_xen;
 #else
-	module_machine = module_machine_i386_xen;
-#endif
+	module_machine = module_machine_i386pae_xen;
 #endif
 #endif
 



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

2018-09-17 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Sep 17 15:53:06 UTC 2018

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

Log Message:
Reduce the noise, reorder and rename some things for clarity.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 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.46 src/sys/arch/x86/x86/fpu.c:1.47
--- src/sys/arch/x86/x86/fpu.c:1.46	Sun Jul  1 08:32:41 2018
+++ src/sys/arch/x86/x86/fpu.c	Mon Sep 17 15:53:06 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.46 2018/07/01 08:32:41 maxv Exp $	*/
+/*	$NetBSD: fpu.c,v 1.47 2018/09/17 15:53:06 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.46 2018/07/01 08:32:41 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.47 2018/09/17 15:53:06 maxv Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -121,9 +121,6 @@ __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.46
 #include 
 #include 
 
-/* Check some duplicate definitions match */
-#include 
-
 #ifdef XEN
 #define clts() HYPERVISOR_fpu_taskswitch(0)
 #define stts() HYPERVISOR_fpu_taskswitch(1)
@@ -134,108 +131,25 @@ bool x86_fpu_eager __read_mostly = false
 static uint32_t x86_fpu_mxcsr_mask __read_mostly = 0;
 
 static inline union savefpu *
-process_fpframe(struct lwp *lwp)
+lwp_fpuarea(struct lwp *l)
 {
-	struct pcb *pcb = lwp_getpcb(lwp);
+	struct pcb *pcb = lwp_getpcb(l);
 
 	return >pcb_savefpu;
 }
 
-/*
- * The following table is used to ensure that the FPE_... value
- * that is passed as a trapcode to the signal handler of the user
- * process does not have more than one bit set.
- *
- * Multiple bits may be set if SSE simd instructions generate errors
- * on more than one value or if the user process modifies the control
- * word while a status word bit is already set (which this is a sign
- * of bad coding).
- * We have no choise than to narrow them down to one bit, since we must
- * not send a trapcode that is not exactly one of the FPE_ macros.
- *
- * The mechanism has a static table with 127 entries.  Each combination
- * of the 7 FPU status word exception bits directly translates to a
- * position in this table, where a single FPE_... value is stored.
- * This FPE_... value stored there is considered the "most important"
- * of the exception bits and will be sent as the signal code.  The
- * precedence of the bits is based upon Intel Document "Numerical
- * Applications", Chapter "Special Computational Situations".
- *
- * The code to choose one of these values does these steps:
- * 1) Throw away status word bits that cannot be masked.
- * 2) Throw away the bits currently masked in the control word,
- *assuming the user isn't interested in them anymore.
- * 3) Reinsert status word bit 7 (stack fault) if it is set, which
- *cannot be masked but must be preserved.
- *'Stack fault' is a sub-class of 'invalid operation'.
- * 4) Use the remaining bits to point into the trapcode table.
- *
- * The 6 maskable bits in order of their preference, as stated in the
- * above referenced Intel manual:
- * 1  Invalid operation (FP_X_INV)
- * 1a   Stack underflow
- * 1b   Stack overflow
- * 1c   Operand of unsupported format
- * 1d   SNaN operand.
- * 2  QNaN operand (not an exception, irrelevant here)
- * 3  Any other invalid-operation not mentioned above or zero divide
- *  (FP_X_INV, FP_X_DZ)
- * 4  Denormal operand (FP_X_DNML)
- * 5  Numeric over/underflow (FP_X_OFL, FP_X_UFL)
- * 6  Inexact result (FP_X_IMP)
- *
- * NB: the above seems to mix up the mxscr error bits and the x87 ones.
- * They are in the same order, but there is no EN_SW_STACK_FAULT in the mmx
- * status.
- *
- * The table is nearly, but not quite, in bit order (ZERODIV and DENORM
- * are swapped).
- *
- * This table assumes that any stack fault is cleared - so that an INVOP
- * fault will only be reported as FLTSUB once.
- * This might not happen if the mask is being changed.
- */
-#define FPE_xxx1(f) (f & EN_SW_INVOP \
-		? (f & EN_SW_STACK_FAULT ? FPE_FLTSUB : FPE_FLTINV) \
-	: f & EN_SW_ZERODIV ? FPE_FLTDIV \
-	: f & EN_SW_DENORM ? FPE_FLTUND \
-	: f & EN_SW_OVERFLOW ? FPE_FLTOVF \
-	: f & EN_SW_UNDERFLOW ? FPE_FLTUND \
-	: f & EN_SW_PRECLOSS ? FPE_FLTRES \
-	: f & EN_SW_STACK_FAULT ? FPE_FLTSUB : 0)
-#define	FPE_xxx2(f)	FPE_xxx1(f),	FPE_xxx1((f + 1))
-#define	FPE_xxx4(f)	FPE_xxx2(f),	FPE_xxx2((f + 2))
-#define	FPE_xxx8(f)	FPE_xxx4(f),	FPE_xxx4((f + 4))
-#define	FPE_xxx16(f)	FPE_xxx8(f),	FPE_xxx8((f + 8))
-#define	FPE_xxx32(f)	FPE_xxx16(f),	FPE_xxx16((f + 16))
-static const uint8_t fpetable[128] = {
-	FPE_xxx32(0), FPE_xxx32(32), FPE_xxx32(64), FPE_xxx32(96)
-};
-#undef FPE_xxx1
-#undef FPE_xxx2
-#undef FPE_xxx4
-#undef FPE_xxx8
-#undef FPE_xxx16
-#undef FPE_xxx32
-
-/*
- * Init the FPU.
- *
- * This 

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

2018-09-13 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Fri Sep 14 01:50:51 UTC 2018

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

Log Message:
fix a !MP build issue.


To generate a diff of this commit:
cvs rdiff -u -r1.128 -r1.129 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.128 src/sys/arch/x86/x86/intr.c:1.129
--- src/sys/arch/x86/x86/intr.c:1.128	Mon Sep 10 05:08:55 2018
+++ src/sys/arch/x86/x86/intr.c	Fri Sep 14 01:50:51 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.128 2018/09/10 05:08:55 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.129 2018/09/14 01:50:51 mrg Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.128 2018/09/10 05:08:55 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.129 2018/09/14 01:50:51 mrg Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1462,9 +1462,11 @@ cpu_intr_init(struct cpu_info *ci)
 {
 #if !defined(XEN)
 	struct intrsource *isp;
-#if NLAPIC > 0 && defined(MULTIPROCESSOR)
-	int i;
+#if NLAPIC > 0
 	static int first = 1;
+#if defined(MULTIPROCESSOR)
+	int i;
+#endif
 #endif
 
 #if NLAPIC > 0



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

2018-08-18 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Aug 18 08:45:55 UTC 2018

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

Log Message:
Simplify the conditions. Fixes compilation of native amd64 without direct
map.


To generate a diff of this commit:
cvs rdiff -u -r1.302 -r1.303 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.302 src/sys/arch/x86/x86/pmap.c:1.303
--- src/sys/arch/x86/x86/pmap.c:1.302	Sun Aug 12 15:31:01 2018
+++ src/sys/arch/x86/x86/pmap.c	Sat Aug 18 08:45:55 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.302 2018/08/12 15:31:01 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.303 2018/08/18 08:45:55 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -157,7 +157,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.302 2018/08/12 15:31:01 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.303 2018/08/18 08:45:55 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1375,8 +1375,7 @@ pmap_init_lapic(void)
 }
 #endif
 
-#if defined(__HAVE_PCPU_AREA) || defined(__HAVE_DIRECT_MAP) || \
-(defined(XEN) && defined(__x86_64__))
+#ifdef __x86_64__
 static size_t
 pmap_pagetree_nentries_range(vaddr_t startva, vaddr_t endva, size_t pgsz)
 {
@@ -1398,7 +1397,7 @@ slotspace_copy(int type, pd_entry_t *dst
 }
 #endif
 
-#if defined(__HAVE_DIRECT_MAP) || (defined(XEN) && defined(__x86_64__))
+#ifdef __x86_64__
 vaddr_t slotspace_rand(int, size_t, size_t);
 
 /*



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

2018-08-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Aug 12 11:51:42 UTC 2018

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

Log Message:
Reduce the minefield: zero out the pdir only once, at the beginning of
the function. This eliminates one assumption on the order of the VM
areas.


To generate a diff of this commit:
cvs rdiff -u -r1.298 -r1.299 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.298 src/sys/arch/x86/x86/pmap.c:1.299
--- src/sys/arch/x86/x86/pmap.c:1.298	Sun Aug 12 10:50:35 2018
+++ src/sys/arch/x86/x86/pmap.c	Sun Aug 12 11:51:42 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.298 2018/08/12 10:50:35 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.299 2018/08/12 11:51:42 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -157,7 +157,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.298 2018/08/12 10:50:35 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.299 2018/08/12 11:51:42 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -2318,6 +2318,8 @@ pmap_pdp_ctor(void *arg, void *v, int fl
 	int s;
 #endif
 
+	memset(pdir, 0, PDP_SIZE * PAGE_SIZE);
+
 	/*
 	 * NOTE: The `pmaps_lock' is held when the PDP is allocated.
 	 */
@@ -2326,9 +2328,6 @@ pmap_pdp_ctor(void *arg, void *v, int fl
 	/* Fetch the physical address of the page directory */
 	(void)pmap_extract(pmap_kernel(), (vaddr_t)pdir, );
 
-	/* Zero the area */
-	memset(pdir, 0, PAGE_SIZE); /* Xen wants a clean page */
-
 	/*
 	 * This pdir will NEVER be active in kernel mode, so mark
 	 * recursive entry invalid.
@@ -2345,9 +2344,6 @@ pmap_pdp_ctor(void *arg, void *v, int fl
 	pdir[PDIR_SLOT_KERN + nkptp[PTP_LEVELS - 1] - 1] =
 	 (pd_entry_t)-1 & PG_FRAME;
 #else /* XEN && __x86_64__*/
-	/* Zero the area */
-	memset(pdir, 0, PDIR_SLOT_PTE * sizeof(pd_entry_t));
-
 	object = (vaddr_t)v;
 	for (i = 0; i < PDP_SIZE; i++, object += PAGE_SIZE) {
 		/* Fetch the physical address of the page directory */
@@ -2367,10 +2363,6 @@ pmap_pdp_ctor(void *arg, void *v, int fl
 	memcpy([PDIR_SLOT_KERN], _BASE[PDIR_SLOT_KERN],
 	npde * sizeof(pd_entry_t));
 
-	/* Zero the rest */
-	memset([PDIR_SLOT_KERN + npde], 0, (PAGE_SIZE * PDP_SIZE) -
-	(PDIR_SLOT_KERN + npde) * sizeof(pd_entry_t));
-
 	if (VM_MIN_KERNEL_ADDRESS != KERNBASE) {
 		int idx = pl_i(KERNBASE, PTP_LEVELS);
 		pdir[idx] = PDP_BASE[idx];



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

2018-08-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Aug 12 10:45:27 UTC 2018

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

Log Message:
Take the last area into account, there is a hole before it.


To generate a diff of this commit:
cvs rdiff -u -r1.296 -r1.297 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.296 src/sys/arch/x86/x86/pmap.c:1.297
--- src/sys/arch/x86/x86/pmap.c:1.296	Sun Aug 12 08:17:50 2018
+++ src/sys/arch/x86/x86/pmap.c	Sun Aug 12 10:45:27 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.296 2018/08/12 08:17:50 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.297 2018/08/12 10:45:27 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -157,7 +157,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.296 2018/08/12 08:17:50 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.297 2018/08/12 10:45:27 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1425,7 +1425,7 @@ slotspace_rand(int type, size_t sz, size
 		 */
 		size_t minsslot = 512;
 		size_t minnslot = 0;
-		for (i = 0; i < SLSPACE_NAREAS-1; i++) {
+		for (i = 0; i < SLSPACE_NAREAS; i++) {
 			if (!slotspace.area[i].active)
 continue;
 			if (slotspace.area[i].sslot >= curslot &&



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

2018-08-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Aug 12 05:43:42 UTC 2018

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

Log Message:
enable the two errata for AMD Family 16h, tested by mrg@, thanks


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/x86/x86/errata.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/errata.c
diff -u src/sys/arch/x86/x86/errata.c:1.24 src/sys/arch/x86/x86/errata.c:1.25
--- src/sys/arch/x86/x86/errata.c:1.24	Tue Aug  7 10:50:12 2018
+++ src/sys/arch/x86/x86/errata.c	Sun Aug 12 05:43:42 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: errata.c,v 1.24 2018/08/07 10:50:12 maxv Exp $	*/
+/*	$NetBSD: errata.c,v 1.25 2018/08/12 05:43:42 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -45,7 +45,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: errata.c,v 1.24 2018/08/07 10:50:12 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: errata.c,v 1.25 2018/08/12 05:43:42 maxv Exp $");
 
 #include 
 #include 
@@ -144,11 +144,9 @@ static const uint8_t x86_errata_set11[] 
 	DA_C3, HY_D0, HY_D1, HY_D1_G34R1,  PH_E0, LN_B0, OINK
 };
 
-#ifdef notyet_f16h
 static const uint8_t x86_errata_set12[] = {
 	KB_A1, OINK
 };
-#endif
 
 static const uint8_t x86_errata_set13[] = {
 	ZP_B1, ZP_B2, PiR_B2, OINK
@@ -158,11 +156,9 @@ static const uint8_t x86_errata_set14[] 
 	ZP_B1, OINK
 };
 
-#ifdef notyet_f16h
 static const uint8_t x86_errata_set15[] = {
 	KB_A1, ML_A1, OINK
 };
-#endif
 
 static bool x86_errata_setmsr(struct cpu_info *, errata_t *);
 static bool x86_errata_testmsr(struct cpu_info *, errata_t *);
@@ -307,7 +303,6 @@ static errata_t errata[] = {
 		721, FALSE, MSR_DE_CFG, x86_errata_set11,
 		x86_errata_setmsr, DE_CFG_ERRATA_721
 	},
-#ifdef notyet_f16h	/* TODO: needs to be tested */
 	/*
 	 * 776: Incorrect Processor Branch Prediction for Two Consecutive
 	 * Linear Pages
@@ -324,7 +319,6 @@ static errata_t errata[] = {
 		793, FALSE, MSR_LS_CFG, x86_errata_set15,
 		x86_errata_setmsr, LS_CFG_ERRATA_793
 	},
-#endif
 	/*
 	 * 1021: Load Operation May Receive Stale Data From Older Store
 	 * Operation



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

2018-07-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Jul 26 08:22:19 UTC 2018

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

Log Message:
Remove useless/outdated comments. No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.159 -r1.160 src/sys/arch/x86/x86/cpu.c
cvs rdiff -u -r1.293 -r1.294 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/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.159 src/sys/arch/x86/x86/cpu.c:1.160
--- src/sys/arch/x86/x86/cpu.c:1.159	Thu Jul 12 18:39:09 2018
+++ src/sys/arch/x86/x86/cpu.c	Thu Jul 26 08:22:19 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.159 2018/07/12 18:39:09 maxv Exp $	*/
+/*	$NetBSD: cpu.c,v 1.160 2018/07/26 08:22:19 maxv Exp $	*/
 
 /*
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.159 2018/07/12 18:39:09 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.160 2018/07/26 08:22:19 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -1313,9 +1313,9 @@ cpu_load_pmap(struct pmap *pmap, struct 
 	if (interrupts_enabled)
 		x86_enable_intr();
 	tlbflush();
-#else /* PAE */
+#else
 	lcr3(pmap_pdirpa(pmap, 0));
-#endif /* PAE */
+#endif
 }
 
 /*

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.293 src/sys/arch/x86/x86/pmap.c:1.294
--- src/sys/arch/x86/x86/pmap.c:1.293	Sat Jul 21 21:26:30 2018
+++ src/sys/arch/x86/x86/pmap.c	Thu Jul 26 08:22:19 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.293 2018/07/21 21:26:30 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.294 2018/07/26 08:22:19 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -130,19 +130,6 @@
  */
 
 /*
- * This is the i386 pmap modified and generalized to support x86-64
- * as well. The idea is to hide the upper N levels of the page tables
- * inside pmap_get_ptp, pmap_free_ptp and pmap_growkernel. The rest
- * is mostly untouched, except that it uses some more generalized
- * macros and interfaces.
- *
- * This pmap has been tested on the i386 as well, and it can be easily
- * adapted to PAE.
- *
- * f...@wasabisystems.com 18-Jun-2001
- */
-
-/*
  * pmap.c: i386 pmap module rewrite
  * Chuck Cranor 
  * 11-Aug-97
@@ -170,7 +157,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.293 2018/07/21 21:26:30 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.294 2018/07/26 08:22:19 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -524,6 +511,7 @@ int pmap_enter_default(pmap_t, vaddr_t, 
 struct pool_cache pmap_pdp_cache;
 static int  pmap_pdp_ctor(void *, void *, int);
 static void pmap_pdp_dtor(void *, void *);
+
 #ifdef PAE
 /* need to allocate items of 4 pages */
 static void *pmap_pdp_alloc(struct pool *, int);
@@ -533,7 +521,7 @@ static struct pool_allocator pmap_pdp_al
 	.pa_free = pmap_pdp_free,
 	.pa_pagesz = PAGE_SIZE * PDP_SIZE,
 };
-#endif /* PAE */
+#endif
 
 extern vaddr_t idt_vaddr;
 extern paddr_t idt_paddr;
@@ -1811,17 +1799,18 @@ pmap_init(void)
 	 * are pinned on xen and R/O for the domU
 	 */
 	flags = PR_NOTOUCH;
-#else /* XEN */
+#else
 	flags = 0;
-#endif /* XEN */
+#endif
+
 #ifdef PAE
 	pool_cache_bootstrap(_pdp_cache, PAGE_SIZE * PDP_SIZE, 0, 0, flags,
 	"pdppl", _pdp_allocator, IPL_NONE,
 	pmap_pdp_ctor, pmap_pdp_dtor, NULL);
-#else /* PAE */
+#else
 	pool_cache_bootstrap(_pdp_cache, PAGE_SIZE, 0, 0, flags,
 	"pdppl", NULL, IPL_NONE, pmap_pdp_ctor, pmap_pdp_dtor, NULL);
-#endif /* PAE */
+#endif
 	pool_cache_bootstrap(_pv_cache, sizeof(struct pv_entry), 0, 0,
 	PR_LARGECACHE, "pvpl", _allocator_kmem, IPL_NONE, NULL,
 	NULL, NULL);
@@ -3032,8 +3021,8 @@ pmap_load(void)
 #ifndef XEN
 	ci->ci_tss->tss.tss_ldt = pmap->pm_ldt_sel;
 	ci->ci_tss->tss.tss_cr3 = pcb->pcb_cr3;
-#endif /* !XEN */
-#endif /* i386 */
+#endif
+#endif
 
 	lldt(pmap->pm_ldt_sel);
 
@@ -4652,7 +4641,7 @@ pmap_growkernel(vaddr_t maxkvaddr)
 #ifdef XEN
 #ifdef __x86_64__
 		/* nothing, kernel entries are never entered in user pmap */
-#else /* __x86_64__ */
+#else
 		int pdkidx;
 #ifndef PAE
 		/*



<    1   2   3   4   5   6   7   8   >