1917Selowe /*
2917Selowe  * CDDL HEADER START
3917Selowe  *
4917Selowe  * The contents of this file are subject to the terms of the
53253Smec  * Common Development and Distribution License (the "License").
63253Smec  * You may not use this file except in compliance with the License.
7917Selowe  *
8917Selowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9917Selowe  * or http://www.opensolaris.org/os/licensing.
10917Selowe  * See the License for the specific language governing permissions
11917Selowe  * and limitations under the License.
12917Selowe  *
13917Selowe  * When distributing Covered Code, include this CDDL HEADER in each
14917Selowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15917Selowe  * If applicable, add the following below this CDDL HEADER, with the
16917Selowe  * fields enclosed by brackets "[]" replaced with your own identifying
17917Selowe  * information: Portions Copyright [yyyy] [name of copyright owner]
18917Selowe  *
19917Selowe  * CDDL HEADER END
20917Selowe  */
21917Selowe /*
22*3480Sjfrank  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23917Selowe  * Use is subject to license terms.
24917Selowe  */
25917Selowe 
26917Selowe #pragma ident	"%Z%%M%	%I%	%E% SMI"
27917Selowe 
28917Selowe /*
29917Selowe  * Page Retire - Big Theory Statement.
30917Selowe  *
31917Selowe  * This file handles removing sections of faulty memory from use when the
32917Selowe  * user land FMA Diagnosis Engine requests that a page be removed or when
33917Selowe  * a CE or UE is detected by the hardware.
34917Selowe  *
35917Selowe  * In the bad old days, the kernel side of Page Retire did a lot of the work
36917Selowe  * on its own. Now, with the DE keeping track of errors, the kernel side is
37917Selowe  * rather simple minded on most platforms.
38917Selowe  *
39917Selowe  * Errors are all reflected to the DE, and after digesting the error and
40917Selowe  * looking at all previously reported errors, the DE decides what should
41917Selowe  * be done about the current error. If the DE wants a particular page to
42917Selowe  * be retired, then the kernel page retire code is invoked via an ioctl.
43917Selowe  * On non-FMA platforms, the ue_drain and ce_drain paths ends up calling
44917Selowe  * page retire to handle the error. Since page retire is just a simple
45917Selowe  * mechanism it doesn't need to differentiate between the different callers.
46917Selowe  *
47917Selowe  * The p_toxic field in the page_t is used to indicate which errors have
48917Selowe  * occurred and what action has been taken on a given page. Because errors are
49917Selowe  * reported without regard to the locked state of a page, no locks are used
50917Selowe  * to SET the error bits in p_toxic. However, in order to clear the error
51917Selowe  * bits, the page_t must be held exclusively locked.
52917Selowe  *
53917Selowe  * When page_retire() is called, it must be able to acquire locks, sleep, etc.
54917Selowe  * It must not be called from high-level interrupt context.
55917Selowe  *
56917Selowe  * Depending on how the requested page is being used at the time of the retire
57917Selowe  * request (and on the availability of sufficient system resources), the page
58917Selowe  * may be retired immediately, or just marked for retirement later. For
59917Selowe  * example, locked pages are marked, while free pages are retired. Multiple
60917Selowe  * requests may be made to retire the same page, although there is no need
61917Selowe  * to: once the p_toxic flags are set, the page will be retired as soon as it
62917Selowe  * can be exclusively locked.
63917Selowe  *
64917Selowe  * The retire mechanism is driven centrally out of page_unlock(). To expedite
65917Selowe  * the retirement of pages, further requests for SE_SHARED locks are denied
66917Selowe  * as long as a page retirement is pending. In addition, as long as pages are
67917Selowe  * pending retirement a background thread runs periodically trying to retire
68917Selowe  * those pages. Pages which could not be retired while the system is running
69917Selowe  * are scrubbed prior to rebooting to avoid latent errors on the next boot.
70917Selowe  *
711338Selowe  * UE pages without persistent errors are scrubbed and returned to service.
721338Selowe  * Recidivist pages, as well as FMA-directed requests for retirement, result
731338Selowe  * in the page being taken out of service. Once the decision is made to take
741338Selowe  * a page out of service, the page is cleared, hashed onto the retired_pages
751338Selowe  * vnode, marked as retired, and it is unlocked.  No other requesters (except
761338Selowe  * for unretire) are allowed to lock retired pages.
77917Selowe  *
78917Selowe  * The public routines return (sadly) 0 if they worked and a non-zero error
79917Selowe  * value if something went wrong. This is done for the ioctl side of the
80917Selowe  * world to allow errors to be reflected all the way out to user land. The
81917Selowe  * non-zero values are explained in comments atop each function.
82917Selowe  */
83917Selowe 
84917Selowe /*
85917Selowe  * Things to fix:
86917Selowe  *
873253Smec  * 	1. Trying to retire non-relocatable kvp pages may result in a
88917Selowe  *      quagmire. This is because seg_kmem() no longer keeps its pages locked,
89917Selowe  *      and calls page_lookup() in the free path; since kvp pages are modified
90917Selowe  *      and don't have a usable backing store, page_retire() can't do anything
91917Selowe  *      with them, and we'll keep denying the lock to seg_kmem_free() in a
92917Selowe  *      vicious cycle. To prevent that, we don't deny locks to kvp pages, and
933253Smec  *      hence only try to retire a page from page_unlock() in the free path.
94917Selowe  *      Since most kernel pages are indefinitely held anyway, and don't
95917Selowe  *      participate in I/O, this is of little consequence.
96917Selowe  *
973253Smec  *      2. Low memory situations will be interesting. If we don't have
98917Selowe  *      enough memory for page_relocate() to succeed, we won't be able to
99917Selowe  *      retire dirty pages; nobody will be able to push them out to disk
100917Selowe  *      either, since we aggressively deny the page lock. We could change
101917Selowe  *      fsflush so it can recognize this situation, grab the lock, and push
102917Selowe  *      the page out, where we'll catch it in the free path and retire it.
103917Selowe  *
1043253Smec  *	3. Beware of places that have code like this in them:
105917Selowe  *
106917Selowe  *		if (! page_tryupgrade(pp)) {
107917Selowe  *			page_unlock(pp);
108917Selowe  *			while (! page_lock(pp, SE_EXCL, NULL, P_RECLAIM)) {
109917Selowe  *				/ *NOTHING* /
110917Selowe  *			}
111917Selowe  *		}
112917Selowe  *		page_free(pp);
113917Selowe  *
114917Selowe  *	The problem is that pp can change identity right after the
115917Selowe  *	page_unlock() call.  In particular, page_retire() can step in
116917Selowe  *	there, change pp's identity, and hash pp onto the retired_vnode.
117917Selowe  *
118917Selowe  *	Of course, other functions besides page_retire() can have the
119917Selowe  *	same effect. A kmem reader can waltz by, set up a mapping to the
120917Selowe  *	page, and then unlock the page. Page_free() will then go castors
121917Selowe  *	up. So if anybody is doing this, it's already a bug.
122917Selowe  *
1233253Smec  *      4. mdboot()'s call into page_retire_mdboot() should probably be
124917Selowe  *      moved lower. Where the call is made now, we can get into trouble
125917Selowe  *      by scrubbing a kernel page that is then accessed later.
126917Selowe  */
127917Selowe 
128917Selowe #include <sys/types.h>
129917Selowe #include <sys/param.h>
130917Selowe #include <sys/systm.h>
131917Selowe #include <sys/mman.h>
132917Selowe #include <sys/vnode.h>
133917Selowe #include <sys/cmn_err.h>
134917Selowe #include <sys/ksynch.h>
135917Selowe #include <sys/thread.h>
136917Selowe #include <sys/disp.h>
137917Selowe #include <sys/ontrap.h>
138917Selowe #include <sys/vmsystm.h>
139917Selowe #include <sys/mem_config.h>
140917Selowe #include <sys/atomic.h>
141917Selowe #include <sys/callb.h>
142917Selowe #include <vm/page.h>
143917Selowe #include <vm/vm_dep.h>
144917Selowe #include <vm/as.h>
145917Selowe #include <vm/hat.h>
146917Selowe 
147917Selowe /*
148917Selowe  * vnode for all pages which are retired from the VM system;
149917Selowe  */
150917Selowe vnode_t *retired_pages;
151917Selowe 
1523253Smec static int page_retire_pp_finish(page_t *, void *, uint_t);
153917Selowe 
154917Selowe /*
155917Selowe  * Make a list of all of the pages that have been marked for retirement
156917Selowe  * but are not yet retired.  At system shutdown, we will scrub all of the
157917Selowe  * pages in the list in case there are outstanding UEs.  Then, we
158917Selowe  * cross-check this list against the number of pages that are yet to be
159917Selowe  * retired, and if we find inconsistencies, we scan every page_t in the
160917Selowe  * whole system looking for any pages that need to be scrubbed for UEs.
161917Selowe  * The background thread also uses this queue to determine which pages
162917Selowe  * it should keep trying to retire.
163917Selowe  */
164917Selowe #ifdef	DEBUG
165917Selowe #define	PR_PENDING_QMAX	32
166917Selowe #else	/* DEBUG */
167917Selowe #define	PR_PENDING_QMAX	256
168917Selowe #endif	/* DEBUG */
169917Selowe page_t		*pr_pending_q[PR_PENDING_QMAX];
170917Selowe kmutex_t	pr_q_mutex;
171917Selowe 
172917Selowe /*
173917Selowe  * Page retire global kstats
174917Selowe  */
175917Selowe struct page_retire_kstat {
176917Selowe 	kstat_named_t	pr_retired;
177917Selowe 	kstat_named_t	pr_requested;
178917Selowe 	kstat_named_t	pr_requested_free;
179917Selowe 	kstat_named_t	pr_enqueue_fail;
180917Selowe 	kstat_named_t	pr_dequeue_fail;
181917Selowe 	kstat_named_t	pr_pending;
182917Selowe 	kstat_named_t	pr_failed;
183917Selowe 	kstat_named_t	pr_failed_kernel;
184917Selowe 	kstat_named_t	pr_limit;
185917Selowe 	kstat_named_t	pr_limit_exceeded;
186917Selowe 	kstat_named_t	pr_fma;
187917Selowe 	kstat_named_t	pr_mce;
188917Selowe 	kstat_named_t	pr_ue;
189917Selowe 	kstat_named_t	pr_ue_cleared_retire;
190917Selowe 	kstat_named_t	pr_ue_cleared_free;
191917Selowe 	kstat_named_t	pr_ue_persistent;
192917Selowe 	kstat_named_t	pr_unretired;
193917Selowe };
194917Selowe 
195917Selowe static struct page_retire_kstat page_retire_kstat = {
196917Selowe 	{ "pages_retired",		KSTAT_DATA_UINT64},
197917Selowe 	{ "pages_retire_request",	KSTAT_DATA_UINT64},
198917Selowe 	{ "pages_retire_request_free",	KSTAT_DATA_UINT64},
199917Selowe 	{ "pages_notenqueued", 		KSTAT_DATA_UINT64},
200917Selowe 	{ "pages_notdequeued", 		KSTAT_DATA_UINT64},
201917Selowe 	{ "pages_pending", 		KSTAT_DATA_UINT64},
202917Selowe 	{ "pages_deferred",		KSTAT_DATA_UINT64},
203917Selowe 	{ "pages_deferred_kernel",	KSTAT_DATA_UINT64},
204917Selowe 	{ "pages_limit",		KSTAT_DATA_UINT64},
205917Selowe 	{ "pages_limit_exceeded",	KSTAT_DATA_UINT64},
206917Selowe 	{ "pages_fma",			KSTAT_DATA_UINT64},
207917Selowe 	{ "pages_multiple_ce",		KSTAT_DATA_UINT64},
208917Selowe 	{ "pages_ue",			KSTAT_DATA_UINT64},
209917Selowe 	{ "pages_ue_cleared_retired",	KSTAT_DATA_UINT64},
210917Selowe 	{ "pages_ue_cleared_freed",	KSTAT_DATA_UINT64},
211917Selowe 	{ "pages_ue_persistent",	KSTAT_DATA_UINT64},
212917Selowe 	{ "pages_unretired",		KSTAT_DATA_UINT64},
213917Selowe };
214917Selowe 
215917Selowe static kstat_t  *page_retire_ksp = NULL;
216917Selowe 
217917Selowe #define	PR_INCR_KSTAT(stat)	\
218917Selowe 	atomic_add_64(&(page_retire_kstat.stat.value.ui64), 1)
219917Selowe #define	PR_DECR_KSTAT(stat)	\
220917Selowe 	atomic_add_64(&(page_retire_kstat.stat.value.ui64), -1)
221917Selowe 
222917Selowe #define	PR_KSTAT_RETIRED_CE	(page_retire_kstat.pr_mce.value.ui64)
223917Selowe #define	PR_KSTAT_RETIRED_FMA	(page_retire_kstat.pr_fma.value.ui64)
224917Selowe #define	PR_KSTAT_RETIRED_NOTUE	(PR_KSTAT_RETIRED_CE + PR_KSTAT_RETIRED_FMA)
225917Selowe #define	PR_KSTAT_PENDING	(page_retire_kstat.pr_pending.value.ui64)
226917Selowe #define	PR_KSTAT_EQFAIL		(page_retire_kstat.pr_enqueue_fail.value.ui64)
227917Selowe #define	PR_KSTAT_DQFAIL		(page_retire_kstat.pr_dequeue_fail.value.ui64)
228917Selowe 
229917Selowe /*
2303253Smec  * page retire kstats to list all retired pages
2313253Smec  */
2323253Smec static int pr_list_kstat_update(kstat_t *ksp, int rw);
2333253Smec static int pr_list_kstat_snapshot(kstat_t *ksp, void *buf, int rw);
2343253Smec kmutex_t pr_list_kstat_mutex;
2353253Smec 
2363253Smec /*
237917Selowe  * Limit the number of multiple CE page retires.
238917Selowe  * The default is 0.1% of physmem, or 1 in 1000 pages. This is set in
239917Selowe  * basis points, where 100 basis points equals one percent.
240917Selowe  */
241917Selowe #define	MCE_BPT	10
242917Selowe uint64_t	max_pages_retired_bps = MCE_BPT;
243917Selowe #define	PAGE_RETIRE_LIMIT	((physmem * max_pages_retired_bps) / 10000)
244917Selowe 
245917Selowe /*
246917Selowe  * Control over the verbosity of page retirement.
247917Selowe  *
248917Selowe  * When set to zero (the default), no messages will be printed.
249917Selowe  * When set to one, summary messages will be printed.
250917Selowe  * When set > one, all messages will be printed.
251917Selowe  *
252917Selowe  * A value of one will trigger detailed messages for retirement operations,
253917Selowe  * and is intended as a platform tunable for processors where FMA's DE does
254917Selowe  * not run (e.g., spitfire). Values > one are intended for debugging only.
255917Selowe  */
256917Selowe int page_retire_messages = 0;
257917Selowe 
258917Selowe /*
259917Selowe  * Control whether or not we return scrubbed UE pages to service.
260917Selowe  * By default we do not since FMA wants to run its diagnostics first
261917Selowe  * and then ask us to unretire the page if it passes. Non-FMA platforms
262917Selowe  * may set this to zero so we will only retire recidivist pages. It should
263917Selowe  * not be changed by the user.
264917Selowe  */
265917Selowe int page_retire_first_ue = 1;
266917Selowe 
267917Selowe /*
268917Selowe  * Master enable for page retire. This prevents a CE or UE early in boot
269917Selowe  * from trying to retire a page before page_retire_init() has finished
270917Selowe  * setting things up. This is internal only and is not a tunable!
271917Selowe  */
272917Selowe static int pr_enable = 0;
273917Selowe 
274917Selowe extern struct vnode kvp;
275917Selowe 
276917Selowe #ifdef	DEBUG
277917Selowe struct page_retire_debug {
2781381Selowe 	int prd_dup1;
2791381Selowe 	int prd_dup2;
2801381Selowe 	int prd_qdup;
281917Selowe 	int prd_noaction;
282917Selowe 	int prd_queued;
283917Selowe 	int prd_notqueued;
284917Selowe 	int prd_dequeue;
285917Selowe 	int prd_top;
286917Selowe 	int prd_locked;
287917Selowe 	int prd_reloc;
288973Selowe 	int prd_relocfail;
289973Selowe 	int prd_mod;
290973Selowe 	int prd_mod_late;
291917Selowe 	int prd_kern;
292917Selowe 	int prd_free;
293917Selowe 	int prd_noreclaim;
294917Selowe 	int prd_hashout;
295917Selowe 	int prd_fma;
296917Selowe 	int prd_uescrubbed;
297917Selowe 	int prd_uenotscrubbed;
298917Selowe 	int prd_mce;
299917Selowe 	int prd_prlocked;
300917Selowe 	int prd_prnotlocked;
301917Selowe 	int prd_prretired;
302917Selowe 	int prd_ulocked;
303917Selowe 	int prd_unotretired;
304917Selowe 	int prd_udestroy;
305917Selowe 	int prd_uhashout;
306917Selowe 	int prd_uunretired;
307917Selowe 	int prd_unotlocked;
308917Selowe 	int prd_checkhit;
3091381Selowe 	int prd_checkmiss_pend;
3101381Selowe 	int prd_checkmiss_noerr;
311917Selowe 	int prd_tctop;
312917Selowe 	int prd_tclocked;
313917Selowe 	int prd_hunt;
314917Selowe 	int prd_dohunt;
315917Selowe 	int prd_earlyhunt;
316917Selowe 	int prd_latehunt;
317917Selowe 	int prd_nofreedemote;
318917Selowe 	int prd_nodemote;
319917Selowe 	int prd_demoted;
320917Selowe } pr_debug;
321917Selowe 
322917Selowe #define	PR_DEBUG(foo)	((pr_debug.foo)++)
323917Selowe 
324917Selowe /*
325917Selowe  * A type histogram. We record the incidence of the various toxic
326917Selowe  * flag combinations along with the interesting page attributes. The
327917Selowe  * goal is to get as many combinations as we can while driving all
328917Selowe  * pr_debug values nonzero (indicating we've exercised all possible
329917Selowe  * code paths across all possible page types). Not all combinations
330917Selowe  * will make sense -- e.g. PRT_MOD|PRT_KERNEL.
331917Selowe  *
332917Selowe  * pr_type offset bit encoding (when examining with a debugger):
333917Selowe  *
334917Selowe  *    PRT_NAMED  - 0x4
335917Selowe  *    PRT_KERNEL - 0x8
336917Selowe  *    PRT_FREE   - 0x10
337917Selowe  *    PRT_MOD    - 0x20
338917Selowe  *    PRT_FMA    - 0x0
339917Selowe  *    PRT_MCE    - 0x40
340917Selowe  *    PRT_UE     - 0x80
341917Selowe  */
342917Selowe 
343917Selowe #define	PRT_NAMED	0x01
344917Selowe #define	PRT_KERNEL	0x02
345917Selowe #define	PRT_FREE	0x04
346917Selowe #define	PRT_MOD		0x08
347917Selowe #define	PRT_FMA		0x00	/* yes, this is not a mistake */
348917Selowe #define	PRT_MCE		0x10
349917Selowe #define	PRT_UE		0x20
350917Selowe #define	PRT_ALL		0x3F
351917Selowe 
352917Selowe int pr_types[PRT_ALL+1];
353917Selowe 
354917Selowe #define	PR_TYPES(pp)	{			\
355917Selowe 	int whichtype = 0;			\
356917Selowe 	if (pp->p_vnode)			\
357917Selowe 		whichtype |= PRT_NAMED;		\
3583290Sjohansen 	if (PP_ISKAS(pp))			\
359917Selowe 		whichtype |= PRT_KERNEL;	\
360917Selowe 	if (PP_ISFREE(pp))			\
361917Selowe 		whichtype |= PRT_FREE;		\
362917Selowe 	if (hat_ismod(pp))			\
363917Selowe 		whichtype |= PRT_MOD;		\
364917Selowe 	if (pp->p_toxic & PR_UE)		\
365917Selowe 		whichtype |= PRT_UE;		\
366917Selowe 	if (pp->p_toxic & PR_MCE)		\
367917Selowe 		whichtype |= PRT_MCE;		\
368917Selowe 	pr_types[whichtype]++;			\
369917Selowe }
370917Selowe 
371917Selowe int recl_calls;
372917Selowe int recl_mtbf = 3;
373917Selowe int reloc_calls;
374917Selowe int reloc_mtbf = 7;
375917Selowe int pr_calls;
376917Selowe int pr_mtbf = 15;
377917Selowe 
378917Selowe #define	MTBF(v, f)	(((++(v)) & (f)) != (f))
379917Selowe 
380917Selowe #else	/* DEBUG */
381917Selowe 
382917Selowe #define	PR_DEBUG(foo)	/* nothing */
383917Selowe #define	PR_TYPES(foo)	/* nothing */
384917Selowe #define	MTBF(v, f)	(1)
385917Selowe 
386917Selowe #endif	/* DEBUG */
387917Selowe 
388917Selowe /*
389917Selowe  * page_retire_done() - completion processing
390917Selowe  *
391917Selowe  * Used by the page_retire code for common completion processing.
392917Selowe  * It keeps track of how many times a given result has happened,
393917Selowe  * and writes out an occasional message.
394917Selowe  *
395917Selowe  * May be called with a NULL pp (PRD_INVALID_PA case).
396917Selowe  */
397917Selowe #define	PRD_INVALID_KEY		-1
398917Selowe #define	PRD_SUCCESS		0
399917Selowe #define	PRD_PENDING		1
400917Selowe #define	PRD_FAILED		2
401917Selowe #define	PRD_DUPLICATE		3
402917Selowe #define	PRD_INVALID_PA		4
403917Selowe #define	PRD_LIMIT		5
404917Selowe #define	PRD_UE_SCRUBBED		6
405917Selowe #define	PRD_UNR_SUCCESS		7
406917Selowe #define	PRD_UNR_CANTLOCK	8
407917Selowe #define	PRD_UNR_NOT		9
408917Selowe 
409917Selowe typedef struct page_retire_op {
410917Selowe 	int	pr_key;		/* one of the PRD_* defines from above */
411917Selowe 	int	pr_count;	/* How many times this has happened */
412917Selowe 	int	pr_retval;	/* return value */
413917Selowe 	int	pr_msglvl;	/* message level - when to print */
414917Selowe 	char	*pr_message;	/* Cryptic message for field service */
415917Selowe } page_retire_op_t;
416917Selowe 
417917Selowe static page_retire_op_t page_retire_ops[] = {
418917Selowe 	/* key			count	retval	msglvl	message */
419917Selowe 	{PRD_SUCCESS,		0,	0,	1,
420917Selowe 		"Page 0x%08x.%08x removed from service"},
421917Selowe 	{PRD_PENDING,		0,	EAGAIN,	2,
422917Selowe 		"Page 0x%08x.%08x will be retired on free"},
423917Selowe 	{PRD_FAILED,		0,	EAGAIN,	0, NULL},
4241381Selowe 	{PRD_DUPLICATE,		0,	EIO,	2,
4251381Selowe 		"Page 0x%08x.%08x already retired or pending"},
426917Selowe 	{PRD_INVALID_PA,	0,	EINVAL, 2,
427917Selowe 		"PA 0x%08x.%08x is not a relocatable page"},
428917Selowe 	{PRD_LIMIT,		0,	0,	1,
429917Selowe 		"Page 0x%08x.%08x not retired due to limit exceeded"},
430917Selowe 	{PRD_UE_SCRUBBED,	0,	0,	1,
431917Selowe 		"Previously reported error on page 0x%08x.%08x cleared"},
432917Selowe 	{PRD_UNR_SUCCESS,	0,	0,	1,
433917Selowe 		"Page 0x%08x.%08x returned to service"},
434917Selowe 	{PRD_UNR_CANTLOCK,	0,	EAGAIN,	2,
435917Selowe 		"Page 0x%08x.%08x could not be unretired"},
4361381Selowe 	{PRD_UNR_NOT,		0,	EIO,	2,
437917Selowe 		"Page 0x%08x.%08x is not retired"},
438917Selowe 	{PRD_INVALID_KEY,	0,	0,	0, NULL} /* MUST BE LAST! */
439917Selowe };
440917Selowe 
441917Selowe /*
442917Selowe  * print a message if page_retire_messages is true.
443917Selowe  */
444917Selowe #define	PR_MESSAGE(debuglvl, msglvl, msg, pa)				\
445917Selowe {									\
446917Selowe 	uint64_t p = (uint64_t)pa;					\
447917Selowe 	if (page_retire_messages >= msglvl && msg != NULL) {		\
448917Selowe 		cmn_err(debuglvl, msg,					\
449917Selowe 		    (uint32_t)(p >> 32), (uint32_t)p);			\
450917Selowe 	}								\
451917Selowe }
452917Selowe 
453917Selowe /*
454917Selowe  * Note that multiple bits may be set in a single settoxic operation.
455917Selowe  * May be called without the page locked.
456917Selowe  */
457917Selowe void
458917Selowe page_settoxic(page_t *pp, uchar_t bits)
459917Selowe {
460917Selowe 	atomic_or_8(&pp->p_toxic, bits);
461917Selowe }
462917Selowe 
463917Selowe /*
464917Selowe  * Note that multiple bits may cleared in a single clrtoxic operation.
4651338Selowe  * Must be called with the page exclusively locked to prevent races which
4661338Selowe  * may attempt to retire a page without any toxic bits set.
4673253Smec  * Note that the PR_CAPTURE bit can be cleared without the exclusive lock
4683253Smec  * being held as there is a separate mutex which protects that bit.
469917Selowe  */
470917Selowe void
471917Selowe page_clrtoxic(page_t *pp, uchar_t bits)
472917Selowe {
4733253Smec 	ASSERT((bits & PR_CAPTURE) || PAGE_EXCL(pp));
474917Selowe 	atomic_and_8(&pp->p_toxic, ~bits);
475917Selowe }
476917Selowe 
477917Selowe /*
478917Selowe  * Prints any page retire messages to the user, and decides what
479917Selowe  * error code is appropriate for the condition reported.
480917Selowe  */
481917Selowe static int
482917Selowe page_retire_done(page_t *pp, int code)
483917Selowe {
484917Selowe 	page_retire_op_t *prop;
485917Selowe 	uint64_t	pa = 0;
486917Selowe 	int		i;
487917Selowe 
488917Selowe 	if (pp != NULL) {
4891338Selowe 		pa = mmu_ptob((uint64_t)pp->p_pagenum);
490917Selowe 	}
491917Selowe 
492917Selowe 	prop = NULL;
493917Selowe 	for (i = 0; page_retire_ops[i].pr_key != PRD_INVALID_KEY; i++) {
494917Selowe 		if (page_retire_ops[i].pr_key == code) {
495917Selowe 			prop = &page_retire_ops[i];
496917Selowe 			break;
497917Selowe 		}
498917Selowe 	}
499917Selowe 
500917Selowe #ifdef	DEBUG
501917Selowe 	if (page_retire_ops[i].pr_key == PRD_INVALID_KEY) {
502917Selowe 		cmn_err(CE_PANIC, "page_retire_done: Invalid opcode %d", code);
503917Selowe 	}
504917Selowe #endif
505917Selowe 
506917Selowe 	ASSERT(prop->pr_key == code);
507917Selowe 
508917Selowe 	prop->pr_count++;
509917Selowe 
510917Selowe 	PR_MESSAGE(CE_NOTE, prop->pr_msglvl, prop->pr_message, pa);
511917Selowe 	if (pp != NULL) {
512917Selowe 		page_settoxic(pp, PR_MSG);
513917Selowe 	}
514917Selowe 
515917Selowe 	return (prop->pr_retval);
516917Selowe }
517917Selowe 
518917Selowe /*
519917Selowe  * Act like page_destroy(), but instead of freeing the page, hash it onto
520917Selowe  * the retired_pages vnode, and mark it retired.
521917Selowe  *
522917Selowe  * For fun, we try to scrub the page until it's squeaky clean.
523917Selowe  * availrmem is adjusted here.
524917Selowe  */
525917Selowe static void
526917Selowe page_retire_destroy(page_t *pp)
527917Selowe {
528973Selowe 	u_offset_t off = (u_offset_t)((uintptr_t)pp);
529973Selowe 
530917Selowe 	ASSERT(PAGE_EXCL(pp));
531917Selowe 	ASSERT(!PP_ISFREE(pp));
532917Selowe 	ASSERT(pp->p_szc == 0);
533917Selowe 	ASSERT(!hat_page_is_mapped(pp));
534917Selowe 	ASSERT(!pp->p_vnode);
535917Selowe 
536917Selowe 	page_clr_all_props(pp);
537917Selowe 	pagescrub(pp, 0, MMU_PAGESIZE);
538917Selowe 
539917Selowe 	pp->p_next = NULL;
540917Selowe 	pp->p_prev = NULL;
541973Selowe 	if (page_hashin(pp, retired_pages, off, NULL) == 0) {
542917Selowe 		cmn_err(CE_PANIC, "retired page %p hashin failed", (void *)pp);
543917Selowe 	}
544917Selowe 
545917Selowe 	page_settoxic(pp, PR_RETIRED);
546917Selowe 	PR_INCR_KSTAT(pr_retired);
547917Selowe 
548917Selowe 	if (pp->p_toxic & PR_FMA) {
549917Selowe 		PR_INCR_KSTAT(pr_fma);
550917Selowe 	} else if (pp->p_toxic & PR_UE) {
551917Selowe 		PR_INCR_KSTAT(pr_ue);
552917Selowe 	} else {
553917Selowe 		PR_INCR_KSTAT(pr_mce);
554917Selowe 	}
555917Selowe 
556917Selowe 	mutex_enter(&freemem_lock);
557917Selowe 	availrmem--;
558917Selowe 	mutex_exit(&freemem_lock);
559917Selowe 
560917Selowe 	page_unlock(pp);
561917Selowe }
562917Selowe 
563917Selowe /*
564917Selowe  * Check whether the number of pages which have been retired already exceeds
565917Selowe  * the maximum allowable percentage of memory which may be retired.
566917Selowe  *
567917Selowe  * Returns 1 if the limit has been exceeded.
568917Selowe  */
569917Selowe static int
570917Selowe page_retire_limit(void)
571917Selowe {
572917Selowe 	if (PR_KSTAT_RETIRED_NOTUE >= (uint64_t)PAGE_RETIRE_LIMIT) {
573917Selowe 		PR_INCR_KSTAT(pr_limit_exceeded);
574917Selowe 		return (1);
575917Selowe 	}
576917Selowe 
577917Selowe 	return (0);
578917Selowe }
579917Selowe 
580917Selowe #define	MSG_DM	"Data Mismatch occurred at PA 0x%08x.%08x"		\
581917Selowe 	"[ 0x%x != 0x%x ] while attempting to clear previously "	\
582917Selowe 	"reported error; page removed from service"
583917Selowe 
584917Selowe #define	MSG_UE	"Uncorrectable Error occurred at PA 0x%08x.%08x while "	\
585917Selowe 	"attempting to clear previously reported error; page removed "	\
586917Selowe 	"from service"
587917Selowe 
588917Selowe /*
589917Selowe  * Attempt to clear a UE from a page.
590917Selowe  * Returns 1 if the error has been successfully cleared.
591917Selowe  */
592917Selowe static int
593917Selowe page_clear_transient_ue(page_t *pp)
594917Selowe {
595917Selowe 	caddr_t		kaddr;
596917Selowe 	uint8_t		rb, wb;
597917Selowe 	uint64_t	pa;
598917Selowe 	uint32_t	pa_hi, pa_lo;
599917Selowe 	on_trap_data_t	otd;
600917Selowe 	int		errors = 0;
601917Selowe 	int		i;
602917Selowe 
603917Selowe 	ASSERT(PAGE_EXCL(pp));
604917Selowe 	ASSERT(PP_PR_REQ(pp));
605917Selowe 	ASSERT(pp->p_szc == 0);
606917Selowe 	ASSERT(!hat_page_is_mapped(pp));
607917Selowe 
608917Selowe 	/*
609917Selowe 	 * Clear the page and attempt to clear the UE.  If we trap
610917Selowe 	 * on the next access to the page, we know the UE has recurred.
611917Selowe 	 */
612917Selowe 	pagescrub(pp, 0, PAGESIZE);
613917Selowe 
614917Selowe 	/*
615917Selowe 	 * Map the page and write a bunch of bit patterns to compare
616917Selowe 	 * what we wrote with what we read back.  This isn't a perfect
617917Selowe 	 * test but it should be good enough to catch most of the
618917Selowe 	 * recurring UEs. If this fails to catch a recurrent UE, we'll
619917Selowe 	 * retire the page the next time we see a UE on the page.
620917Selowe 	 */
621917Selowe 	kaddr = ppmapin(pp, PROT_READ|PROT_WRITE, (caddr_t)-1);
622917Selowe 
623917Selowe 	pa = ptob((uint64_t)page_pptonum(pp));
624917Selowe 	pa_hi = (uint32_t)(pa >> 32);
625917Selowe 	pa_lo = (uint32_t)pa;
626917Selowe 
627917Selowe 	/*
628917Selowe 	 * Fill the page with each (0x00 - 0xFF] bit pattern, flushing
629917Selowe 	 * the cache in between reading and writing.  We do this under
630917Selowe 	 * on_trap() protection to avoid recursion.
631917Selowe 	 */
632917Selowe 	if (on_trap(&otd, OT_DATA_EC)) {
633917Selowe 		PR_MESSAGE(CE_WARN, 1, MSG_UE, pa);
634917Selowe 		errors = 1;
635917Selowe 	} else {
636917Selowe 		for (wb = 0xff; wb > 0; wb--) {
637917Selowe 			for (i = 0; i < PAGESIZE; i++) {
638917Selowe 				kaddr[i] = wb;
639917Selowe 			}
640917Selowe 
641917Selowe 			sync_data_memory(kaddr, PAGESIZE);
642917Selowe 
643917Selowe 			for (i = 0; i < PAGESIZE; i++) {
644917Selowe 				rb = kaddr[i];
645917Selowe 				if (rb != wb) {
646917Selowe 					/*
647917Selowe 					 * We had a mismatch without a trap.
648917Selowe 					 * Uh-oh. Something is really wrong
649917Selowe 					 * with this system.
650917Selowe 					 */
651917Selowe 					if (page_retire_messages) {
652917Selowe 						cmn_err(CE_WARN, MSG_DM,
653917Selowe 						    pa_hi, pa_lo, rb, wb);
654917Selowe 					}
655917Selowe 					errors = 1;
656917Selowe 					goto out;	/* double break */
657917Selowe 				}
658917Selowe 			}
659917Selowe 		}
660917Selowe 	}
661917Selowe out:
662917Selowe 	no_trap();
663917Selowe 	ppmapout(kaddr);
664917Selowe 
665917Selowe 	return (errors ? 0 : 1);
666917Selowe }
667917Selowe 
668917Selowe /*
669917Selowe  * Try to clear a page_t with a single UE. If the UE was transient, it is
670917Selowe  * returned to service, and we return 1. Otherwise we return 0 meaning
671917Selowe  * that further processing is required to retire the page.
672917Selowe  */
673917Selowe static int
674917Selowe page_retire_transient_ue(page_t *pp)
675917Selowe {
676917Selowe 	ASSERT(PAGE_EXCL(pp));
677917Selowe 	ASSERT(!hat_page_is_mapped(pp));
678917Selowe 
679917Selowe 	/*
680917Selowe 	 * If this page is a repeat offender, retire him under the
681917Selowe 	 * "two strikes and you're out" rule. The caller is responsible
682917Selowe 	 * for scrubbing the page to try to clear the error.
683917Selowe 	 */
684917Selowe 	if (pp->p_toxic & PR_UE_SCRUBBED) {
685917Selowe 		PR_INCR_KSTAT(pr_ue_persistent);
686917Selowe 		return (0);
687917Selowe 	}
688917Selowe 
689917Selowe 	if (page_clear_transient_ue(pp)) {
690917Selowe 		/*
691917Selowe 		 * We set the PR_SCRUBBED_UE bit; if we ever see this
692917Selowe 		 * page again, we will retire it, no questions asked.
693917Selowe 		 */
694917Selowe 		page_settoxic(pp, PR_UE_SCRUBBED);
695917Selowe 
696917Selowe 		if (page_retire_first_ue) {
697917Selowe 			PR_INCR_KSTAT(pr_ue_cleared_retire);
698917Selowe 			return (0);
699917Selowe 		} else {
700917Selowe 			PR_INCR_KSTAT(pr_ue_cleared_free);
701917Selowe 
7023253Smec 			page_clrtoxic(pp, PR_UE | PR_MCE | PR_MSG);
703917Selowe 
704917Selowe 			/* LINTED: CONSTCOND */
705917Selowe 			VN_DISPOSE(pp, B_FREE, 1, kcred);
706917Selowe 			return (1);
707917Selowe 		}
708917Selowe 	}
709917Selowe 
710917Selowe 	PR_INCR_KSTAT(pr_ue_persistent);
711917Selowe 	return (0);
712917Selowe }
713917Selowe 
714917Selowe /*
715917Selowe  * Update the statistics dynamically when our kstat is read.
716917Selowe  */
717917Selowe static int
718917Selowe page_retire_kstat_update(kstat_t *ksp, int rw)
719917Selowe {
720917Selowe 	struct page_retire_kstat *pr;
721917Selowe 
722917Selowe 	if (ksp == NULL)
723917Selowe 	    return (EINVAL);
724917Selowe 
725917Selowe 	switch (rw) {
726917Selowe 
727917Selowe 	case KSTAT_READ:
728917Selowe 		pr = (struct page_retire_kstat *)ksp->ks_data;
729917Selowe 		ASSERT(pr == &page_retire_kstat);
730917Selowe 		pr->pr_limit.value.ui64 = PAGE_RETIRE_LIMIT;
731917Selowe 		return (0);
732917Selowe 
733917Selowe 	case KSTAT_WRITE:
734917Selowe 		return (EACCES);
735917Selowe 
736917Selowe 	default:
737917Selowe 		return (EINVAL);
738917Selowe 	}
739917Selowe 	/*NOTREACHED*/
740917Selowe }
741917Selowe 
7423253Smec static int
7433253Smec pr_list_kstat_update(kstat_t *ksp, int rw)
7443253Smec {
7453253Smec 	uint_t count;
7463253Smec 	page_t *pp;
7473253Smec 	kmutex_t *vphm;
7483253Smec 
7493253Smec 	if (rw == KSTAT_WRITE)
7503253Smec 		return (EACCES);
7513253Smec 
7523253Smec 	vphm = page_vnode_mutex(retired_pages);
7533253Smec 	mutex_enter(vphm);
7543253Smec 	/* Needs to be under a lock so that for loop will work right */
7553253Smec 	if (retired_pages->v_pages == NULL) {
7563253Smec 		mutex_exit(vphm);
7573253Smec 		ksp->ks_ndata = 0;
7583253Smec 		ksp->ks_data_size = 0;
7593253Smec 		return (0);
7603253Smec 	}
7613253Smec 
7623253Smec 	count = 1;
7633253Smec 	for (pp = retired_pages->v_pages->p_vpnext;
7643253Smec 	    pp != retired_pages->v_pages; pp = pp->p_vpnext) {
7653253Smec 		count++;
7663253Smec 	}
7673253Smec 	mutex_exit(vphm);
7683253Smec 
7693253Smec 	ksp->ks_ndata = count;
7703253Smec 	ksp->ks_data_size = count * 2 * sizeof (uint64_t);
7713253Smec 
7723253Smec 	return (0);
7733253Smec }
7743253Smec 
7753253Smec /*
7763253Smec  * all spans will be pagesize and no coalescing will be done with the
7773253Smec  * list produced.
7783253Smec  */
7793253Smec static int
7803253Smec pr_list_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
7813253Smec {
7823253Smec 	kmutex_t *vphm;
7833253Smec 	page_t *pp;
7843253Smec 	struct memunit {
7853253Smec 		uint64_t address;
7863253Smec 		uint64_t size;
7873253Smec 	} *kspmem;
7883253Smec 
7893253Smec 	if (rw == KSTAT_WRITE)
7903253Smec 		return (EACCES);
7913253Smec 
7923253Smec 	ksp->ks_snaptime = gethrtime();
7933253Smec 
7943253Smec 	kspmem = (struct memunit *)buf;
7953253Smec 
7963253Smec 	vphm = page_vnode_mutex(retired_pages);
7973253Smec 	mutex_enter(vphm);
7983253Smec 	pp = retired_pages->v_pages;
7993253Smec 	if (((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size) ||
8003253Smec 	    (pp == NULL)) {
8013253Smec 		mutex_exit(vphm);
8023253Smec 		return (0);
8033253Smec 	}
8043253Smec 	kspmem->address = ptob(pp->p_pagenum);
8053253Smec 	kspmem->size = PAGESIZE;
8063253Smec 	kspmem++;
8073253Smec 	for (pp = pp->p_vpnext; pp != retired_pages->v_pages;
8083253Smec 	    pp = pp->p_vpnext, kspmem++) {
8093253Smec 		if ((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size)
8103253Smec 			break;
8113253Smec 		kspmem->address = ptob(pp->p_pagenum);
8123253Smec 		kspmem->size = PAGESIZE;
8133253Smec 	}
8143253Smec 	mutex_exit(vphm);
8153253Smec 
8163253Smec 	return (0);
8173253Smec }
8183253Smec 
819917Selowe /*
820*3480Sjfrank  * page_retire_pend_count -- helper function for page_capture_thread,
821*3480Sjfrank  * returns the number of pages pending retirement.
822*3480Sjfrank  */
823*3480Sjfrank uint64_t
824*3480Sjfrank page_retire_pend_count(void)
825*3480Sjfrank {
826*3480Sjfrank 	return (PR_KSTAT_PENDING);
827*3480Sjfrank }
828*3480Sjfrank 
829*3480Sjfrank void
830*3480Sjfrank page_retire_incr_pend_count(void)
831*3480Sjfrank {
832*3480Sjfrank 	PR_INCR_KSTAT(pr_pending);
833*3480Sjfrank }
834*3480Sjfrank 
835*3480Sjfrank void
836*3480Sjfrank page_retire_decr_pend_count(void)
837*3480Sjfrank {
838*3480Sjfrank 	PR_DECR_KSTAT(pr_pending);
839*3480Sjfrank }
840*3480Sjfrank 
841*3480Sjfrank /*
842917Selowe  * Initialize the page retire mechanism:
843917Selowe  *
844917Selowe  *   - Establish the correctable error retire limit.
845917Selowe  *   - Initialize locks.
846917Selowe  *   - Build the retired_pages vnode.
847917Selowe  *   - Set up the kstats.
848917Selowe  *   - Fire off the background thread.
8493253Smec  *   - Tell page_retire() it's OK to start retiring pages.
850917Selowe  */
851917Selowe void
852917Selowe page_retire_init(void)
853917Selowe {
854917Selowe 	const fs_operation_def_t retired_vnodeops_template[] = {NULL, NULL};
855917Selowe 	struct vnodeops *vops;
8563253Smec 	kstat_t *ksp;
857917Selowe 
858917Selowe 	const uint_t page_retire_ndata =
859917Selowe 	    sizeof (page_retire_kstat) / sizeof (kstat_named_t);
860917Selowe 
861917Selowe 	ASSERT(page_retire_ksp == NULL);
862917Selowe 
863917Selowe 	if (max_pages_retired_bps <= 0) {
864917Selowe 		max_pages_retired_bps = MCE_BPT;
865917Selowe 	}
866917Selowe 
867917Selowe 	mutex_init(&pr_q_mutex, NULL, MUTEX_DEFAULT, NULL);
868917Selowe 
869917Selowe 	retired_pages = vn_alloc(KM_SLEEP);
870917Selowe 	if (vn_make_ops("retired_pages", retired_vnodeops_template, &vops)) {
871917Selowe 		cmn_err(CE_PANIC,
872917Selowe 		    "page_retired_init: can't make retired vnodeops");
873917Selowe 	}
874917Selowe 	vn_setops(retired_pages, vops);
875917Selowe 
876917Selowe 	if ((page_retire_ksp = kstat_create("unix", 0, "page_retire",
877917Selowe 	    "misc", KSTAT_TYPE_NAMED, page_retire_ndata,
878917Selowe 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
879917Selowe 		cmn_err(CE_WARN, "kstat_create for page_retire failed");
880917Selowe 	} else {
881917Selowe 		page_retire_ksp->ks_data = (void *)&page_retire_kstat;
882917Selowe 		page_retire_ksp->ks_update = page_retire_kstat_update;
883917Selowe 		kstat_install(page_retire_ksp);
884917Selowe 	}
885917Selowe 
8863253Smec 	mutex_init(&pr_list_kstat_mutex, NULL, MUTEX_DEFAULT, NULL);
8873253Smec 	ksp = kstat_create("unix", 0, "page_retire_list", "misc",
8883253Smec 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_VIRTUAL);
8893253Smec 	if (ksp != NULL) {
8903253Smec 		ksp->ks_update = pr_list_kstat_update;
8913253Smec 		ksp->ks_snapshot = pr_list_kstat_snapshot;
8923253Smec 		ksp->ks_lock = &pr_list_kstat_mutex;
8933253Smec 		kstat_install(ksp);
8943253Smec 	}
895917Selowe 
8963253Smec 	page_capture_register_callback(PC_RETIRE, -1, page_retire_pp_finish);
897917Selowe 	pr_enable = 1;
898917Selowe }
899917Selowe 
900917Selowe /*
901917Selowe  * page_retire_hunt() callback for the retire thread.
902917Selowe  */
903917Selowe static void
904917Selowe page_retire_thread_cb(page_t *pp)
905917Selowe {
906917Selowe 	PR_DEBUG(prd_tctop);
9073290Sjohansen 	if (!PP_ISKAS(pp) && page_trylock(pp, SE_EXCL)) {
908917Selowe 		PR_DEBUG(prd_tclocked);
909917Selowe 		page_unlock(pp);
910917Selowe 	}
911917Selowe }
912917Selowe 
913917Selowe /*
914917Selowe  * page_retire_hunt() callback for mdboot().
915917Selowe  *
916917Selowe  * It is necessary to scrub any failing pages prior to reboot in order to
917917Selowe  * prevent a latent error trap from occurring on the next boot.
918917Selowe  */
919917Selowe void
920917Selowe page_retire_mdboot_cb(page_t *pp)
921917Selowe {
922917Selowe 	/*
923917Selowe 	 * Don't scrub the kernel, since we might still need it, unless
924917Selowe 	 * we have UEs on the page, in which case we have nothing to lose.
925917Selowe 	 */
9263290Sjohansen 	if (!PP_ISKAS(pp) || PP_TOXIC(pp)) {
927917Selowe 		pp->p_selock = -1;	/* pacify ASSERTs */
928973Selowe 		PP_CLRFREE(pp);
929917Selowe 		pagescrub(pp, 0, PAGESIZE);
930917Selowe 		pp->p_selock = 0;
931917Selowe 	}
932917Selowe 	pp->p_toxic = 0;
933917Selowe }
934917Selowe 
935917Selowe 
936917Selowe /*
9373253Smec  * Callback used by page_trycapture() to finish off retiring a page.
9383253Smec  * The page has already been cleaned and we've been given sole access to
9393253Smec  * it.
9403253Smec  * Always returns 0 to indicate that callback succeded as the callback never
9413253Smec  * fails to finish retiring the given page.
942917Selowe  */
9433253Smec /*ARGSUSED*/
944917Selowe static int
9453253Smec page_retire_pp_finish(page_t *pp, void *notused, uint_t flags)
946917Selowe {
947917Selowe 	int		toxic;
948917Selowe 
949917Selowe 	ASSERT(PAGE_EXCL(pp));
950917Selowe 	ASSERT(pp->p_iolock_state == 0);
951917Selowe 	ASSERT(pp->p_szc == 0);
952917Selowe 
953917Selowe 	toxic = pp->p_toxic;
954917Selowe 
955917Selowe 	/*
956917Selowe 	 * The problem page is locked, demoted, unmapped, not free,
957917Selowe 	 * hashed out, and not COW or mlocked (whew!).
958917Selowe 	 *
959917Selowe 	 * Now we select our ammunition, take it around back, and shoot it.
960917Selowe 	 */
961917Selowe 	if (toxic & PR_UE) {
9623253Smec ue_error:
963917Selowe 		if (page_retire_transient_ue(pp)) {
964917Selowe 			PR_DEBUG(prd_uescrubbed);
9653253Smec 			(void) page_retire_done(pp, PRD_UE_SCRUBBED);
966917Selowe 		} else {
967917Selowe 			PR_DEBUG(prd_uenotscrubbed);
968917Selowe 			page_retire_destroy(pp);
9693253Smec 			(void) page_retire_done(pp, PRD_SUCCESS);
970917Selowe 		}
9713253Smec 		return (0);
972917Selowe 	} else if (toxic & PR_FMA) {
973917Selowe 		PR_DEBUG(prd_fma);
974917Selowe 		page_retire_destroy(pp);
9753253Smec 		(void) page_retire_done(pp, PRD_SUCCESS);
9763253Smec 		return (0);
977917Selowe 	} else if (toxic & PR_MCE) {
978917Selowe 		PR_DEBUG(prd_mce);
979917Selowe 		page_retire_destroy(pp);
9803253Smec 		(void) page_retire_done(pp, PRD_SUCCESS);
9813253Smec 		return (0);
982917Selowe 	}
983917Selowe 
984917Selowe 	/*
9853253Smec 	 * When page_retire_first_ue is set to zero and a UE occurs which is
9863253Smec 	 * transient, it's possible that we clear some flags set by a second
9873253Smec 	 * UE error on the page which occurs while the first is currently being
9883253Smec 	 * handled and thus we need to handle the case where none of the above
9893253Smec 	 * are set.  In this instance, PR_UE_SCRUBBED should be set and thus
9903253Smec 	 * we should execute the UE code above.
991917Selowe 	 */
9923253Smec 	if (toxic & PR_UE_SCRUBBED) {
9933253Smec 		goto ue_error;
994917Selowe 	}
9953253Smec 
9963253Smec 	/*
9973253Smec 	 * It's impossible to get here.
9983253Smec 	 */
9993253Smec 	panic("bad toxic flags 0x%x in page_retire_pp_finish\n", toxic);
10003253Smec 	return (0);
1001917Selowe }
1002917Selowe 
1003917Selowe /*
1004917Selowe  * page_retire() - the front door in to retire a page.
1005917Selowe  *
1006917Selowe  * Ideally, page_retire() would instantly retire the requested page.
1007917Selowe  * Unfortunately, some pages are locked or otherwise tied up and cannot be
10083253Smec  * retired right away.  We use the page capture logic to deal with this
10093253Smec  * situation as it will continuously try to retire the page in the background
10103253Smec  * if the first attempt fails.  Success is determined by looking to see whether
10113253Smec  * the page has been retired after the page_trycapture() attempt.
1012917Selowe  *
1013917Selowe  * Returns:
1014917Selowe  *
1015917Selowe  *   - 0 on success,
1016917Selowe  *   - EINVAL when the PA is whacko,
10171381Selowe  *   - EIO if the page is already retired or already pending retirement, or
10181381Selowe  *   - EAGAIN if the page could not be _immediately_ retired but is pending.
1019917Selowe  */
1020917Selowe int
1021917Selowe page_retire(uint64_t pa, uchar_t reason)
1022917Selowe {
1023917Selowe 	page_t	*pp;
1024917Selowe 
1025917Selowe 	ASSERT(reason & PR_REASONS);		/* there must be a reason */
1026917Selowe 	ASSERT(!(reason & ~PR_REASONS));	/* but no other bits */
1027917Selowe 
1028917Selowe 	pp = page_numtopp_nolock(mmu_btop(pa));
1029917Selowe 	if (pp == NULL) {
1030917Selowe 		PR_MESSAGE(CE_WARN, 1, "Cannot schedule clearing of error on"
1031917Selowe 		    " page 0x%08x.%08x; page is not relocatable memory", pa);
1032917Selowe 		return (page_retire_done(pp, PRD_INVALID_PA));
1033917Selowe 	}
1034917Selowe 	if (PP_RETIRED(pp)) {
10351381Selowe 		PR_DEBUG(prd_dup1);
1036917Selowe 		return (page_retire_done(pp, PRD_DUPLICATE));
1037917Selowe 	}
1038917Selowe 
10391381Selowe 	if ((reason & PR_UE) && !PP_TOXIC(pp)) {
1040917Selowe 		PR_MESSAGE(CE_NOTE, 1, "Scheduling clearing of error on"
1041917Selowe 		    " page 0x%08x.%08x", pa);
10421381Selowe 	} else if (PP_PR_REQ(pp)) {
10431381Selowe 		PR_DEBUG(prd_dup2);
10441381Selowe 		return (page_retire_done(pp, PRD_DUPLICATE));
1045917Selowe 	} else {
1046917Selowe 		PR_MESSAGE(CE_NOTE, 1, "Scheduling removal of"
1047917Selowe 		    " page 0x%08x.%08x", pa);
1048917Selowe 	}
10493253Smec 
10503253Smec 	/* Avoid setting toxic bits in the first place */
10513253Smec 	if ((reason & (PR_FMA | PR_MCE)) && !(reason & PR_UE) &&
10523253Smec 	    page_retire_limit()) {
10533253Smec 		return (page_retire_done(pp, PRD_LIMIT));
10543253Smec 	}
1055917Selowe 
10563253Smec 	if (MTBF(pr_calls, pr_mtbf)) {
10573253Smec 		page_settoxic(pp, reason);
10583253Smec 		if (page_trycapture(pp, 0, CAPTURE_RETIRE, NULL) == 0) {
10593253Smec 			PR_DEBUG(prd_prlocked);
10603253Smec 		} else {
10613253Smec 			PR_DEBUG(prd_prnotlocked);
10623253Smec 		}
1063917Selowe 	} else {
1064917Selowe 		PR_DEBUG(prd_prnotlocked);
1065917Selowe 	}
1066917Selowe 
1067917Selowe 	if (PP_RETIRED(pp)) {
1068917Selowe 		PR_DEBUG(prd_prretired);
1069917Selowe 		return (0);
1070917Selowe 	} else {
10713253Smec 		cv_signal(&pc_cv);
1072917Selowe 		PR_INCR_KSTAT(pr_failed);
1073917Selowe 
1074917Selowe 		if (pp->p_toxic & PR_MSG) {
1075917Selowe 			return (page_retire_done(pp, PRD_FAILED));
1076917Selowe 		} else {
1077917Selowe 			return (page_retire_done(pp, PRD_PENDING));
1078917Selowe 		}
1079917Selowe 	}
1080917Selowe }
1081917Selowe 
1082917Selowe /*
1083917Selowe  * Take a retired page off the retired-pages vnode and clear the toxic flags.
1084917Selowe  * If "free" is nonzero, lock it and put it back on the freelist. If "free"
1085917Selowe  * is zero, the caller already holds SE_EXCL lock so we simply unretire it
1086917Selowe  * and don't do anything else with it.
1087917Selowe  *
1088917Selowe  * Any unretire messages are printed from this routine.
1089917Selowe  *
1090917Selowe  * Returns 0 if page pp was unretired; else an error code.
10913253Smec  *
10923253Smec  * If flags is:
10933253Smec  *	PR_UNR_FREE - lock the page, clear the toxic flags and free it
10943253Smec  *	    to the freelist.
10953253Smec  *	PR_UNR_TEMP - lock the page, unretire it, leave the toxic
10963253Smec  *	    bits set as is and return it to the caller.
10973253Smec  *	PR_UNR_CLEAN - page is SE_EXCL locked, unretire it, clear the
10983253Smec  *	    toxic flags and return it to caller as is.
1099917Selowe  */
1100917Selowe int
11013253Smec page_unretire_pp(page_t *pp, int flags)
1102917Selowe {
1103917Selowe 	/*
1104917Selowe 	 * To be retired, a page has to be hashed onto the retired_pages vnode
1105917Selowe 	 * and have PR_RETIRED set in p_toxic.
1106917Selowe 	 */
11073253Smec 	if (flags == PR_UNR_CLEAN ||
11083253Smec 	    page_try_reclaim_lock(pp, SE_EXCL, SE_RETIRED)) {
1109917Selowe 		ASSERT(PAGE_EXCL(pp));
1110917Selowe 		PR_DEBUG(prd_ulocked);
1111917Selowe 		if (!PP_RETIRED(pp)) {
1112917Selowe 			PR_DEBUG(prd_unotretired);
1113917Selowe 			page_unlock(pp);
1114917Selowe 			return (page_retire_done(pp, PRD_UNR_NOT));
1115917Selowe 		}
1116917Selowe 
1117917Selowe 		PR_MESSAGE(CE_NOTE, 1, "unretiring retired"
11181338Selowe 		    " page 0x%08x.%08x", mmu_ptob((uint64_t)pp->p_pagenum));
1119917Selowe 		if (pp->p_toxic & PR_FMA) {
1120917Selowe 			PR_DECR_KSTAT(pr_fma);
1121917Selowe 		} else if (pp->p_toxic & PR_UE) {
1122917Selowe 			PR_DECR_KSTAT(pr_ue);
1123917Selowe 		} else {
1124917Selowe 			PR_DECR_KSTAT(pr_mce);
1125917Selowe 		}
1126917Selowe 
11273253Smec 		if (flags == PR_UNR_TEMP)
11283253Smec 			page_clrtoxic(pp, PR_RETIRED);
11293253Smec 		else
11303253Smec 			page_clrtoxic(pp, PR_TOXICFLAGS);
11313253Smec 
11323253Smec 		if (flags == PR_UNR_FREE) {
1133917Selowe 			PR_DEBUG(prd_udestroy);
1134917Selowe 			page_destroy(pp, 0);
1135917Selowe 		} else {
1136917Selowe 			PR_DEBUG(prd_uhashout);
1137917Selowe 			page_hashout(pp, NULL);
1138917Selowe 		}
1139917Selowe 
1140917Selowe 		mutex_enter(&freemem_lock);
1141917Selowe 		availrmem++;
1142917Selowe 		mutex_exit(&freemem_lock);
1143917Selowe 
1144917Selowe 		PR_DEBUG(prd_uunretired);
1145917Selowe 		PR_DECR_KSTAT(pr_retired);
1146917Selowe 		PR_INCR_KSTAT(pr_unretired);
1147917Selowe 		return (page_retire_done(pp, PRD_UNR_SUCCESS));
1148917Selowe 	}
1149917Selowe 	PR_DEBUG(prd_unotlocked);
1150917Selowe 	return (page_retire_done(pp, PRD_UNR_CANTLOCK));
1151917Selowe }
1152917Selowe 
1153917Selowe /*
1154917Selowe  * Return a page to service by moving it from the retired_pages vnode
1155917Selowe  * onto the freelist.
1156917Selowe  *
1157917Selowe  * Called from mmioctl_page_retire() on behalf of the FMA DE.
1158917Selowe  *
1159917Selowe  * Returns:
1160917Selowe  *
1161917Selowe  *   - 0 if the page is unretired,
1162917Selowe  *   - EAGAIN if the pp can not be locked,
1163917Selowe  *   - EINVAL if the PA is whacko, and
11641381Selowe  *   - EIO if the pp is not retired.
1165917Selowe  */
1166917Selowe int
1167917Selowe page_unretire(uint64_t pa)
1168917Selowe {
1169917Selowe 	page_t	*pp;
1170917Selowe 
1171917Selowe 	pp = page_numtopp_nolock(mmu_btop(pa));
1172917Selowe 	if (pp == NULL) {
1173917Selowe 		return (page_retire_done(pp, PRD_INVALID_PA));
1174917Selowe 	}
1175917Selowe 
11763253Smec 	return (page_unretire_pp(pp, PR_UNR_FREE));
1177917Selowe }
1178917Selowe 
1179917Selowe /*
1180917Selowe  * Test a page to see if it is retired. If errors is non-NULL, the toxic
1181917Selowe  * bits of the page are returned. Returns 0 on success, error code on failure.
1182917Selowe  */
1183917Selowe int
1184917Selowe page_retire_check_pp(page_t *pp, uint64_t *errors)
1185917Selowe {
1186917Selowe 	int rc;
1187917Selowe 
1188917Selowe 	if (PP_RETIRED(pp)) {
1189917Selowe 		PR_DEBUG(prd_checkhit);
1190917Selowe 		rc = 0;
11911381Selowe 	} else if (PP_PR_REQ(pp)) {
11921381Selowe 		PR_DEBUG(prd_checkmiss_pend);
11931381Selowe 		rc = EAGAIN;
1194917Selowe 	} else {
11951381Selowe 		PR_DEBUG(prd_checkmiss_noerr);
11961381Selowe 		rc = EIO;
1197917Selowe 	}
1198917Selowe 
1199917Selowe 	/*
1200917Selowe 	 * We have magically arranged the bit values returned to fmd(1M)
1201917Selowe 	 * to line up with the FMA, MCE, and UE bits of the page_t.
1202917Selowe 	 */
1203917Selowe 	if (errors) {
1204917Selowe 		uint64_t toxic = (uint64_t)(pp->p_toxic & PR_ERRMASK);
1205917Selowe 		if (toxic & PR_UE_SCRUBBED) {
1206917Selowe 			toxic &= ~PR_UE_SCRUBBED;
1207917Selowe 			toxic |= PR_UE;
1208917Selowe 		}
1209917Selowe 		*errors = toxic;
1210917Selowe 	}
1211917Selowe 
1212917Selowe 	return (rc);
1213917Selowe }
1214917Selowe 
1215917Selowe /*
1216917Selowe  * Test to see if the page_t for a given PA is retired, and return the
1217917Selowe  * hardware errors we have seen on the page if requested.
1218917Selowe  *
1219917Selowe  * Called from mmioctl_page_retire on behalf of the FMA DE.
1220917Selowe  *
1221917Selowe  * Returns:
1222917Selowe  *
1223917Selowe  *   - 0 if the page is retired,
12241381Selowe  *   - EIO if the page is not retired and has no errors,
12251381Selowe  *   - EAGAIN if the page is not retired but is pending; and
1226917Selowe  *   - EINVAL if the PA is whacko.
1227917Selowe  */
1228917Selowe int
1229917Selowe page_retire_check(uint64_t pa, uint64_t *errors)
1230917Selowe {
1231917Selowe 	page_t	*pp;
1232917Selowe 
1233917Selowe 	if (errors) {
1234917Selowe 		*errors = 0;
1235917Selowe 	}
1236917Selowe 
1237917Selowe 	pp = page_numtopp_nolock(mmu_btop(pa));
1238917Selowe 	if (pp == NULL) {
1239917Selowe 		return (page_retire_done(pp, PRD_INVALID_PA));
1240917Selowe 	}
1241917Selowe 
1242917Selowe 	return (page_retire_check_pp(pp, errors));
1243917Selowe }
1244917Selowe 
1245917Selowe /*
1246917Selowe  * Page retire self-test. For now, it always returns 0.
1247917Selowe  */
1248917Selowe int
1249917Selowe page_retire_test(void)
1250917Selowe {
1251917Selowe 	page_t *first, *pp, *cpp, *cpp2, *lpp;
1252917Selowe 
1253917Selowe 	/*
1254917Selowe 	 * Tests the corner case where a large page can't be retired
1255917Selowe 	 * because one of the constituent pages is locked. We mark
1256917Selowe 	 * one page to be retired and try to retire it, and mark the
1257917Selowe 	 * other page to be retired but don't try to retire it, so
1258917Selowe 	 * that page_unlock() in the failure path will recurse and try
1259917Selowe 	 * to retire THAT page. This is the worst possible situation
1260917Selowe 	 * we can get ourselves into.
1261917Selowe 	 */
1262917Selowe 	memsegs_lock(0);
1263917Selowe 	pp = first = page_first();
1264917Selowe 	do {
1265917Selowe 		if (pp->p_szc && PP_PAGEROOT(pp) == pp) {
1266917Selowe 			cpp = pp + 1;
1267917Selowe 			lpp = PP_ISFREE(pp)? pp : pp + 2;
1268917Selowe 			cpp2 = pp + 3;
1269917Selowe 			if (!page_trylock(lpp, pp == lpp? SE_EXCL : SE_SHARED))
1270917Selowe 				continue;
1271917Selowe 			if (!page_trylock(cpp, SE_EXCL)) {
1272917Selowe 				page_unlock(lpp);
1273917Selowe 				continue;
1274917Selowe 			}
12753253Smec 
12763253Smec 			/* fails */
12773253Smec 			(void) page_retire(ptob(cpp->p_pagenum), PR_FMA);
12783253Smec 
1279917Selowe 			page_unlock(lpp);
12803253Smec 			page_unlock(cpp);
12813253Smec 			(void) page_retire(ptob(cpp->p_pagenum), PR_FMA);
12823253Smec 			(void) page_retire(ptob(cpp2->p_pagenum), PR_FMA);
1283917Selowe 		}
1284917Selowe 	} while ((pp = page_next(pp)) != first);
1285917Selowe 	memsegs_unlock(0);
1286917Selowe 
1287917Selowe 	return (0);
1288917Selowe }
1289