xref: /onnv-gate/usr/src/lib/efcode/engine/mcookie.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2000-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <stdarg.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <fcode/private.h>
35*0Sstevel@tonic-gate #include <fcode/log.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #define	MAX_MAPS	16
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #define	MAP_IS_VALID		0x01
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate struct map_table {
44*0Sstevel@tonic-gate 	int map_flags;
45*0Sstevel@tonic-gate 	uint64_t map_add;
46*0Sstevel@tonic-gate 	size_t map_size;
47*0Sstevel@tonic-gate 	uint64_t adj_virt;
48*0Sstevel@tonic-gate 	size_t adj_length;
49*0Sstevel@tonic-gate } map_table[MAX_MAPS];
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * Originally, this code translated kernel supplied virtual addresses into
53*0Sstevel@tonic-gate  * "memory cookies", which was a 32-bit number with ascii-M in the upper 8
54*0Sstevel@tonic-gate  * bits, a 4-bit index and a 20-bit offset.  However, this caused two
55*0Sstevel@tonic-gate  * problems:  1) the 20-bit offset was too small for some devices, esp. some
56*0Sstevel@tonic-gate  * with frame-buffers;  2) if the fcode used the cookie to program the
57*0Sstevel@tonic-gate  * hardware, there was no easy way for the software to detect that a
58*0Sstevel@tonic-gate  * translation needed to be done.
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  * For that reason, "memory cookies" are now just the kernel-supplied
61*0Sstevel@tonic-gate  * virtual address, and we now check each memory access to see if it's
62*0Sstevel@tonic-gate  * attempting to access kernel-supplied memory.  The only important thing
63*0Sstevel@tonic-gate  * now is that "is_mcookie" returns 1 (or true) if the tested mcookie
64*0Sstevel@tonic-gate  * is a kernel virtual address.
65*0Sstevel@tonic-gate  *
66*0Sstevel@tonic-gate  * There is a potential bug if the kernel virtual address happens to
67*0Sstevel@tonic-gate  * conflict with a user virtual address.  However, the current implementation
68*0Sstevel@tonic-gate  * of Solaris avoids this conflict.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate fstack_t
72*0Sstevel@tonic-gate mapping_to_mcookie(uint64_t req_add, size_t req_size, uint64_t adj_virt,
73*0Sstevel@tonic-gate     size_t adj_length)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	int i;
76*0Sstevel@tonic-gate 	struct map_table *mp;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
79*0Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) == 0)
80*0Sstevel@tonic-gate 			break;
81*0Sstevel@tonic-gate 	if (i == MAX_MAPS) {
82*0Sstevel@tonic-gate 		log_message(MSG_WARN, "Warning: too many mappings\n");
83*0Sstevel@tonic-gate 		return (0);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	debug_msg(DEBUG_REG_ACCESS, "Allocating mapping: %d add: 0x%llx"
86*0Sstevel@tonic-gate 	    " size: 0x%x\n", i, req_add, req_size);
87*0Sstevel@tonic-gate 	mp->map_flags |= MAP_IS_VALID;
88*0Sstevel@tonic-gate 	mp->map_add = req_add;
89*0Sstevel@tonic-gate 	mp->map_size = req_size;
90*0Sstevel@tonic-gate 	mp->adj_virt = adj_virt;
91*0Sstevel@tonic-gate 	mp->adj_length = adj_length;
92*0Sstevel@tonic-gate 	if (mp->adj_length != 0)
93*0Sstevel@tonic-gate 		return (adj_virt);
94*0Sstevel@tonic-gate 	else
95*0Sstevel@tonic-gate 		return (req_add);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate void
99*0Sstevel@tonic-gate delete_mapping(fstack_t mcookie)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	int i;
102*0Sstevel@tonic-gate 	struct map_table *mp;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
105*0Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
106*0Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
107*0Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
108*0Sstevel@tonic-gate 			debug_msg(DEBUG_REG_ACCESS, "Deallocating mapping: %d"
109*0Sstevel@tonic-gate 			    " add: 0x%llx size: 0x%x\n", i, mp->map_add,
110*0Sstevel@tonic-gate 			    mp->map_size);
111*0Sstevel@tonic-gate 			mp->map_flags &= ~MAP_IS_VALID;
112*0Sstevel@tonic-gate 			mp->map_add = 0;
113*0Sstevel@tonic-gate 			mp->map_size = 0;
114*0Sstevel@tonic-gate 			mp->adj_virt = 0;
115*0Sstevel@tonic-gate 			mp->adj_length = 0;
116*0Sstevel@tonic-gate 			return;
117*0Sstevel@tonic-gate 		}
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: delete_mapping: invalid"
120*0Sstevel@tonic-gate 		" mcookie: %llx\n", (uint64_t)mcookie);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate int
124*0Sstevel@tonic-gate is_mcookie(fstack_t mcookie)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	struct map_table *mp;
127*0Sstevel@tonic-gate 	int i;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
130*0Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
131*0Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
132*0Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size)
133*0Sstevel@tonic-gate 			return (1);
134*0Sstevel@tonic-gate 	return (0);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate uint64_t
138*0Sstevel@tonic-gate mcookie_to_addr(fstack_t mcookie)
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	return (mcookie);
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate fstack_t
144*0Sstevel@tonic-gate mcookie_to_rlen(fstack_t mcookie)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	int i;
147*0Sstevel@tonic-gate 	struct map_table *mp;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
150*0Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
151*0Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
152*0Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
153*0Sstevel@tonic-gate 			return (mp->map_size);
154*0Sstevel@tonic-gate 		}
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: mcookie_to_rlen: invalid"
157*0Sstevel@tonic-gate 	    " mcookie: %llx\n", (uint64_t)mcookie);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate fstack_t
161*0Sstevel@tonic-gate mcookie_to_rvirt(fstack_t mcookie)
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate 	int i;
164*0Sstevel@tonic-gate 	struct map_table *mp;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
167*0Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
168*0Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
169*0Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
170*0Sstevel@tonic-gate 			return (mp->map_add);
171*0Sstevel@tonic-gate 		}
172*0Sstevel@tonic-gate 	}
173*0Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: mcookie_to_rvirt: invalid"
174*0Sstevel@tonic-gate 	    " mcookie: %llx\n", (uint64_t)mcookie);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate static void
178*0Sstevel@tonic-gate dot_maps(fcode_env_t *env)
179*0Sstevel@tonic-gate {
180*0Sstevel@tonic-gate 	int i;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	log_message(MSG_DEBUG, "idx     base-addr        size\n");
183*0Sstevel@tonic-gate 	for (i = 0; i < MAX_MAPS; i++) {
184*0Sstevel@tonic-gate 		if (map_table[i].map_flags & MAP_IS_VALID)
185*0Sstevel@tonic-gate 			log_message(MSG_DEBUG, "%3d %016llx %8x\n", i,
186*0Sstevel@tonic-gate 			    map_table[i].map_add, map_table[i].map_size);
187*0Sstevel@tonic-gate 	}
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate static void
191*0Sstevel@tonic-gate map_qmark(fcode_env_t *env)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	fstack_t d = POP(DS);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if (!is_mcookie(d))
196*0Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx: not mcookie\n", (uint64_t)d);
197*0Sstevel@tonic-gate 	else
198*0Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx -> %llx\n", (uint64_t)d,
199*0Sstevel@tonic-gate 		    mcookie_to_addr(d));
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate static void
203*0Sstevel@tonic-gate add_map(fcode_env_t *env)
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	fstack_t size, addr;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	size = POP(DS);
208*0Sstevel@tonic-gate 	addr = POP(DS);
209*0Sstevel@tonic-gate 	addr = mapping_to_mcookie(addr, size, NULL, NULL);
210*0Sstevel@tonic-gate 	PUSH(DS, addr);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate static void
214*0Sstevel@tonic-gate del_map(fcode_env_t *env)
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 	fstack_t addr;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	addr = POP(DS);
219*0Sstevel@tonic-gate 	delete_mapping(addr);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate #pragma init(_init)
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate static void
226*0Sstevel@tonic-gate _init(void)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	ASSERT(env);
231*0Sstevel@tonic-gate 	NOTICE;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	FORTH(0,	".maps",		dot_maps);
234*0Sstevel@tonic-gate 	FORTH(0,	"map?",			map_qmark);
235*0Sstevel@tonic-gate 	FORTH(0,	"add-map",		add_map);
236*0Sstevel@tonic-gate 	FORTH(0,	"del-map",		del_map);
237*0Sstevel@tonic-gate }
238