Module Name:    src
Committed By:   jdolecek
Date:           Mon Feb 19 22:01:16 UTC 2018

Modified Files:
        src/sys/uvm/pmap: pmap_tlb.c pmap_tlb.h

Log Message:
convert to use actual __BITMAP_*() macros from <sys/bitops.h>, and make
it possible to override the ASID bitmap length; default to 256 ASIDs as before

XXX NFCI; compile tested only on evbpcc and evbmips, unfortunately didn't
find any combination of port using the MI pmap_tlb.c and working in QEMU


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/uvm/pmap/pmap_tlb.c
cvs rdiff -u -r1.12 -r1.13 src/sys/uvm/pmap/pmap_tlb.h

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

Modified files:

Index: src/sys/uvm/pmap/pmap_tlb.c
diff -u src/sys/uvm/pmap/pmap_tlb.c:1.24 src/sys/uvm/pmap/pmap_tlb.c:1.25
--- src/sys/uvm/pmap/pmap_tlb.c:1.24	Mon Feb 19 21:40:45 2018
+++ src/sys/uvm/pmap/pmap_tlb.c	Mon Feb 19 22:01:15 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap_tlb.c,v 1.24 2018/02/19 21:40:45 jdolecek Exp $	*/
+/*	$NetBSD: pmap_tlb.c,v 1.25 2018/02/19 22:01:15 jdolecek Exp $	*/
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(0, "$NetBSD: pmap_tlb.c,v 1.24 2018/02/19 21:40:45 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap_tlb.c,v 1.25 2018/02/19 22:01:15 jdolecek Exp $");
 
 /*
  * Manages address spaces in a TLB.
@@ -140,6 +140,10 @@ static kmutex_t pmap_tlb0_lock __cacheli
 
 #define	IFCONSTANT(x)	(__builtin_constant_p((x)) ? (x) : 0)
 
+#if KERNEL_PID > 31
+#error "KERNEL_PID expected in range 0-31"
+#endif
+
 struct pmap_tlb_info pmap_tlb0_info = {
 	.ti_name = "tlb0",
 	.ti_asid_hint = KERNEL_PID + 1,
@@ -147,7 +151,7 @@ struct pmap_tlb_info pmap_tlb0_info = {
 	.ti_asid_max = IFCONSTANT(PMAP_TLB_NUM_PIDS - 1),
 	.ti_asids_free = IFCONSTANT(PMAP_TLB_NUM_PIDS - (1 + KERNEL_PID)),
 #endif
-	.ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1,
+	.ti_asid_bitmap._b[0] = __BIT(KERNEL_PID),
 #ifdef PMAP_TLB_WIRED_UPAGES
 	.ti_wired = PMAP_TLB_WIRED_UPAGES,
 #endif
@@ -167,22 +171,19 @@ struct pmap_tlb_info *pmap_tlbs[PMAP_TLB
 u_int pmap_ntlbs = 1;
 #endif
 
-#define	__BITMAP_SET(bm, n) \
-	((bm)[(n) / (8*sizeof(bm[0]))] |= 1LU << ((n) % (8*sizeof(bm[0]))))
-#define	__BITMAP_CLR(bm, n) \
-	((bm)[(n) / (8*sizeof(bm[0]))] &= ~(1LU << ((n) % (8*sizeof(bm[0])))))
-#define	__BITMAP_ISSET_P(bm, n) \
-	(((bm)[(n) / (8*sizeof(bm[0]))] & (1LU << ((n) % (8*sizeof(bm[0]))))) != 0)
-
 #define	TLBINFO_ASID_MARK_UNUSED(ti, asid) \
-	__BITMAP_CLR((ti)->ti_asid_bitmap, (asid))
+	__BITMAP_CLR((asid), &(ti)->ti_asid_bitmap)
 #define	TLBINFO_ASID_MARK_USED(ti, asid) \
-	__BITMAP_SET((ti)->ti_asid_bitmap, (asid))
+	__BITMAP_SET((asid), &(ti)->ti_asid_bitmap)
 #define	TLBINFO_ASID_INUSE_P(ti, asid) \
-	__BITMAP_ISSET_P((ti)->ti_asid_bitmap, (asid))
+	__BITMAP_ISSET((asid), &(ti)->ti_asid_bitmap)
+#define	TLBINFO_ASID_RESET(ti) \
+	do {						\
+		__BITMAP_ZERO(&ti->ti_asid_bitmap);	\
+		TLBINFO_ASID_MARK_USED(ti, KERNEL_PID); \
+	} while (0)
 #define	TLBINFO_ASID_INITIAL_FREE(ti) \
 	((ti)->ti_asid_max + 1 /* 0 */ - 1 /* reserved KERNEL_PID */)
