xref: /titanic_53/usr/src/uts/sun4u/os/memscrub.c (revision 56f33205c9ed776c3c909e07d52e94610a675740)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
561ef38f7Svb70745  * Common Development and Distribution License (the "License").
661ef38f7Svb70745  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*56f33205SJonathan Adams  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * sun4u Memory Scrubbing
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * On detection of a correctable memory ECC error, the sun4u kernel
307c478bd9Sstevel@tonic-gate  * returns the corrected data to the requester and re-writes it
317c478bd9Sstevel@tonic-gate  * to memory (DRAM).  So if the correctable error was transient,
327c478bd9Sstevel@tonic-gate  * the read has effectively been cleaned (scrubbed) from memory.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Scrubbing thus reduces the likelyhood that multiple transient errors
357c478bd9Sstevel@tonic-gate  * will occur in the same memory word, making uncorrectable errors due
367c478bd9Sstevel@tonic-gate  * to transients less likely.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Thus is born the desire that every memory location be periodically
397c478bd9Sstevel@tonic-gate  * accessed.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * This file implements a memory scrubbing thread.  This scrubber
427c478bd9Sstevel@tonic-gate  * guarantees that all of physical memory is accessed periodically
437c478bd9Sstevel@tonic-gate  * (memscrub_period_sec -- 12 hours).
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * It attempts to do this as unobtrusively as possible.  The thread
467c478bd9Sstevel@tonic-gate  * schedules itself to wake up at an interval such that if it reads
4794836d42Spt157919  * memscrub_span_pages (32MB) on each wakeup, it will read all of physical
487c478bd9Sstevel@tonic-gate  * memory in in memscrub_period_sec (12 hours).
497c478bd9Sstevel@tonic-gate  *
5094836d42Spt157919  * The scrubber uses the block load and prefetch hardware to read memory
5194836d42Spt157919  * @ 1300MB/s, so it reads spans of 32MB in 0.025 seconds.  Unlike the
5294836d42Spt157919  * original sun4d scrubber the sun4u scrubber does not read ahead if the
5394836d42Spt157919  * system is idle because we can read memory very efficently.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * The scrubber maintains a private copy of the phys_install memory list
567c478bd9Sstevel@tonic-gate  * to keep track of what memory should be scrubbed.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * The global routines memscrub_add_span() and memscrub_delete_span() are
597c478bd9Sstevel@tonic-gate  * used to add and delete from this list.  If hotplug memory is later
607c478bd9Sstevel@tonic-gate  * supported these two routines can be used to notify the scrubber of
617c478bd9Sstevel@tonic-gate  * memory configuration changes.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * The following parameters can be set via /etc/system
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * memscrub_span_pages = MEMSCRUB_DFL_SPAN_PAGES (8MB)
667c478bd9Sstevel@tonic-gate  * memscrub_period_sec = MEMSCRUB_DFL_PERIOD_SEC (12 hours)
677c478bd9Sstevel@tonic-gate  * memscrub_thread_pri = MEMSCRUB_DFL_THREAD_PRI (MINCLSYSPRI)
687c478bd9Sstevel@tonic-gate  * memscrub_delay_start_sec = (5 minutes)
697c478bd9Sstevel@tonic-gate  * memscrub_verbose = (0)
707c478bd9Sstevel@tonic-gate  * memscrub_override_ticks = (1 tick)
717c478bd9Sstevel@tonic-gate  * disable_memscrub = (0)
727c478bd9Sstevel@tonic-gate  * pause_memscrub = (0)
737c478bd9Sstevel@tonic-gate  * read_all_memscrub = (0)
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * The scrubber will print NOTICE messages of what it is doing if
767c478bd9Sstevel@tonic-gate  * "memscrub_verbose" is set.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * If the scrubber's sleep time calculation drops to zero ticks,
797c478bd9Sstevel@tonic-gate  * memscrub_override_ticks will be used as the sleep time instead. The
8094836d42Spt157919  * sleep time should only drop to zero on a system with over 131.84
817c478bd9Sstevel@tonic-gate  * terabytes of memory, or where the default scrubber parameters have
827c478bd9Sstevel@tonic-gate  * been adjusted. For example, reducing memscrub_span_pages or
837c478bd9Sstevel@tonic-gate  * memscrub_period_sec causes the sleep time to drop to zero with less
847c478bd9Sstevel@tonic-gate  * memory. Note that since the sleep time is calculated in clock ticks,
857c478bd9Sstevel@tonic-gate  * using hires clock ticks allows for more memory before the sleep time
867c478bd9Sstevel@tonic-gate  * becomes zero.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * The scrubber will exit (or never be started) if it finds the variable
897c478bd9Sstevel@tonic-gate  * "disable_memscrub" set.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * The scrubber will pause (not read memory) when "pause_memscrub"
927c478bd9Sstevel@tonic-gate  * is set.  It will check the state of pause_memscrub at each wakeup
937c478bd9Sstevel@tonic-gate  * period.  The scrubber will not make up for lost time.  If you
947c478bd9Sstevel@tonic-gate  * pause the scrubber for a prolonged period of time you can use
957c478bd9Sstevel@tonic-gate  * the "read_all_memscrub" switch (see below) to catch up. In addition,
967c478bd9Sstevel@tonic-gate  * pause_memscrub is used internally by the post memory DR callbacks.
977c478bd9Sstevel@tonic-gate  * It is set for the small period of time during which the callbacks
987c478bd9Sstevel@tonic-gate  * are executing. This ensures "memscrub_lock" will be released,
997c478bd9Sstevel@tonic-gate  * allowing the callbacks to finish.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  * The scrubber will read all memory if "read_all_memscrub" is set.
1027c478bd9Sstevel@tonic-gate  * The normal span read will also occur during the wakeup.
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  * MEMSCRUB_MIN_PAGES (32MB) is the minimum amount of memory a system
1057c478bd9Sstevel@tonic-gate  * must have before we'll start the scrubber.
1067c478bd9Sstevel@tonic-gate  *
10794836d42Spt157919  * MEMSCRUB_DFL_SPAN_PAGES (32MB) is based on the guess that 0.025 sec
1087c478bd9Sstevel@tonic-gate  * is a "good" amount of minimum time for the thread to run at a time.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * MEMSCRUB_DFL_PERIOD_SEC (12 hours) is nearly a total guess --
1117c478bd9Sstevel@tonic-gate  * twice the frequency the hardware folk estimated would be necessary.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * MEMSCRUB_DFL_THREAD_PRI (MINCLSYSPRI) is based on the assumption
1147c478bd9Sstevel@tonic-gate  * that the scurbber should get its fair share of time (since it
1157c478bd9Sstevel@tonic-gate  * is short).  At a priority of 0 the scrubber will be starved.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #include <sys/systm.h>		/* timeout, types, t_lock */
1197c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1207c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>	/* MIN */
1217c478bd9Sstevel@tonic-gate #include <sys/memlist.h>	/* memlist */
1227c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>	/* memory add/delete */
1237c478bd9Sstevel@tonic-gate #include <sys/kmem.h>		/* KMEM_NOSLEEP */
1247c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>		/* ncpus_online */
1257c478bd9Sstevel@tonic-gate #include <sys/debug.h>		/* ASSERTs */
1267c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys */
1277c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>	/* vtag_flushpage */
1287c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
1297c478bd9Sstevel@tonic-gate #include <sys/atomic.h>		/* atomic_add_32 */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #include <vm/hat.h>
1327c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
1337c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>	/* XXX FIXME - delete */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #include <sys/time.h>
1367c478bd9Sstevel@tonic-gate #include <sys/callb.h>		/* CPR callback */
1377c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Should really have paddr_t defined, but it is broken.  Use
1417c478bd9Sstevel@tonic-gate  * ms_paddr_t in the meantime to make the code cleaner
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate typedef uint64_t ms_paddr_t;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Global Routines:
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate int memscrub_add_span(pfn_t pfn, pgcnt_t pages);
1497c478bd9Sstevel@tonic-gate int memscrub_delete_span(pfn_t pfn, pgcnt_t pages);
1507c478bd9Sstevel@tonic-gate int memscrub_init(void);
15161ef38f7Svb70745 void memscrub_induced_error(void);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Global Data:
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * scrub if we have at least this many pages
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate #define	MEMSCRUB_MIN_PAGES (32 * 1024 * 1024 / PAGESIZE)
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * scan all of physical memory at least once every MEMSCRUB_PERIOD_SEC
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate #define	MEMSCRUB_DFL_PERIOD_SEC	(12 * 60 * 60)	/* 12 hours */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * scan at least MEMSCRUB_DFL_SPAN_PAGES each iteration
1697c478bd9Sstevel@tonic-gate  */
17094836d42Spt157919 #define	MEMSCRUB_DFL_SPAN_PAGES	((32 * 1024 * 1024) / PAGESIZE)
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * almost anything is higher priority than scrubbing
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate #define	MEMSCRUB_DFL_THREAD_PRI	MINCLSYSPRI
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * size used when scanning memory
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate #define	MEMSCRUB_BLOCK_SIZE		256
1817c478bd9Sstevel@tonic-gate #define	MEMSCRUB_BLOCK_SIZE_SHIFT	8 	/* log2(MEMSCRUB_BLOCK_SIZE) */
1827c478bd9Sstevel@tonic-gate #define	MEMSCRUB_BLOCKS_PER_PAGE	(PAGESIZE >> MEMSCRUB_BLOCK_SIZE_SHIFT)
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate #define	MEMSCRUB_BPP4M		MMU_PAGESIZE4M >> MEMSCRUB_BLOCK_SIZE_SHIFT
1857c478bd9Sstevel@tonic-gate #define	MEMSCRUB_BPP512K	MMU_PAGESIZE512K >> MEMSCRUB_BLOCK_SIZE_SHIFT
1867c478bd9Sstevel@tonic-gate #define	MEMSCRUB_BPP64K		MMU_PAGESIZE64K >> MEMSCRUB_BLOCK_SIZE_SHIFT
1877c478bd9Sstevel@tonic-gate #define	MEMSCRUB_BPP		MMU_PAGESIZE >> MEMSCRUB_BLOCK_SIZE_SHIFT
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * This message indicates that we have exceeded the limitations of
1917c478bd9Sstevel@tonic-gate  * the memscrubber. See the comments above regarding what would
1927c478bd9Sstevel@tonic-gate  * cause the sleep time to become zero. In DEBUG mode, this message
1937c478bd9Sstevel@tonic-gate  * is logged on the console and in the messages file. In non-DEBUG
1947c478bd9Sstevel@tonic-gate  * mode, it is only logged in the messages file.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate #ifdef DEBUG
1977c478bd9Sstevel@tonic-gate #define	MEMSCRUB_OVERRIDE_MSG	"Memory scrubber sleep time is zero " \
1987c478bd9Sstevel@tonic-gate 	"seconds, consuming entire CPU."
1997c478bd9Sstevel@tonic-gate #else
2007c478bd9Sstevel@tonic-gate #define	MEMSCRUB_OVERRIDE_MSG	"!Memory scrubber sleep time is zero " \
2017c478bd9Sstevel@tonic-gate 	"seconds, consuming entire CPU."
2027c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * we can patch these defaults in /etc/system if necessary
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate uint_t disable_memscrub = 0;
2087c478bd9Sstevel@tonic-gate uint_t pause_memscrub = 0;
2097c478bd9Sstevel@tonic-gate uint_t read_all_memscrub = 0;
2107c478bd9Sstevel@tonic-gate uint_t memscrub_verbose = 0;
2117c478bd9Sstevel@tonic-gate uint_t memscrub_all_idle = 0;
2127c478bd9Sstevel@tonic-gate uint_t memscrub_span_pages = MEMSCRUB_DFL_SPAN_PAGES;
2137c478bd9Sstevel@tonic-gate uint_t memscrub_period_sec = MEMSCRUB_DFL_PERIOD_SEC;
2147c478bd9Sstevel@tonic-gate uint_t memscrub_thread_pri = MEMSCRUB_DFL_THREAD_PRI;
2157c478bd9Sstevel@tonic-gate uint_t memscrub_delay_start_sec = 5 * 60;
2167c478bd9Sstevel@tonic-gate uint_t memscrub_override_ticks = 1;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Static Routines
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate static void memscrubber(void);
2227c478bd9Sstevel@tonic-gate static void memscrub_cleanup(void);
2237c478bd9Sstevel@tonic-gate static int memscrub_add_span_gen(pfn_t, pgcnt_t, struct memlist **, uint_t *);
2247c478bd9Sstevel@tonic-gate static int memscrub_verify_span(ms_paddr_t *addrp, pgcnt_t *pagesp);
2257c478bd9Sstevel@tonic-gate static void memscrub_scan(uint_t blks, ms_paddr_t src);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * Static Data
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static struct memlist *memscrub_memlist;
2327c478bd9Sstevel@tonic-gate static uint_t memscrub_phys_pages;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static kcondvar_t memscrub_cv;
2357c478bd9Sstevel@tonic-gate static kmutex_t memscrub_lock;
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * memscrub_lock protects memscrub_memlist, interval_ticks, cprinfo, ...
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate static void memscrub_init_mem_config(void);
2407c478bd9Sstevel@tonic-gate static void memscrub_uninit_mem_config(void);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
24361ef38f7Svb70745  * Linked list of memscrub aware spans having retired pages.
24461ef38f7Svb70745  * Currently enabled only on sun4u USIII-based platforms.
24561ef38f7Svb70745  */
24661ef38f7Svb70745 typedef struct memscrub_page_retire_span {
24761ef38f7Svb70745 	ms_paddr_t				address;
24861ef38f7Svb70745 	struct memscrub_page_retire_span	*next;
24961ef38f7Svb70745 } memscrub_page_retire_span_t;
25061ef38f7Svb70745 
25161ef38f7Svb70745 static memscrub_page_retire_span_t *memscrub_page_retire_span_list = NULL;
25261ef38f7Svb70745 
25361ef38f7Svb70745 static void memscrub_page_retire_span_add(ms_paddr_t);
25461ef38f7Svb70745 static void memscrub_page_retire_span_delete(ms_paddr_t);
25561ef38f7Svb70745 static int memscrub_page_retire_span_search(ms_paddr_t);
25661ef38f7Svb70745 static void memscrub_page_retire_span_list_update(void);
25761ef38f7Svb70745 
25861ef38f7Svb70745 /*
25961ef38f7Svb70745  * add_to_page_retire_list: Set by cpu_async_log_err() routine
26061ef38f7Svb70745  * by calling memscrub_induced_error() when CE/UE occurs on a retired
26161ef38f7Svb70745  * page due to memscrub reading.  Cleared by memscrub after updating
26261ef38f7Svb70745  * global page retire span list.  Piggybacking on protection of
26361ef38f7Svb70745  * memscrub_lock, which is held during set and clear.
26461ef38f7Svb70745  * Note: When cpu_async_log_err() calls memscrub_induced_error(), it is running
26561ef38f7Svb70745  * on softint context, which gets fired on a cpu memscrub thread currently
26661ef38f7Svb70745  * running.  Memscrub thread has affinity set during memscrub_read(), hence
26761ef38f7Svb70745  * migration to new cpu not expected.
26861ef38f7Svb70745  */
26961ef38f7Svb70745 static int add_to_page_retire_list = 0;
27061ef38f7Svb70745 
27161ef38f7Svb70745 /*
2727c478bd9Sstevel@tonic-gate  * Keep track of some interesting statistics
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static struct memscrub_kstats {
2757c478bd9Sstevel@tonic-gate 	kstat_named_t	done_early;	/* ahead of schedule */
2767c478bd9Sstevel@tonic-gate 	kstat_named_t	early_sec;	/* by cumulative num secs */
2777c478bd9Sstevel@tonic-gate 	kstat_named_t	done_late;	/* behind schedule */
2787c478bd9Sstevel@tonic-gate 	kstat_named_t	late_sec;	/* by cumulative num secs */
2797c478bd9Sstevel@tonic-gate 	kstat_named_t	interval_ticks;	/* num ticks between intervals */
2807c478bd9Sstevel@tonic-gate 	kstat_named_t	force_run;	/* forced to run, non-timeout */
2817c478bd9Sstevel@tonic-gate 	kstat_named_t	errors_found;	/* num errors found by memscrub */
2827c478bd9Sstevel@tonic-gate } memscrub_counts = {
2837c478bd9Sstevel@tonic-gate 	{ "done_early",		KSTAT_DATA_UINT32 },
2847c478bd9Sstevel@tonic-gate 	{ "early_sec", 		KSTAT_DATA_UINT32 },
2857c478bd9Sstevel@tonic-gate 	{ "done_late", 		KSTAT_DATA_UINT32 },
2867c478bd9Sstevel@tonic-gate 	{ "late_sec",		KSTAT_DATA_UINT32 },
2877c478bd9Sstevel@tonic-gate 	{ "interval_ticks",	KSTAT_DATA_UINT32 },
2887c478bd9Sstevel@tonic-gate 	{ "force_run",		KSTAT_DATA_UINT32 },
2897c478bd9Sstevel@tonic-gate 	{ "errors_found",	KSTAT_DATA_UINT32 },
2907c478bd9Sstevel@tonic-gate };
2917c478bd9Sstevel@tonic-gate static struct kstat *memscrub_ksp = (struct kstat *)NULL;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate static timeout_id_t memscrub_tid = 0;	/* keep track of timeout id */
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * create memscrub_memlist from phys_install list
2977c478bd9Sstevel@tonic-gate  * initialize locks, set memscrub_phys_pages.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate int
3007c478bd9Sstevel@tonic-gate memscrub_init(void)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	struct memlist *src;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * only startup the scrubber if we have a minimum
3067c478bd9Sstevel@tonic-gate 	 * number of pages
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 	if (physinstalled >= MEMSCRUB_MIN_PAGES) {
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		/*
3117c478bd9Sstevel@tonic-gate 		 * initialize locks
3127c478bd9Sstevel@tonic-gate 		 */
3137c478bd9Sstevel@tonic-gate 		mutex_init(&memscrub_lock, NULL, MUTEX_DRIVER, NULL);
3147c478bd9Sstevel@tonic-gate 		cv_init(&memscrub_cv, NULL, CV_DRIVER, NULL);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		/*
3177c478bd9Sstevel@tonic-gate 		 * copy phys_install to memscrub_memlist
3187c478bd9Sstevel@tonic-gate 		 */
319*56f33205SJonathan Adams 		for (src = phys_install; src; src = src->ml_next) {
3207c478bd9Sstevel@tonic-gate 			if (memscrub_add_span(
321*56f33205SJonathan Adams 			    (pfn_t)(src->ml_address >> PAGESHIFT),
322*56f33205SJonathan Adams 			    (pgcnt_t)(src->ml_size >> PAGESHIFT))) {
3237c478bd9Sstevel@tonic-gate 				memscrub_cleanup();
3247c478bd9Sstevel@tonic-gate 				return (-1);
3257c478bd9Sstevel@tonic-gate 			}
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		/*
3297c478bd9Sstevel@tonic-gate 		 * initialize kstats
3307c478bd9Sstevel@tonic-gate 		 */
3317c478bd9Sstevel@tonic-gate 		memscrub_ksp = kstat_create("unix", 0, "memscrub_kstat",
3327c478bd9Sstevel@tonic-gate 		    "misc", KSTAT_TYPE_NAMED,
3337c478bd9Sstevel@tonic-gate 		    sizeof (memscrub_counts) / sizeof (kstat_named_t),
3347c478bd9Sstevel@tonic-gate 		    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		if (memscrub_ksp) {
3377c478bd9Sstevel@tonic-gate 			memscrub_ksp->ks_data = (void *)&memscrub_counts;
3387c478bd9Sstevel@tonic-gate 			kstat_install(memscrub_ksp);
3397c478bd9Sstevel@tonic-gate 		} else {
3407c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "Memscrubber cannot create kstats\n");
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		/*
3447c478bd9Sstevel@tonic-gate 		 * create memscrubber thread
3457c478bd9Sstevel@tonic-gate 		 */
3467c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, 0, (void (*)())memscrubber,
3477c478bd9Sstevel@tonic-gate 		    NULL, 0, &p0, TS_RUN, memscrub_thread_pri);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * We don't want call backs changing the list
3517c478bd9Sstevel@tonic-gate 		 * if there is no thread running. We do not
3527c478bd9Sstevel@tonic-gate 		 * attempt to deal with stopping/starting scrubbing
3537c478bd9Sstevel@tonic-gate 		 * on memory size changes.
3547c478bd9Sstevel@tonic-gate 		 */
3557c478bd9Sstevel@tonic-gate 		memscrub_init_mem_config();
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	return (0);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static void
3627c478bd9Sstevel@tonic-gate memscrub_cleanup(void)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	memscrub_uninit_mem_config();
3657c478bd9Sstevel@tonic-gate 	while (memscrub_memlist) {
3667c478bd9Sstevel@tonic-gate 		(void) memscrub_delete_span(
367*56f33205SJonathan Adams 		    (pfn_t)(memscrub_memlist->ml_address >> PAGESHIFT),
368*56f33205SJonathan Adams 		    (pgcnt_t)(memscrub_memlist->ml_size >> PAGESHIFT));
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	if (memscrub_ksp)
3717c478bd9Sstevel@tonic-gate 		kstat_delete(memscrub_ksp);
3727c478bd9Sstevel@tonic-gate 	cv_destroy(&memscrub_cv);
3737c478bd9Sstevel@tonic-gate 	mutex_destroy(&memscrub_lock);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate #ifdef MEMSCRUB_DEBUG
3777c478bd9Sstevel@tonic-gate static void
3787c478bd9Sstevel@tonic-gate memscrub_printmemlist(char *title, struct memlist *listp)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	struct memlist *list;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s:\n", title);
3837c478bd9Sstevel@tonic-gate 
384*56f33205SJonathan Adams 	for (list = listp; list; list = list->ml_next) {
3857c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "addr = 0x%llx, size = 0x%llx\n",
386*56f33205SJonathan Adams 		    list->ml_address, list->ml_size);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate #endif /* MEMSCRUB_DEBUG */
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /* ARGSUSED */
3927c478bd9Sstevel@tonic-gate static void
3937c478bd9Sstevel@tonic-gate memscrub_wakeup(void *c)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * grab mutex to guarantee that our wakeup call
3977c478bd9Sstevel@tonic-gate 	 * arrives after we go to sleep -- so we can't sleep forever.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	mutex_enter(&memscrub_lock);
4007c478bd9Sstevel@tonic-gate 	cv_signal(&memscrub_cv);
4017c478bd9Sstevel@tonic-gate 	mutex_exit(&memscrub_lock);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * provide an interface external to the memscrubber
4067c478bd9Sstevel@tonic-gate  * which will force the memscrub thread to run vs.
4077c478bd9Sstevel@tonic-gate  * waiting for the timeout, if one is set
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate void
4107c478bd9Sstevel@tonic-gate memscrub_run(void)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	memscrub_counts.force_run.value.ui32++;
4137c478bd9Sstevel@tonic-gate 	if (memscrub_tid) {
4147c478bd9Sstevel@tonic-gate 		(void) untimeout(memscrub_tid);
4157c478bd9Sstevel@tonic-gate 		memscrub_wakeup((void *)NULL);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * this calculation doesn't account for the time
4217c478bd9Sstevel@tonic-gate  * that the actual scan consumes -- so we'd fall
4227c478bd9Sstevel@tonic-gate  * slightly behind schedule with this interval.
4237c478bd9Sstevel@tonic-gate  * It's very small.
4247c478bd9Sstevel@tonic-gate  */
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate static uint_t
4277c478bd9Sstevel@tonic-gate compute_interval_ticks(void)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * We use msp_safe mpp_safe below to insure somebody
4317c478bd9Sstevel@tonic-gate 	 * doesn't set memscrub_span_pages or memscrub_phys_pages
4327c478bd9Sstevel@tonic-gate 	 * to 0 on us.
4337c478bd9Sstevel@tonic-gate 	 */
4347c478bd9Sstevel@tonic-gate 	static uint_t msp_safe, mpp_safe;
4357c478bd9Sstevel@tonic-gate 	static uint_t interval_ticks, period_ticks;
4367c478bd9Sstevel@tonic-gate 	msp_safe = memscrub_span_pages;
4377c478bd9Sstevel@tonic-gate 	mpp_safe = memscrub_phys_pages;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	period_ticks = memscrub_period_sec * hz;
4407c478bd9Sstevel@tonic-gate 	interval_ticks = period_ticks;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&memscrub_lock));
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if ((msp_safe != 0) && (mpp_safe != 0)) {
4457c478bd9Sstevel@tonic-gate 		if (memscrub_phys_pages <= msp_safe) {
4467c478bd9Sstevel@tonic-gate 			interval_ticks = period_ticks;
4477c478bd9Sstevel@tonic-gate 		} else {
4487c478bd9Sstevel@tonic-gate 			interval_ticks = (period_ticks /
4497c478bd9Sstevel@tonic-gate 			    (mpp_safe / msp_safe));
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	return (interval_ticks);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate void
4567c478bd9Sstevel@tonic-gate memscrubber(void)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	ms_paddr_t address, addr;
4597c478bd9Sstevel@tonic-gate 	time_t deadline;
4607c478bd9Sstevel@tonic-gate 	pgcnt_t pages;
4617c478bd9Sstevel@tonic-gate 	uint_t reached_end = 1;
4627c478bd9Sstevel@tonic-gate 	uint_t paused_message = 0;
4637c478bd9Sstevel@tonic-gate 	uint_t interval_ticks = 0;
4647c478bd9Sstevel@tonic-gate 	uint_t sleep_warn_printed = 0;
4657c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * notify CPR of our existence
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &memscrub_lock, callb_generic_cpr, "memscrub");
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	mutex_enter(&memscrub_lock);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (memscrub_memlist == NULL) {
4757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "memscrub_memlist not initialized.");
4767c478bd9Sstevel@tonic-gate 		goto memscrub_exit;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
479*56f33205SJonathan Adams 	address = memscrub_memlist->ml_address;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	deadline = gethrestime_sec() + memscrub_delay_start_sec;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	for (;;) {
4847c478bd9Sstevel@tonic-gate 		if (disable_memscrub)
4857c478bd9Sstevel@tonic-gate 			break;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		/*
4887c478bd9Sstevel@tonic-gate 		 * compute interval_ticks
4897c478bd9Sstevel@tonic-gate 		 */
4907c478bd9Sstevel@tonic-gate 		interval_ticks = compute_interval_ticks();
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		/*
4937c478bd9Sstevel@tonic-gate 		 * If the calculated sleep time is zero, and pause_memscrub
4947c478bd9Sstevel@tonic-gate 		 * has been set, make sure we sleep so that another thread
4957c478bd9Sstevel@tonic-gate 		 * can acquire memscrub_lock.
4967c478bd9Sstevel@tonic-gate 		 */
4977c478bd9Sstevel@tonic-gate 		if (interval_ticks == 0 && pause_memscrub) {
4987c478bd9Sstevel@tonic-gate 			interval_ticks = hz;
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * And as a fail safe, under normal non-paused operation, do
5037c478bd9Sstevel@tonic-gate 		 * not allow the sleep time to be zero.
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		if (interval_ticks == 0) {
5067c478bd9Sstevel@tonic-gate 			interval_ticks = memscrub_override_ticks;
5077c478bd9Sstevel@tonic-gate 			if (!sleep_warn_printed) {
5087c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, MEMSCRUB_OVERRIDE_MSG);
5097c478bd9Sstevel@tonic-gate 				sleep_warn_printed = 1;
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		memscrub_counts.interval_ticks.value.ui32 = interval_ticks;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		/*
5167c478bd9Sstevel@tonic-gate 		 * Did we just reach the end of memory? If we are at the
5177c478bd9Sstevel@tonic-gate 		 * end of memory, delay end of memory processing until
5187c478bd9Sstevel@tonic-gate 		 * pause_memscrub is not set.
5197c478bd9Sstevel@tonic-gate 		 */
5207c478bd9Sstevel@tonic-gate 		if (reached_end && !pause_memscrub) {
5217c478bd9Sstevel@tonic-gate 			time_t now = gethrestime_sec();
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 			if (now >= deadline) {
5247c478bd9Sstevel@tonic-gate 				memscrub_counts.done_late.value.ui32++;
5257c478bd9Sstevel@tonic-gate 				memscrub_counts.late_sec.value.ui32 +=
5267c478bd9Sstevel@tonic-gate 				    (now - deadline);
5277c478bd9Sstevel@tonic-gate 				/*
5287c478bd9Sstevel@tonic-gate 				 * past deadline, start right away
5297c478bd9Sstevel@tonic-gate 				 */
5307c478bd9Sstevel@tonic-gate 				interval_ticks = 0;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 				deadline = now + memscrub_period_sec;
5337c478bd9Sstevel@tonic-gate 			} else {
5347c478bd9Sstevel@tonic-gate 				/*
5357c478bd9Sstevel@tonic-gate 				 * we finished ahead of schedule.
5367c478bd9Sstevel@tonic-gate 				 * wait till previous deadline before re-start.
5377c478bd9Sstevel@tonic-gate 				 */
5387c478bd9Sstevel@tonic-gate 				interval_ticks = (deadline - now) * hz;
5397c478bd9Sstevel@tonic-gate 				memscrub_counts.done_early.value.ui32++;
5407c478bd9Sstevel@tonic-gate 				memscrub_counts.early_sec.value.ui32 +=
5417c478bd9Sstevel@tonic-gate 				    (deadline - now);
5427c478bd9Sstevel@tonic-gate 				deadline += memscrub_period_sec;
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 			reached_end = 0;
5457c478bd9Sstevel@tonic-gate 			sleep_warn_printed = 0;
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		if (interval_ticks != 0) {
5497c478bd9Sstevel@tonic-gate 			/*
5507c478bd9Sstevel@tonic-gate 			 * it is safe from our standpoint for CPR to
5517c478bd9Sstevel@tonic-gate 			 * suspend the system
5527c478bd9Sstevel@tonic-gate 			 */
5537c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 			/*
5567c478bd9Sstevel@tonic-gate 			 * hit the snooze bar
5577c478bd9Sstevel@tonic-gate 			 */
5587c478bd9Sstevel@tonic-gate 			memscrub_tid = timeout(memscrub_wakeup, NULL,
5597c478bd9Sstevel@tonic-gate 			    interval_ticks);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 			/*
5627c478bd9Sstevel@tonic-gate 			 * go to sleep
5637c478bd9Sstevel@tonic-gate 			 */
5647c478bd9Sstevel@tonic-gate 			cv_wait(&memscrub_cv, &memscrub_lock);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			/*
5677c478bd9Sstevel@tonic-gate 			 * at this point, no timeout should be set
5687c478bd9Sstevel@tonic-gate 			 */
5697c478bd9Sstevel@tonic-gate 			memscrub_tid = 0;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 			/*
5727c478bd9Sstevel@tonic-gate 			 * we need to goto work and will be modifying
5737c478bd9Sstevel@tonic-gate 			 * our internal state and mapping/unmapping
5747c478bd9Sstevel@tonic-gate 			 * TTEs
5757c478bd9Sstevel@tonic-gate 			 */
5767c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &memscrub_lock);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		if (memscrub_phys_pages == 0) {
5817c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Memory scrubber has 0 pages to read");
5827c478bd9Sstevel@tonic-gate 			goto memscrub_exit;
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		if (!pause_memscrub) {
5867c478bd9Sstevel@tonic-gate 			if (paused_message) {
5877c478bd9Sstevel@tonic-gate 				paused_message = 0;
5887c478bd9Sstevel@tonic-gate 				if (memscrub_verbose)
5897c478bd9Sstevel@tonic-gate 					cmn_err(CE_NOTE, "Memory scrubber "
5907c478bd9Sstevel@tonic-gate 					    "resuming");
5917c478bd9Sstevel@tonic-gate 			}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 			if (read_all_memscrub) {
5947c478bd9Sstevel@tonic-gate 				if (memscrub_verbose)
5957c478bd9Sstevel@tonic-gate 					cmn_err(CE_NOTE, "Memory scrubber "
5967c478bd9Sstevel@tonic-gate 					    "reading all memory per request");
5977c478bd9Sstevel@tonic-gate 
598*56f33205SJonathan Adams 				addr = memscrub_memlist->ml_address;
5997c478bd9Sstevel@tonic-gate 				reached_end = 0;
6007c478bd9Sstevel@tonic-gate 				while (!reached_end) {
6017c478bd9Sstevel@tonic-gate 					if (disable_memscrub)
6027c478bd9Sstevel@tonic-gate 						break;
6037c478bd9Sstevel@tonic-gate 					pages = memscrub_phys_pages;
6047c478bd9Sstevel@tonic-gate 					reached_end = memscrub_verify_span(
6057c478bd9Sstevel@tonic-gate 					    &addr, &pages);
6067c478bd9Sstevel@tonic-gate 					memscrub_scan(pages *
6077c478bd9Sstevel@tonic-gate 					    MEMSCRUB_BLOCKS_PER_PAGE, addr);
6087c478bd9Sstevel@tonic-gate 					addr += ((uint64_t)pages * PAGESIZE);
6097c478bd9Sstevel@tonic-gate 				}
6107c478bd9Sstevel@tonic-gate 				read_all_memscrub = 0;
6117c478bd9Sstevel@tonic-gate 			}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 			/*
6147c478bd9Sstevel@tonic-gate 			 * read 1 span
6157c478bd9Sstevel@tonic-gate 			 */
6167c478bd9Sstevel@tonic-gate 			pages = memscrub_span_pages;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 			if (disable_memscrub)
6197c478bd9Sstevel@tonic-gate 				break;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			/*
6227c478bd9Sstevel@tonic-gate 			 * determine physical address range
6237c478bd9Sstevel@tonic-gate 			 */
6247c478bd9Sstevel@tonic-gate 			reached_end = memscrub_verify_span(&address,
6257c478bd9Sstevel@tonic-gate 			    &pages);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 			memscrub_scan(pages * MEMSCRUB_BLOCKS_PER_PAGE,
6287c478bd9Sstevel@tonic-gate 			    address);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 			address += ((uint64_t)pages * PAGESIZE);
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		if (pause_memscrub && !paused_message) {
6347c478bd9Sstevel@tonic-gate 			paused_message = 1;
6357c478bd9Sstevel@tonic-gate 			if (memscrub_verbose)
6367c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "Memory scrubber paused");
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate memscrub_exit:
6417c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "Memory scrubber exiting");
6427c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
6437c478bd9Sstevel@tonic-gate 	memscrub_cleanup();
6447c478bd9Sstevel@tonic-gate 	thread_exit();
6457c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * condition address and size
6507c478bd9Sstevel@tonic-gate  * such that they span legal physical addresses.
6517c478bd9Sstevel@tonic-gate  *
6527c478bd9Sstevel@tonic-gate  * when appropriate, address will be rounded up to start of next
6537c478bd9Sstevel@tonic-gate  * struct memlist, and pages will be rounded down to the end of the
6547c478bd9Sstevel@tonic-gate  * memlist size.
6557c478bd9Sstevel@tonic-gate  *
6567c478bd9Sstevel@tonic-gate  * returns 1 if reached end of list, else returns 0.
6577c478bd9Sstevel@tonic-gate  */
6587c478bd9Sstevel@tonic-gate static int
6597c478bd9Sstevel@tonic-gate memscrub_verify_span(ms_paddr_t *addrp, pgcnt_t *pagesp)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate 	struct memlist *mlp;
6627c478bd9Sstevel@tonic-gate 	ms_paddr_t address = *addrp;
6637c478bd9Sstevel@tonic-gate 	uint64_t bytes = (uint64_t)*pagesp * PAGESIZE;
6647c478bd9Sstevel@tonic-gate 	uint64_t bytes_remaining;
6657c478bd9Sstevel@tonic-gate 	int reached_end = 0;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&memscrub_lock));
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/*
6707c478bd9Sstevel@tonic-gate 	 * find memlist struct that contains addrp
6717c478bd9Sstevel@tonic-gate 	 * assumes memlist is sorted by ascending address.
6727c478bd9Sstevel@tonic-gate 	 */
673*56f33205SJonathan Adams 	for (mlp = memscrub_memlist; mlp != NULL; mlp = mlp->ml_next) {
6747c478bd9Sstevel@tonic-gate 		/*
6757c478bd9Sstevel@tonic-gate 		 * if before this chunk, round up to beginning
6767c478bd9Sstevel@tonic-gate 		 */
677*56f33205SJonathan Adams 		if (address < mlp->ml_address) {
678*56f33205SJonathan Adams 			address = mlp->ml_address;
6797c478bd9Sstevel@tonic-gate 			break;
6807c478bd9Sstevel@tonic-gate 		}
6817c478bd9Sstevel@tonic-gate 		/*
6827c478bd9Sstevel@tonic-gate 		 * if before end of chunk, then we found it
6837c478bd9Sstevel@tonic-gate 		 */
684*56f33205SJonathan Adams 		if (address < (mlp->ml_address + mlp->ml_size))
6857c478bd9Sstevel@tonic-gate 			break;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		/* else go to next struct memlist */
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * if we hit end of list, start at beginning
6917c478bd9Sstevel@tonic-gate 	 */
6927c478bd9Sstevel@tonic-gate 	if (mlp == NULL) {
6937c478bd9Sstevel@tonic-gate 		mlp = memscrub_memlist;
694*56f33205SJonathan Adams 		address = mlp->ml_address;
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/*
6987c478bd9Sstevel@tonic-gate 	 * now we have legal address, and its mlp, condition bytes
6997c478bd9Sstevel@tonic-gate 	 */
700*56f33205SJonathan Adams 	bytes_remaining = (mlp->ml_address + mlp->ml_size) - address;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (bytes > bytes_remaining)
7037c478bd9Sstevel@tonic-gate 		bytes = bytes_remaining;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * will this span take us to end of list?
7077c478bd9Sstevel@tonic-gate 	 */
708*56f33205SJonathan Adams 	if ((mlp->ml_next == NULL) &&
709*56f33205SJonathan Adams 	    ((mlp->ml_address + mlp->ml_size) == (address + bytes)))
7107c478bd9Sstevel@tonic-gate 		reached_end = 1;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	/* return values */
7137c478bd9Sstevel@tonic-gate 	*addrp = address;
7147c478bd9Sstevel@tonic-gate 	*pagesp = bytes / PAGESIZE;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	return (reached_end);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * add a span to the memscrub list
7217c478bd9Sstevel@tonic-gate  * add to memscrub_phys_pages
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate int
7247c478bd9Sstevel@tonic-gate memscrub_add_span(pfn_t pfn, pgcnt_t pages)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate #ifdef MEMSCRUB_DEBUG
7277c478bd9Sstevel@tonic-gate 	ms_paddr_t address = (ms_paddr_t)pfn << PAGESHIFT;
7287c478bd9Sstevel@tonic-gate 	uint64_t bytes = (uint64_t)pages << PAGESHIFT;
7297c478bd9Sstevel@tonic-gate #endif /* MEMSCRUB_DEBUG */
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	int retval;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	mutex_enter(&memscrub_lock);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate #ifdef MEMSCRUB_DEBUG
7367c478bd9Sstevel@tonic-gate 	memscrub_printmemlist("memscrub_memlist before", memscrub_memlist);
7377c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "memscrub_phys_pages: 0x%x\n", memscrub_phys_pages);
7387c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "memscrub_add_span: address: 0x%llx"
7397c478bd9Sstevel@tonic-gate 	    " size: 0x%llx\n", address, bytes);
7407c478bd9Sstevel@tonic-gate #endif /* MEMSCRUB_DEBUG */
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	retval = memscrub_add_span_gen(pfn, pages, &memscrub_memlist,
7437c478bd9Sstevel@tonic-gate 	    &memscrub_phys_pages);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate #ifdef MEMSCRUB_DEBUG
7467c478bd9Sstevel@tonic-gate 	memscrub_printmemlist("memscrub_memlist after", memscrub_memlist);
7477c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "memscrub_phys_pages: 0x%x\n", memscrub_phys_pages);
7487c478bd9Sstevel@tonic-gate #endif /* MEMSCRUB_DEBUG */
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	mutex_exit(&memscrub_lock);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	return (retval);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate static int
7567c478bd9Sstevel@tonic-gate memscrub_add_span_gen(
7577c478bd9Sstevel@tonic-gate 	pfn_t pfn,
7587c478bd9Sstevel@tonic-gate 	pgcnt_t pages,
7597c478bd9Sstevel@tonic-gate 	struct memlist **list,
7607c478bd9Sstevel@tonic-gate 	uint_t *npgs)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	ms_paddr_t address = (ms_paddr_t)pfn << PAGESHIFT;
7637c478bd9Sstevel@tonic-gate 	uint64_t bytes = (uint64_t)pages << PAGESHIFT;
7647c478bd9Sstevel@tonic-gate 	struct memlist *dst;
7657c478bd9Sstevel@tonic-gate 	struct memlist *prev, *next;
7667c478bd9Sstevel@tonic-gate 	int retval = 0;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * allocate a new struct memlist
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	dst = (struct memlist *)
7737c478bd9Sstevel@tonic-gate 	    kmem_alloc(sizeof (struct memlist), KM_NOSLEEP);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (dst == NULL) {
7767c478bd9Sstevel@tonic-gate 		retval = -1;
7777c478bd9Sstevel@tonic-gate 		goto add_done;
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
780*56f33205SJonathan Adams 	dst->ml_address = address;
781*56f33205SJonathan Adams 	dst->ml_size = bytes;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * first insert
7857c478bd9Sstevel@tonic-gate 	 */
7867c478bd9Sstevel@tonic-gate 	if (*list == NULL) {
787*56f33205SJonathan Adams 		dst->ml_prev = NULL;
788*56f33205SJonathan Adams 		dst->ml_next = NULL;
7897c478bd9Sstevel@tonic-gate 		*list = dst;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		goto add_done;
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	/*
7957c478bd9Sstevel@tonic-gate 	 * insert into sorted list
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	for (prev = NULL, next = *list;
7987c478bd9Sstevel@tonic-gate 	    next != NULL;
799*56f33205SJonathan Adams 	    prev = next, next = next->ml_next) {
800*56f33205SJonathan Adams 		if (address > (next->ml_address + next->ml_size))
8017c478bd9Sstevel@tonic-gate 			continue;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		/*
8047c478bd9Sstevel@tonic-gate 		 * else insert here
8057c478bd9Sstevel@tonic-gate 		 */
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		/*
8087c478bd9Sstevel@tonic-gate 		 * prepend to next
8097c478bd9Sstevel@tonic-gate 		 */
810*56f33205SJonathan Adams 		if ((address + bytes) == next->ml_address) {
8117c478bd9Sstevel@tonic-gate 			kmem_free(dst, sizeof (struct memlist));
8127c478bd9Sstevel@tonic-gate 
813*56f33205SJonathan Adams 			next->ml_address = address;
814*56f33205SJonathan Adams 			next->ml_size += bytes;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 			goto add_done;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		/*
8207c478bd9Sstevel@tonic-gate 		 * append to next
8217c478bd9Sstevel@tonic-gate 		 */
822*56f33205SJonathan Adams 		if (address == (next->ml_address + next->ml_size)) {
8237c478bd9Sstevel@tonic-gate 			kmem_free(dst, sizeof (struct memlist));
8247c478bd9Sstevel@tonic-gate 
825*56f33205SJonathan Adams 			if (next->ml_next) {
8267c478bd9Sstevel@tonic-gate 				/*
827*56f33205SJonathan Adams 				 * don't overlap with next->ml_next
8287c478bd9Sstevel@tonic-gate 				 */
829*56f33205SJonathan Adams 				if ((address + bytes) >
830*56f33205SJonathan Adams 				    next->ml_next->ml_address) {
8317c478bd9Sstevel@tonic-gate 					retval = -1;
8327c478bd9Sstevel@tonic-gate 					goto add_done;
8337c478bd9Sstevel@tonic-gate 				}
8347c478bd9Sstevel@tonic-gate 				/*
835*56f33205SJonathan Adams 				 * concatenate next and next->ml_next
8367c478bd9Sstevel@tonic-gate 				 */
837*56f33205SJonathan Adams 				if ((address + bytes) ==
838*56f33205SJonathan Adams 				    next->ml_next->ml_address) {
839*56f33205SJonathan Adams 					struct memlist *mlp = next->ml_next;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 					if (next == *list)
842*56f33205SJonathan Adams 						*list = next->ml_next;
8437c478bd9Sstevel@tonic-gate 
844*56f33205SJonathan Adams 					mlp->ml_address = next->ml_address;
845*56f33205SJonathan Adams 					mlp->ml_size += next->ml_size;
846*56f33205SJonathan Adams 					mlp->ml_size += bytes;
8477c478bd9Sstevel@tonic-gate 
848*56f33205SJonathan Adams 					if (next->ml_prev)
849*56f33205SJonathan Adams 						next->ml_prev->ml_next = mlp;
850*56f33205SJonathan Adams 					mlp->ml_prev = next->ml_prev;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 					kmem_free(next,
8537c478bd9Sstevel@tonic-gate 					    sizeof (struct memlist));
8547c478bd9Sstevel@tonic-gate 					goto add_done;
8557c478bd9Sstevel@tonic-gate 				}
8567c478bd9Sstevel@tonic-gate 			}
8577c478bd9Sstevel@tonic-gate 
858*56f33205SJonathan Adams 			next->ml_size += bytes;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 			goto add_done;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		/* don't overlap with next */
864*56f33205SJonathan Adams 		if ((address + bytes) > next->ml_address) {
8657c478bd9Sstevel@tonic-gate 			retval = -1;
8667c478bd9Sstevel@tonic-gate 			kmem_free(dst, sizeof (struct memlist));
8677c478bd9Sstevel@tonic-gate 			goto add_done;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		/*
8717c478bd9Sstevel@tonic-gate 		 * insert before next
8727c478bd9Sstevel@tonic-gate 		 */
873*56f33205SJonathan Adams 		dst->ml_prev = prev;
874*56f33205SJonathan Adams 		dst->ml_next = next;
875*56f33205SJonathan Adams 		next->ml_prev = dst;
8767c478bd9Sstevel@tonic-gate 		if (prev == NULL) {
8777c478bd9Sstevel@tonic-gate 			*list = dst;
8787c478bd9Sstevel@tonic-gate 		} else {
879*56f33205SJonathan Adams 			prev->ml_next = dst;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 		goto add_done;
8827c478bd9Sstevel@tonic-gate 	}	/* end for */
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	 * end of list, prev is valid and next is NULL
8867c478bd9Sstevel@tonic-gate 	 */
887*56f33205SJonathan Adams 	prev->ml_next = dst;
888*56f33205SJonathan Adams 	dst->ml_prev = prev;
889*56f33205SJonathan Adams 	dst->ml_next = NULL;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate add_done:
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (retval != -1)
8947c478bd9Sstevel@tonic-gate 		*npgs += pages;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	return (retval);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate /*
9007c478bd9Sstevel@tonic-gate  * delete a span from the memscrub list
9017c478bd9Sstevel@tonic-gate  * subtract from memscrub_phys_pages
9027c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate int
9047c478bd9Sstevel@tonic-gate memscrub_delete_span(pfn_t pfn, pgcnt_t pages)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	ms_paddr_t address = (ms_paddr_t)pfn << PAGESHIFT;
9077c478bd9Sstevel@tonic-gate 	uint64_t bytes = (uint64_t)pages << PAGESHIFT;
9087c478bd9Sstevel@tonic-gate 	struct memlist *dst, *next;
9097c478bd9Sstevel@tonic-gate 	int retval = 0;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	mutex_enter(&memscrub_lock);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate #ifdef MEMSCRUB_DEBUG
9147c478bd9Sstevel@tonic-gate 	memscrub_printmemlist("memscrub_memlist Before", memscrub_memlist);
9157c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "memscrub_phys_pages: 0x%x\n", memscrub_phys_pages);
9167c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "memscrub_delete_span: 0x%llx 0x%llx\n",
9177c478bd9Sstevel@tonic-gate 	    address, bytes);
9187c478bd9Sstevel@tonic-gate #endif /* MEMSCRUB_DEBUG */
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * find struct memlist containing page
9227c478bd9Sstevel@tonic-gate 	 */
923*56f33205SJonathan Adams 	for (next = memscrub_memlist; next != NULL; next = next->ml_next) {
924*56f33205SJonathan Adams 		if ((address >= next->ml_address) &&
925*56f33205SJonathan Adams 		    (address < next->ml_address + next->ml_size))
9267c478bd9Sstevel@tonic-gate 			break;
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	/*
9307c478bd9Sstevel@tonic-gate 	 * if start address not in list
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	if (next == NULL) {
9337c478bd9Sstevel@tonic-gate 		retval = -1;
9347c478bd9Sstevel@tonic-gate 		goto delete_done;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * error if size goes off end of this struct memlist
9397c478bd9Sstevel@tonic-gate 	 */
940*56f33205SJonathan Adams 	if (address + bytes > next->ml_address + next->ml_size) {
9417c478bd9Sstevel@tonic-gate 		retval = -1;
9427c478bd9Sstevel@tonic-gate 		goto delete_done;
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	/*
9467c478bd9Sstevel@tonic-gate 	 * pages at beginning of struct memlist
9477c478bd9Sstevel@tonic-gate 	 */
948*56f33205SJonathan Adams 	if (address == next->ml_address) {
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * if start & size match, delete from list
9517c478bd9Sstevel@tonic-gate 		 */
952*56f33205SJonathan Adams 		if (bytes == next->ml_size) {
9537c478bd9Sstevel@tonic-gate 			if (next == memscrub_memlist)
954*56f33205SJonathan Adams 				memscrub_memlist = next->ml_next;
955*56f33205SJonathan Adams 			if (next->ml_prev != NULL)
956*56f33205SJonathan Adams 				next->ml_prev->ml_next = next->ml_next;
957*56f33205SJonathan Adams 			if (next->ml_next != NULL)
958*56f33205SJonathan Adams 				next->ml_next->ml_prev = next->ml_prev;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 			kmem_free(next, sizeof (struct memlist));
9617c478bd9Sstevel@tonic-gate 		} else {
9627c478bd9Sstevel@tonic-gate 		/*
9637c478bd9Sstevel@tonic-gate 		 * increment start address by bytes
9647c478bd9Sstevel@tonic-gate 		 */
965*56f33205SJonathan Adams 			next->ml_address += bytes;
966*56f33205SJonathan Adams 			next->ml_size -= bytes;
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 		goto delete_done;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	/*
9727c478bd9Sstevel@tonic-gate 	 * pages at end of struct memlist
9737c478bd9Sstevel@tonic-gate 	 */
974*56f33205SJonathan Adams 	if (address + bytes == next->ml_address + next->ml_size) {
9757c478bd9Sstevel@tonic-gate 		/*
9767c478bd9Sstevel@tonic-gate 		 * decrement size by bytes
9777c478bd9Sstevel@tonic-gate 		 */
978*56f33205SJonathan Adams 		next->ml_size -= bytes;
9797c478bd9Sstevel@tonic-gate 		goto delete_done;
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * delete a span in the middle of the struct memlist
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	{
9867c478bd9Sstevel@tonic-gate 		/*
9877c478bd9Sstevel@tonic-gate 		 * create a new struct memlist
9887c478bd9Sstevel@tonic-gate 		 */
9897c478bd9Sstevel@tonic-gate 		dst = (struct memlist *)
9907c478bd9Sstevel@tonic-gate 		    kmem_alloc(sizeof (struct memlist), KM_NOSLEEP);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		if (dst == NULL) {
9937c478bd9Sstevel@tonic-gate 			retval = -1;
9947c478bd9Sstevel@tonic-gate 			goto delete_done;
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 		/*
9987c478bd9Sstevel@tonic-gate 		 * existing struct memlist gets address
9997c478bd9Sstevel@tonic-gate 		 * and size up to pfn
10007c478bd9Sstevel@tonic-gate 		 */
1001*56f33205SJonathan Adams 		dst->ml_address = address + bytes;
1002*56f33205SJonathan Adams 		dst->ml_size =
1003*56f33205SJonathan Adams 		    (next->ml_address + next->ml_size) - dst->ml_address;
1004*56f33205SJonathan Adams 		next->ml_size = address - next->ml_address;
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 		/*
10077c478bd9Sstevel@tonic-gate 		 * new struct memlist gets address starting
10087c478bd9Sstevel@tonic-gate 		 * after pfn, until end
10097c478bd9Sstevel@tonic-gate 		 */
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 		/*
10127c478bd9Sstevel@tonic-gate 		 * link in new memlist after old
10137c478bd9Sstevel@tonic-gate 		 */
1014*56f33205SJonathan Adams 		dst->ml_next = next->ml_next;
1015*56f33205SJonathan Adams 		dst->ml_prev = next;
10167c478bd9Sstevel@tonic-gate 
1017*56f33205SJonathan Adams 		if (next->ml_next != NULL)
1018*56f33205SJonathan Adams 			next->ml_next->ml_prev = dst;
1019*56f33205SJonathan Adams 		next->ml_next = dst;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate delete_done:
10237c478bd9Sstevel@tonic-gate 	if (retval != -1) {
10247c478bd9Sstevel@tonic-gate 		memscrub_phys_pages -= pages;
10257c478bd9Sstevel@tonic-gate 		if (memscrub_phys_pages == 0)
10267c478bd9Sstevel@tonic-gate 			disable_memscrub = 1;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate #ifdef MEMSCRUB_DEBUG
10307c478bd9Sstevel@tonic-gate 	memscrub_printmemlist("memscrub_memlist After", memscrub_memlist);
10317c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "memscrub_phys_pages: 0x%x\n", memscrub_phys_pages);
10327c478bd9Sstevel@tonic-gate #endif /* MEMSCRUB_DEBUG */
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	mutex_exit(&memscrub_lock);
10357c478bd9Sstevel@tonic-gate 	return (retval);
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate static void
10397c478bd9Sstevel@tonic-gate memscrub_scan(uint_t blks, ms_paddr_t src)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	uint_t 		psz, bpp, pgsread;
10427c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
10437c478bd9Sstevel@tonic-gate 	ms_paddr_t	pa;
10447c478bd9Sstevel@tonic-gate 	caddr_t		va;
10457c478bd9Sstevel@tonic-gate 	on_trap_data_t	otd;
104661ef38f7Svb70745 	int		scan_mmu_pagesize = 0;
104761ef38f7Svb70745 	int		retired_pages = 0;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	extern void memscrub_read(caddr_t src, uint_t blks);
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&memscrub_lock));
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	pgsread = 0;
10547c478bd9Sstevel@tonic-gate 	pa = src;
10557c478bd9Sstevel@tonic-gate 
105661ef38f7Svb70745 	if (memscrub_page_retire_span_list != NULL) {
105761ef38f7Svb70745 		if (memscrub_page_retire_span_search(src)) {
105861ef38f7Svb70745 			/* retired pages in current span */
105961ef38f7Svb70745 			scan_mmu_pagesize = 1;
106061ef38f7Svb70745 		}
106161ef38f7Svb70745 	}
106261ef38f7Svb70745 
106361ef38f7Svb70745 #ifdef MEMSCRUB_DEBUG
106461ef38f7Svb70745 	cmn_err(CE_NOTE, "scan_mmu_pagesize = %d\n" scan_mmu_pagesize);
106561ef38f7Svb70745 #endif /* MEMSCRUB_DEBUG */
106661ef38f7Svb70745 
10677c478bd9Sstevel@tonic-gate 	while (blks != 0) {
10687c478bd9Sstevel@tonic-gate 		/* Ensure the PA is properly aligned */
10697c478bd9Sstevel@tonic-gate 		if (((pa & MMU_PAGEMASK4M) == pa) &&
10707c478bd9Sstevel@tonic-gate 		    (blks >= MEMSCRUB_BPP4M)) {
10717c478bd9Sstevel@tonic-gate 			psz = MMU_PAGESIZE4M;
10727c478bd9Sstevel@tonic-gate 			bpp = MEMSCRUB_BPP4M;
10737c478bd9Sstevel@tonic-gate 		} else if (((pa & MMU_PAGEMASK512K) == pa) &&
10747c478bd9Sstevel@tonic-gate 		    (blks >= MEMSCRUB_BPP512K)) {
10757c478bd9Sstevel@tonic-gate 			psz = MMU_PAGESIZE512K;
10767c478bd9Sstevel@tonic-gate 			bpp = MEMSCRUB_BPP512K;
10777c478bd9Sstevel@tonic-gate 		} else if (((pa & MMU_PAGEMASK64K) == pa) &&
10787c478bd9Sstevel@tonic-gate 		    (blks >= MEMSCRUB_BPP64K)) {
10797c478bd9Sstevel@tonic-gate 			psz = MMU_PAGESIZE64K;
10807c478bd9Sstevel@tonic-gate 			bpp = MEMSCRUB_BPP64K;
10817c478bd9Sstevel@tonic-gate 		} else if ((pa & MMU_PAGEMASK) == pa) {
10827c478bd9Sstevel@tonic-gate 			psz = MMU_PAGESIZE;
10837c478bd9Sstevel@tonic-gate 			bpp = MEMSCRUB_BPP;
10847c478bd9Sstevel@tonic-gate 		} else {
10857c478bd9Sstevel@tonic-gate 			if (memscrub_verbose) {
10867c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "Memory scrubber ignoring "
10877c478bd9Sstevel@tonic-gate 				    "non-page aligned block starting at 0x%"
10887c478bd9Sstevel@tonic-gate 				    PRIx64, src);
10897c478bd9Sstevel@tonic-gate 			}
10907c478bd9Sstevel@tonic-gate 			return;
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 		if (blks < bpp) bpp = blks;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate #ifdef MEMSCRUB_DEBUG
10957c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Going to run psz=%x, "
10967c478bd9Sstevel@tonic-gate 		    "bpp=%x pa=%llx\n", psz, bpp, pa);
10977c478bd9Sstevel@tonic-gate #endif /* MEMSCRUB_DEBUG */
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		/*
11007c478bd9Sstevel@tonic-gate 		 * MEMSCRUBBASE is a 4MB aligned page in the
11017c478bd9Sstevel@tonic-gate 		 * kernel so that we can quickly map the PA
11027c478bd9Sstevel@tonic-gate 		 * to a VA for the block loads performed in
11037c478bd9Sstevel@tonic-gate 		 * memscrub_read.
11047c478bd9Sstevel@tonic-gate 		 */
11057c478bd9Sstevel@tonic-gate 		pfn = mmu_btop(pa);
11067c478bd9Sstevel@tonic-gate 		va = (caddr_t)MEMSCRUBBASE;
11077c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, va, psz, pfn, PROT_READ,
11087c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 		/*
11117c478bd9Sstevel@tonic-gate 		 * Can't allow the memscrubber to migrate across CPUs as
11127c478bd9Sstevel@tonic-gate 		 * we need to know whether CEEN is enabled for the current
11137c478bd9Sstevel@tonic-gate 		 * CPU to enable us to scrub the memory. Don't use
11147c478bd9Sstevel@tonic-gate 		 * kpreempt_disable as the time we take to scan a span (even
11157c478bd9Sstevel@tonic-gate 		 * without cpu_check_ce having to manually cpu_check_block)
11167c478bd9Sstevel@tonic-gate 		 * is too long to hold a higher priority thread (eg, RT)
11177c478bd9Sstevel@tonic-gate 		 * off cpu.
11187c478bd9Sstevel@tonic-gate 		 */
11197c478bd9Sstevel@tonic-gate 		thread_affinity_set(curthread, CPU_CURRENT);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		/*
11227c478bd9Sstevel@tonic-gate 		 * Protect read scrub from async faults.  For now, we simply
11237c478bd9Sstevel@tonic-gate 		 * maintain a count of such faults caught.
11247c478bd9Sstevel@tonic-gate 		 */
11257c478bd9Sstevel@tonic-gate 
112661ef38f7Svb70745 		if (!scan_mmu_pagesize && !on_trap(&otd, OT_DATA_EC)) {
11277c478bd9Sstevel@tonic-gate 			memscrub_read(va, bpp);
11287c478bd9Sstevel@tonic-gate 			/*
11297c478bd9Sstevel@tonic-gate 			 * Check if CEs require logging
11307c478bd9Sstevel@tonic-gate 			 */
11317c478bd9Sstevel@tonic-gate 			cpu_check_ce(SCRUBBER_CEEN_CHECK,
11327c478bd9Sstevel@tonic-gate 			    (uint64_t)pa, va, psz);
1133a08365b4Srjnoe 			no_trap();
11347c478bd9Sstevel@tonic-gate 			thread_affinity_clear(curthread);
11357c478bd9Sstevel@tonic-gate 		} else {
11367c478bd9Sstevel@tonic-gate 			no_trap();
11377c478bd9Sstevel@tonic-gate 			thread_affinity_clear(curthread);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 			/*
11407c478bd9Sstevel@tonic-gate 			 * Got an async error..
11417c478bd9Sstevel@tonic-gate 			 * Try rescanning it at MMU_PAGESIZE
11427c478bd9Sstevel@tonic-gate 			 * granularity if we were trying to
11437c478bd9Sstevel@tonic-gate 			 * read at a larger page size.
11447c478bd9Sstevel@tonic-gate 			 * This is to ensure we continue to
11457c478bd9Sstevel@tonic-gate 			 * scan the rest of the span.
114661ef38f7Svb70745 			 * OR scanning MMU_PAGESIZE granularity to avoid
114761ef38f7Svb70745 			 * reading retired pages memory when scan_mmu_pagesize
114861ef38f7Svb70745 			 * is set.
11497c478bd9Sstevel@tonic-gate 			 */
115061ef38f7Svb70745 			if (psz > MMU_PAGESIZE || scan_mmu_pagesize) {
11517c478bd9Sstevel@tonic-gate 			    caddr_t vaddr = va;
11527c478bd9Sstevel@tonic-gate 			    ms_paddr_t paddr = pa;
11537c478bd9Sstevel@tonic-gate 			    int tmp = 0;
11547c478bd9Sstevel@tonic-gate 			    for (; tmp < bpp; tmp += MEMSCRUB_BPP) {
115561ef38f7Svb70745 				/* Don't scrub retired pages */
115661ef38f7Svb70745 				if (page_retire_check(paddr, NULL) == 0) {
115761ef38f7Svb70745 					vaddr += MMU_PAGESIZE;
115861ef38f7Svb70745 					paddr += MMU_PAGESIZE;
115961ef38f7Svb70745 					retired_pages++;
116061ef38f7Svb70745 					continue;
116161ef38f7Svb70745 				}
11627c478bd9Sstevel@tonic-gate 				thread_affinity_set(curthread, CPU_CURRENT);
1163a08365b4Srjnoe 				if (!on_trap(&otd, OT_DATA_EC)) {
11647c478bd9Sstevel@tonic-gate 				    memscrub_read(vaddr, MEMSCRUB_BPP);
11657c478bd9Sstevel@tonic-gate 				    cpu_check_ce(SCRUBBER_CEEN_CHECK,
11667c478bd9Sstevel@tonic-gate 					(uint64_t)paddr, vaddr, MMU_PAGESIZE);
1167a08365b4Srjnoe 				    no_trap();
1168a08365b4Srjnoe 				} else {
1169a08365b4Srjnoe 				    no_trap();
1170a08365b4Srjnoe 				    memscrub_counts.errors_found.value.ui32++;
1171a08365b4Srjnoe 				}
11727c478bd9Sstevel@tonic-gate 				thread_affinity_clear(curthread);
11737c478bd9Sstevel@tonic-gate 				vaddr += MMU_PAGESIZE;
11747c478bd9Sstevel@tonic-gate 				paddr += MMU_PAGESIZE;
11757c478bd9Sstevel@tonic-gate 			    }
11767c478bd9Sstevel@tonic-gate 			}
11777c478bd9Sstevel@tonic-gate 		}
11787c478bd9Sstevel@tonic-gate 		hat_unload(kas.a_hat, va, psz, HAT_UNLOAD_UNLOCK);
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 		blks -= bpp;
11817c478bd9Sstevel@tonic-gate 		pa += psz;
11827c478bd9Sstevel@tonic-gate 		pgsread++;
11837c478bd9Sstevel@tonic-gate 	}
118461ef38f7Svb70745 
118561ef38f7Svb70745 	/*
118661ef38f7Svb70745 	 * If just finished scrubbing MMU_PAGESIZE at a time, but no retired
118761ef38f7Svb70745 	 * pages found so delete span from global list.
118861ef38f7Svb70745 	 */
118961ef38f7Svb70745 	if (scan_mmu_pagesize && retired_pages == 0)
119061ef38f7Svb70745 		memscrub_page_retire_span_delete(src);
119161ef38f7Svb70745 
119261ef38f7Svb70745 	/*
119361ef38f7Svb70745 	 * Encountered CE/UE on a retired page during memscrub read of current
119461ef38f7Svb70745 	 * span.  Adding span to global list to enable avoid reading further.
119561ef38f7Svb70745 	 */
119661ef38f7Svb70745 	if (add_to_page_retire_list) {
119761ef38f7Svb70745 		if (!memscrub_page_retire_span_search(src))
119861ef38f7Svb70745 			memscrub_page_retire_span_add(src);
119961ef38f7Svb70745 		add_to_page_retire_list = 0;
120061ef38f7Svb70745 	}
120161ef38f7Svb70745 
12027c478bd9Sstevel@tonic-gate 	if (memscrub_verbose) {
12037c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Memory scrubber read 0x%x pages starting "
12047c478bd9Sstevel@tonic-gate 		    "at 0x%" PRIx64, pgsread, src);
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate /*
120961ef38f7Svb70745  * Called by cpu_async_log_err() when memscrub read causes
121061ef38f7Svb70745  * CE/UE on a retired page.
121161ef38f7Svb70745  */
121261ef38f7Svb70745 void
121361ef38f7Svb70745 memscrub_induced_error(void)
121461ef38f7Svb70745 {
121561ef38f7Svb70745 	add_to_page_retire_list = 1;
121661ef38f7Svb70745 }
121761ef38f7Svb70745 
121861ef38f7Svb70745 
121961ef38f7Svb70745 /*
122061ef38f7Svb70745  * Called by memscrub_scan().
122161ef38f7Svb70745  * pa: physical address of span with CE/UE, add to global list.
122261ef38f7Svb70745  */
122361ef38f7Svb70745 static void
122461ef38f7Svb70745 memscrub_page_retire_span_add(ms_paddr_t pa)
122561ef38f7Svb70745 {
122661ef38f7Svb70745 	memscrub_page_retire_span_t *new_span;
122761ef38f7Svb70745 
122861ef38f7Svb70745 	new_span = (memscrub_page_retire_span_t *)
122961ef38f7Svb70745 	    kmem_zalloc(sizeof (memscrub_page_retire_span_t), KM_NOSLEEP);
123061ef38f7Svb70745 
123161ef38f7Svb70745 	if (new_span == NULL) {
123261ef38f7Svb70745 #ifdef MEMSCRUB_DEBUG
123361ef38f7Svb70745 		cmn_err(CE_NOTE, "failed to allocate new span - span with"
123461ef38f7Svb70745 		    " retired page/s not tracked.\n");
123561ef38f7Svb70745 #endif /* MEMSCRUB_DEBUG */
123661ef38f7Svb70745 		return;
123761ef38f7Svb70745 	}
123861ef38f7Svb70745 
123961ef38f7Svb70745 	new_span->address = pa;
124061ef38f7Svb70745 	new_span->next = memscrub_page_retire_span_list;
124161ef38f7Svb70745 	memscrub_page_retire_span_list = new_span;
124261ef38f7Svb70745 }
124361ef38f7Svb70745 
124461ef38f7Svb70745 /*
124561ef38f7Svb70745  * Called by memscrub_scan().
124661ef38f7Svb70745  * pa: physical address of span to be removed from global list.
124761ef38f7Svb70745  */
124861ef38f7Svb70745 static void
124961ef38f7Svb70745 memscrub_page_retire_span_delete(ms_paddr_t pa)
125061ef38f7Svb70745 {
125161ef38f7Svb70745 	memscrub_page_retire_span_t *prev_span, *next_span;
125261ef38f7Svb70745 
125361ef38f7Svb70745 	prev_span = memscrub_page_retire_span_list;
125461ef38f7Svb70745 	next_span = memscrub_page_retire_span_list->next;
125561ef38f7Svb70745 
125661ef38f7Svb70745 	if (pa == prev_span->address) {
125761ef38f7Svb70745 		memscrub_page_retire_span_list = next_span;
125861ef38f7Svb70745 		kmem_free(prev_span, sizeof (memscrub_page_retire_span_t));
125961ef38f7Svb70745 		return;
126061ef38f7Svb70745 	}
126161ef38f7Svb70745 
126261ef38f7Svb70745 	while (next_span) {
126361ef38f7Svb70745 		if (pa == next_span->address) {
126461ef38f7Svb70745 			prev_span->next = next_span->next;
126561ef38f7Svb70745 			kmem_free(next_span,
126661ef38f7Svb70745 			    sizeof (memscrub_page_retire_span_t));
126761ef38f7Svb70745 			return;
126861ef38f7Svb70745 		}
126961ef38f7Svb70745 		prev_span = next_span;
127061ef38f7Svb70745 		next_span = next_span->next;
127161ef38f7Svb70745 	}
127261ef38f7Svb70745 }
127361ef38f7Svb70745 
127461ef38f7Svb70745 /*
127561ef38f7Svb70745  * Called by memscrub_scan().
127661ef38f7Svb70745  * pa: physical address of span to be searched in global list.
127761ef38f7Svb70745  */
127861ef38f7Svb70745 static int
127961ef38f7Svb70745 memscrub_page_retire_span_search(ms_paddr_t pa)
128061ef38f7Svb70745 {
128161ef38f7Svb70745 	memscrub_page_retire_span_t *next_span = memscrub_page_retire_span_list;
128261ef38f7Svb70745 
128361ef38f7Svb70745 	while (next_span) {
128461ef38f7Svb70745 		if (pa == next_span->address)
128561ef38f7Svb70745 			return (1);
128661ef38f7Svb70745 		next_span = next_span->next;
128761ef38f7Svb70745 	}
128861ef38f7Svb70745 	return (0);
128961ef38f7Svb70745 }
129061ef38f7Svb70745 
129161ef38f7Svb70745 /*
129261ef38f7Svb70745  * Called from new_memscrub() as a result of memory delete.
129361ef38f7Svb70745  * Using page_numtopp_nolock() to determine if we have valid PA.
129461ef38f7Svb70745  */
129561ef38f7Svb70745 static void
129661ef38f7Svb70745 memscrub_page_retire_span_list_update(void)
129761ef38f7Svb70745 {
129861ef38f7Svb70745 	memscrub_page_retire_span_t *prev, *cur, *next;
129961ef38f7Svb70745 
130061ef38f7Svb70745 	if (memscrub_page_retire_span_list == NULL)
130161ef38f7Svb70745 		return;
130261ef38f7Svb70745 
130361ef38f7Svb70745 	prev = cur = memscrub_page_retire_span_list;
130461ef38f7Svb70745 	next = cur->next;
130561ef38f7Svb70745 
130661ef38f7Svb70745 	while (cur) {
130761ef38f7Svb70745 		if (page_numtopp_nolock(mmu_btop(cur->address)) == NULL) {
130861ef38f7Svb70745 			if (cur == memscrub_page_retire_span_list) {
130961ef38f7Svb70745 				memscrub_page_retire_span_list = next;
131061ef38f7Svb70745 				kmem_free(cur,
131161ef38f7Svb70745 				    sizeof (memscrub_page_retire_span_t));
131261ef38f7Svb70745 				prev = cur = memscrub_page_retire_span_list;
131361ef38f7Svb70745 			} else {
131461ef38f7Svb70745 				prev->next = cur->next;
131561ef38f7Svb70745 				kmem_free(cur,
131661ef38f7Svb70745 				    sizeof (memscrub_page_retire_span_t));
131761ef38f7Svb70745 				cur = next;
131861ef38f7Svb70745 			}
131961ef38f7Svb70745 		} else {
132061ef38f7Svb70745 			prev = cur;
132161ef38f7Svb70745 			cur = next;
132261ef38f7Svb70745 		}
132361ef38f7Svb70745 		if (cur != NULL)
132461ef38f7Svb70745 			next = cur->next;
132561ef38f7Svb70745 	}
132661ef38f7Svb70745 }
132761ef38f7Svb70745 
132861ef38f7Svb70745 /*
13297c478bd9Sstevel@tonic-gate  * The memory add/delete callback mechanism does not pass in the
13307c478bd9Sstevel@tonic-gate  * page ranges. The phys_install list has been updated though, so
13317c478bd9Sstevel@tonic-gate  * create a new scrub list from it.
13327c478bd9Sstevel@tonic-gate  */
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate static int
133561ef38f7Svb70745 new_memscrub(int update_page_retire_list)
13367c478bd9Sstevel@tonic-gate {
13377c478bd9Sstevel@tonic-gate 	struct memlist *src, *list, *old_list;
13387c478bd9Sstevel@tonic-gate 	uint_t npgs;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	/*
13417c478bd9Sstevel@tonic-gate 	 * copy phys_install to memscrub_memlist
13427c478bd9Sstevel@tonic-gate 	 */
13437c478bd9Sstevel@tonic-gate 	list = NULL;
13447c478bd9Sstevel@tonic-gate 	npgs = 0;
13457c478bd9Sstevel@tonic-gate 	memlist_read_lock();
1346*56f33205SJonathan Adams 	for (src = phys_install; src; src = src->ml_next) {
1347*56f33205SJonathan Adams 		if (memscrub_add_span_gen((pfn_t)(src->ml_address >> PAGESHIFT),
1348*56f33205SJonathan Adams 		    (pgcnt_t)(src->ml_size >> PAGESHIFT), &list, &npgs)) {
13497c478bd9Sstevel@tonic-gate 			memlist_read_unlock();
13507c478bd9Sstevel@tonic-gate 			while (list) {
13517c478bd9Sstevel@tonic-gate 				struct memlist *el;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 				el = list;
1354*56f33205SJonathan Adams 				list = list->ml_next;
13557c478bd9Sstevel@tonic-gate 				kmem_free(el, sizeof (struct memlist));
13567c478bd9Sstevel@tonic-gate 			}
13577c478bd9Sstevel@tonic-gate 			return (-1);
13587c478bd9Sstevel@tonic-gate 		}
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	mutex_enter(&memscrub_lock);
13637c478bd9Sstevel@tonic-gate 	memscrub_phys_pages = npgs;
13647c478bd9Sstevel@tonic-gate 	old_list = memscrub_memlist;
13657c478bd9Sstevel@tonic-gate 	memscrub_memlist = list;
136661ef38f7Svb70745 
136761ef38f7Svb70745 	if (update_page_retire_list)
136861ef38f7Svb70745 		memscrub_page_retire_span_list_update();
136961ef38f7Svb70745 
13707c478bd9Sstevel@tonic-gate 	mutex_exit(&memscrub_lock);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	while (old_list) {
13737c478bd9Sstevel@tonic-gate 		struct memlist *el;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		el = old_list;
1376*56f33205SJonathan Adams 		old_list = old_list->ml_next;
13777c478bd9Sstevel@tonic-gate 		kmem_free(el, sizeof (struct memlist));
13787c478bd9Sstevel@tonic-gate 	}
137961ef38f7Svb70745 
13807c478bd9Sstevel@tonic-gate 	return (0);
13817c478bd9Sstevel@tonic-gate }
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13847c478bd9Sstevel@tonic-gate static void
13857c478bd9Sstevel@tonic-gate memscrub_mem_config_post_add(
13867c478bd9Sstevel@tonic-gate 	void *arg,
13877c478bd9Sstevel@tonic-gate 	pgcnt_t delta_pages)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * We increment pause_memscrub before entering new_memscrub(). This
13917c478bd9Sstevel@tonic-gate 	 * will force the memscrubber to sleep, allowing the DR callback
13927c478bd9Sstevel@tonic-gate 	 * thread to acquire memscrub_lock in new_memscrub(). The use of
13937c478bd9Sstevel@tonic-gate 	 * atomic_add_32() allows concurrent memory DR operations to use the
13947c478bd9Sstevel@tonic-gate 	 * callbacks safely.
13957c478bd9Sstevel@tonic-gate 	 */
13967c478bd9Sstevel@tonic-gate 	atomic_add_32(&pause_memscrub, 1);
13977c478bd9Sstevel@tonic-gate 	ASSERT(pause_memscrub != 0);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * "Don't care" if we are not scrubbing new memory.
14017c478bd9Sstevel@tonic-gate 	 */
140261ef38f7Svb70745 	(void) new_memscrub(0);		/* retain page retire list */
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	/* Restore the pause setting. */
14057c478bd9Sstevel@tonic-gate 	atomic_add_32(&pause_memscrub, -1);
14067c478bd9Sstevel@tonic-gate }
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14097c478bd9Sstevel@tonic-gate static int
14107c478bd9Sstevel@tonic-gate memscrub_mem_config_pre_del(
14117c478bd9Sstevel@tonic-gate 	void *arg,
14127c478bd9Sstevel@tonic-gate 	pgcnt_t delta_pages)
14137c478bd9Sstevel@tonic-gate {
14147c478bd9Sstevel@tonic-gate 	/* Nothing to do. */
14157c478bd9Sstevel@tonic-gate 	return (0);
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14197c478bd9Sstevel@tonic-gate static void
14207c478bd9Sstevel@tonic-gate memscrub_mem_config_post_del(
14217c478bd9Sstevel@tonic-gate 	void *arg,
14227c478bd9Sstevel@tonic-gate 	pgcnt_t delta_pages,
14237c478bd9Sstevel@tonic-gate 	int cancelled)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	/*
14267c478bd9Sstevel@tonic-gate 	 * We increment pause_memscrub before entering new_memscrub(). This
14277c478bd9Sstevel@tonic-gate 	 * will force the memscrubber to sleep, allowing the DR callback
14287c478bd9Sstevel@tonic-gate 	 * thread to acquire memscrub_lock in new_memscrub(). The use of
14297c478bd9Sstevel@tonic-gate 	 * atomic_add_32() allows concurrent memory DR operations to use the
14307c478bd9Sstevel@tonic-gate 	 * callbacks safely.
14317c478bd9Sstevel@tonic-gate 	 */
14327c478bd9Sstevel@tonic-gate 	atomic_add_32(&pause_memscrub, 1);
14337c478bd9Sstevel@tonic-gate 	ASSERT(pause_memscrub != 0);
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/*
14367c478bd9Sstevel@tonic-gate 	 * Must stop scrubbing deleted memory as it may be disconnected.
14377c478bd9Sstevel@tonic-gate 	 */
143861ef38f7Svb70745 	if (new_memscrub(1)) {	/* update page retire list */
14397c478bd9Sstevel@tonic-gate 		disable_memscrub = 1;
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	/* Restore the pause setting. */
14437c478bd9Sstevel@tonic-gate 	atomic_add_32(&pause_memscrub, -1);
14447c478bd9Sstevel@tonic-gate }
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate static kphysm_setup_vector_t memscrub_mem_config_vec = {
14477c478bd9Sstevel@tonic-gate 	KPHYSM_SETUP_VECTOR_VERSION,
14487c478bd9Sstevel@tonic-gate 	memscrub_mem_config_post_add,
14497c478bd9Sstevel@tonic-gate 	memscrub_mem_config_pre_del,
14507c478bd9Sstevel@tonic-gate 	memscrub_mem_config_post_del,
14517c478bd9Sstevel@tonic-gate };
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate static void
14547c478bd9Sstevel@tonic-gate memscrub_init_mem_config()
14557c478bd9Sstevel@tonic-gate {
14567c478bd9Sstevel@tonic-gate 	int ret;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	ret = kphysm_setup_func_register(&memscrub_mem_config_vec,
14597c478bd9Sstevel@tonic-gate 	    (void *)NULL);
14607c478bd9Sstevel@tonic-gate 	ASSERT(ret == 0);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate static void
14647c478bd9Sstevel@tonic-gate memscrub_uninit_mem_config()
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	/* This call is OK if the register call was not done. */
14677c478bd9Sstevel@tonic-gate 	kphysm_setup_func_unregister(&memscrub_mem_config_vec, (void *)NULL);
14687c478bd9Sstevel@tonic-gate }
1469