1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#include <sys/param.h>
30#include <sys/kmem.h>
31#include <sys/sysmacros.h>
32#include <sys/cmn_err.h>
33#include <sys/systm.h>
34#include <sys/modctl.h>
35#include <sys/kobj.h>
36#include <vm/hat.h>
37
38/*
39 * PSARC 2004/405 made hat_getkpfnum(9F) obsolete. As part of the
40 * obsolecense, the original documented behavior will begin to be
41 * enforced in the future; namely, hat_getkpfnum(9F) may _only_
42 * be called with device-mapped memory virtual addresses. Since
43 * changing hat_getkpfnum(9F) to return PFN_INVALID on kernel memory
44 * would break a lot of modules without any warning, we've implemented
45 * the following mechanism as a stop-gap. In a future release, this
46 * can all be ripped out and hat_getkpfnum(9F) changed to return
47 * PFN_INVALID if it isn't called with a device-mapped memory address.
48 *
49 * We keep track of each module that has used hat_getkpfnum(9F)
50 * incorrectly. This allows us to avoid flooding the console/logs
51 * with too many warnings about a bad module that has already been
52 * flagged.
53 *
54 * On amd64 hat_getkpfnum() is never supported.
55 */
56
57#if !defined(__amd64)
58
59#define	HAT_STACK_MAXDEPTH	15
60
61struct badcall_node {
62	char	*bc_modname;
63	int	bc_stackdepth;
64	pc_t	bc_callstack[HAT_STACK_MAXDEPTH];
65	struct badcall_node *bc_linkage;
66};
67
68static struct badcall_node *bad_getkpfnum_callers;
69
70/*
71 * Common VM HAT routines.
72 */
73
74static void
75printwarn(struct badcall_node *bc)
76{
77	int sf;
78	char *ksym;
79	ulong_t off;
80
81	cmn_err(CE_WARN, "Module %s is using the obsolete hat_getkpfnum(9F)",
82	    bc->bc_modname);
83	cmn_err(CE_CONT, "interface in a way that will not be supported in\n");
84	cmn_err(CE_CONT, "a future release of Solaris. Please contact the\n");
85	cmn_err(CE_CONT, "vendor that supplied the module for assistance,\n");
86	cmn_err(CE_CONT, "or consult the Writing Device Drivers guide,\n");
87	cmn_err(CE_CONT, "available from http://www.sun.com for migration\n");
88	cmn_err(CE_CONT, "advice.\n");
89	cmn_err(CE_CONT, "---\n");
90	cmn_err(CE_CONT, "Callstack of bad caller:\n");
91
92	for (sf = 0; sf < bc->bc_stackdepth; sf++) {
93		ksym = kobj_getsymname(bc->bc_callstack[sf], &off);
94		cmn_err(CE_CONT, "\t%s+%lx\n", ksym? ksym : "?", off);
95	}
96}
97
98
99void
100hat_getkpfnum_badcall(void *caller)
101{
102	struct badcall_node bcs;
103	char *modname = mod_containing_pc((caddr_t)caller);
104	struct badcall_node *bc;
105
106#ifdef	__sparc
107	/*
108	 * This is a hack until the ifb and jfb framebuffer drivers
109	 * are fixed. Right now they use hat_getkpfnum() in a way that
110	 * is really safe but will be incorrectly flagged as being
111	 * buggy.
112	 */
113	if (strcmp(modname, "ifb") == 0 || strcmp(modname, "jfb") == 0)
114		return;
115#elif defined(__i386)
116	/*
117	 * This is a hack until these ethernet drivers can be fixed
118	 * or EOL'd.  hat_getkpfnum() will continue to work correctly
119	 * until this list can be removed.
120	 */
121	if (strcmp(modname, "dnet") == 0 || strcmp(modname, "pcn") == 0 ||
122	    strcmp(modname, "adp") == 0)
123		return;
124#endif	/* __sparc / __i386 */
125
126	for (bc = bad_getkpfnum_callers; bc != NULL; bc = bc->bc_linkage)
127		if (strcmp(bc->bc_modname, modname) == 0)
128			return;
129
130	/*
131	 * We haven't seen this caller before, so create a log of
132	 * the callstack and module name, and emit a warning to the
133	 * user.
134	 */
135	bc = kmem_zalloc(sizeof (struct badcall_node), KM_NOSLEEP);
136	if (bc != NULL) {
137		bc->bc_linkage = bad_getkpfnum_callers;
138		bc->bc_modname = modname;
139		bad_getkpfnum_callers = bc;
140	} else {
141		bc = &bcs;
142		bc->bc_modname = modname;
143	}
144
145	bc->bc_stackdepth = getpcstack(bc->bc_callstack, HAT_STACK_MAXDEPTH);
146
147	printwarn(bc);
148}
149#endif /* __amd64 */
150