-	
 
 #ifdef MULTIPROCESSOR
 __unused static inline bool
@@ -337,7 +338,7 @@ pmap_tlb_info_init(struct pmap_tlb_info 
 		KASSERT(pmap_tlbs[pmap_ntlbs] == NULL);
 
 		ti->ti_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
-		ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
+		TLBINFO_ASID_MARK_USED(ti, KERNEL_PID);
 		ti->ti_asid_hint = KERNEL_PID + 1;
 		ti->ti_asid_max = pmap_tlbs[0]->ti_asid_max;
 		ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(ti);
@@ -350,6 +351,8 @@ pmap_tlb_info_init(struct pmap_tlb_info 
 		snprintf(ti->ti_name, sizeof(ti->ti_name), "tlb%u",
 		    ti->ti_index);
 		pmap_tlb_info_evcnt_attach(ti);
+
+		KASSERT(ti->ti_asid_max < PMAP_TLB_BITMAP_LENGTH);
 		return;
 	}
 #endif
@@ -368,7 +371,7 @@ pmap_tlb_info_init(struct pmap_tlb_info 
 		ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(ti);
 	}
 
-	KASSERT(ti->ti_asid_max < sizeof(ti->ti_asid_bitmap)*8);
+	KASSERT(ti->ti_asid_max < PMAP_TLB_BITMAP_LENGTH);
 }
 
 #if defined(MULTIPROCESSOR)
@@ -413,9 +416,6 @@ pmap_tlb_asid_count(struct pmap_tlb_info
 static void
 pmap_tlb_asid_reinitialize(struct pmap_tlb_info *ti, enum tlb_invalidate_op op)
 {
-	const size_t asid_bitmap_words =
-	    ti->ti_asid_max / (8 * sizeof(ti->ti_asid_bitmap[0]));
-
 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
 	UVMHIST_LOG(maphist, "(ti=%#jx, op=%ju)", (uintptr_t)ti, op, 0, 0);
 
@@ -429,10 +429,7 @@ pmap_tlb_asid_reinitialize(struct pmap_t
 	 */
 	ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(ti);
 	ti->ti_asid_hint = KERNEL_PID + 1;
-	ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
-	for (size_t word = 1; word <= asid_bitmap_words; word++) {
-		ti->ti_asid_bitmap[word] = 0;
-	}
+	TLBINFO_ASID_RESET(ti);
 
 	switch (op) {
 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
@@ -455,8 +452,8 @@ pmap_tlb_asid_reinitialize(struct pmap_t
 		 */
 #if !defined(MULTIPROCESSOR) || defined(PMAP_TLB_NEED_SHOOTDOWN)
 		pmap_tlb_asid_check();
-		const u_int asids_found = tlb_record_asids(ti->ti_asid_bitmap,
-		    ti->ti_asid_max);
+		const u_int asids_found = tlb_record_asids(
+		    ti->ti_asid_bitmap._b, ti->ti_asid_max);
 		pmap_tlb_asid_check();
 		KASSERT(asids_found == pmap_tlb_asid_count(ti));
 		if (__predict_false(asids_found >= ti->ti_asid_max / 2)) {
@@ -473,12 +470,7 @@ pmap_tlb_asid_reinitialize(struct pmap_t
 			 */
 			tlb_invalidate_all();
 #endif /* MULTIPROCESSOR && !PMAP_TLB_NEED_SHOOTDOWN */
-			ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
-			for (size_t word = 1;
-			     word <= asid_bitmap_words;
-			     word++) {
-				ti->ti_asid_bitmap[word] = 0;
-			}
+			TLBINFO_ASID_RESET(ti);
 			ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(ti);
 #if !defined(MULTIPROCESSOR) || defined(PMAP_TLB_NEED_SHOOTDOWN)
 		} else {
@@ -829,11 +821,11 @@ pmap_tlb_asid_alloc(struct pmap_tlb_info
 	 * a new one.
 	 */
 	if (__predict_true(TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint))) {
-		const size_t nbpw = 8 * sizeof(ti->ti_asid_bitmap[0]);
+		const size_t nbpw = NBBY * sizeof(ti->ti_asid_bitmap._b[0]);
 		size_t i;
 		u_long bits;
-		for (i = 0; (bits = ~ti->ti_asid_bitmap[i]) == 0; i++) {
-			KASSERT(i < __arraycount(ti->ti_asid_bitmap) - 1);
+		for (i = 0; (bits = ~ti->ti_asid_bitmap._b[i]) == 0; i++) {
+			KASSERT(i < __arraycount(ti->ti_asid_bitmap._b) - 1);
 		}
 		/*
 		 * ffs wants to find the first bit set while we want
@@ -850,9 +842,9 @@ pmap_tlb_asid_alloc(struct pmap_tlb_info
 	KASSERT(ti->ti_asid_hint <= ti->ti_asid_max);
 	KASSERTMSG(PMAP_TLB_FLUSH_ASID_ON_RESET
 	    || TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint - 1),
-	    "hint %u bitmap %p", ti->ti_asid_hint, ti->ti_asid_bitmap);
+	    "hint %u bitmap %p", ti->ti_asid_hint, &ti->ti_asid_bitmap);
 	KASSERTMSG(!TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint),
-	    "hint %u bitmap %p", ti->ti_asid_hint, ti->ti_asid_bitmap);
+	    "hint %u bitmap %p", ti->ti_asid_hint, &ti->ti_asid_bitmap);
 
 	/*
 	 * The hint contains our next ASID so take it and advance the hint.

Index: src/sys/uvm/pmap/pmap_tlb.h
diff -u src/sys/uvm/pmap/pmap_tlb.h:1.12 src/sys/uvm/pmap/pmap_tlb.h:1.13
--- src/sys/uvm/pmap/pmap_tlb.h:1.12	Mon Feb 19 21:20:33 2018
+++ src/sys/uvm/pmap/pmap_tlb.h	Mon Feb 19 22:01:15 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap_tlb.h,v 1.12 2018/02/19 21:20:33 jdolecek Exp $	*/
+/*	$NetBSD: pmap_tlb.h,v 1.13 2018/02/19 22:01:15 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -76,6 +76,7 @@
 
 #include <sys/evcnt.h>
 #include <sys/kcpuset.h>
+#include <sys/bitops.h>
 
 #if !defined(PMAP_TLB_MAX)
 # if defined(MULTIPROCESSOR)
@@ -141,7 +142,10 @@ struct pmap_tlb_info {
 #define tlbinfo_index(ti)	((void)(ti), 0)
 #endif
 	struct evcnt ti_evcnt_asid_reinits;
-	u_long ti_asid_bitmap[256 / (sizeof(u_long) * 8)];
+#ifndef PMAP_TLB_BITMAP_LENGTH
+#define	PMAP_TLB_BITMAP_LENGTH 256
+#endif
+	__BITMAP_TYPE(, u_long, PMAP_TLB_BITMAP_LENGTH) ti_asid_bitmap;
 };
 
 #ifdef	_KERNEL

Reply via email to