xref: /onnv-gate/usr/src/lib/efcode/engine/mcookie.c (revision 3423:fd25d153620d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3423Sbm42561  * Common Development and Distribution License (the "License").
6*3423Sbm42561  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3423Sbm42561  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <fcode/private.h>
340Sstevel@tonic-gate #include <fcode/log.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
370Sstevel@tonic-gate 
38*3423Sbm42561 #define	MAX_MAPS	256
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	MAP_IS_VALID		0x01
410Sstevel@tonic-gate 
420Sstevel@tonic-gate struct map_table {
430Sstevel@tonic-gate 	int map_flags;
440Sstevel@tonic-gate 	uint64_t map_add;
450Sstevel@tonic-gate 	size_t map_size;
460Sstevel@tonic-gate 	uint64_t adj_virt;
470Sstevel@tonic-gate 	size_t adj_length;
480Sstevel@tonic-gate } map_table[MAX_MAPS];
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Originally, this code translated kernel supplied virtual addresses into
520Sstevel@tonic-gate  * "memory cookies", which was a 32-bit number with ascii-M in the upper 8
530Sstevel@tonic-gate  * bits, a 4-bit index and a 20-bit offset.  However, this caused two
540Sstevel@tonic-gate  * problems:  1) the 20-bit offset was too small for some devices, esp. some
550Sstevel@tonic-gate  * with frame-buffers;  2) if the fcode used the cookie to program the
560Sstevel@tonic-gate  * hardware, there was no easy way for the software to detect that a
570Sstevel@tonic-gate  * translation needed to be done.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * For that reason, "memory cookies" are now just the kernel-supplied
600Sstevel@tonic-gate  * virtual address, and we now check each memory access to see if it's
610Sstevel@tonic-gate  * attempting to access kernel-supplied memory.  The only important thing
620Sstevel@tonic-gate  * now is that "is_mcookie" returns 1 (or true) if the tested mcookie
630Sstevel@tonic-gate  * is a kernel virtual address.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * There is a potential bug if the kernel virtual address happens to
660Sstevel@tonic-gate  * conflict with a user virtual address.  However, the current implementation
670Sstevel@tonic-gate  * of Solaris avoids this conflict.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate fstack_t
mapping_to_mcookie(uint64_t req_add,size_t req_size,uint64_t adj_virt,size_t adj_length)710Sstevel@tonic-gate mapping_to_mcookie(uint64_t req_add, size_t req_size, uint64_t adj_virt,
720Sstevel@tonic-gate     size_t adj_length)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	int i;
750Sstevel@tonic-gate 	struct map_table *mp;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
780Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) == 0)
790Sstevel@tonic-gate 			break;
800Sstevel@tonic-gate 	if (i == MAX_MAPS) {
810Sstevel@tonic-gate 		log_message(MSG_WARN, "Warning: too many mappings\n");
820Sstevel@tonic-gate 		return (0);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	debug_msg(DEBUG_REG_ACCESS, "Allocating mapping: %d add: 0x%llx"
850Sstevel@tonic-gate 	    " size: 0x%x\n", i, req_add, req_size);
860Sstevel@tonic-gate 	mp->map_flags |= MAP_IS_VALID;
870Sstevel@tonic-gate 	mp->map_add = req_add;
880Sstevel@tonic-gate 	mp->map_size = req_size;
890Sstevel@tonic-gate 	mp->adj_virt = adj_virt;
900Sstevel@tonic-gate 	mp->adj_length = adj_length;
910Sstevel@tonic-gate 	if (mp->adj_length != 0)
920Sstevel@tonic-gate 		return (adj_virt);
930Sstevel@tonic-gate 	else
940Sstevel@tonic-gate 		return (req_add);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate void
delete_mapping(fstack_t mcookie)980Sstevel@tonic-gate delete_mapping(fstack_t mcookie)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	int i;
1010Sstevel@tonic-gate 	struct map_table *mp;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
1040Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1050Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1060Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
1070Sstevel@tonic-gate 			debug_msg(DEBUG_REG_ACCESS, "Deallocating mapping: %d"
1080Sstevel@tonic-gate 			    " add: 0x%llx size: 0x%x\n", i, mp->map_add,
1090Sstevel@tonic-gate 			    mp->map_size);
1100Sstevel@tonic-gate 			mp->map_flags &= ~MAP_IS_VALID;
1110Sstevel@tonic-gate 			mp->map_add = 0;
1120Sstevel@tonic-gate 			mp->map_size = 0;
1130Sstevel@tonic-gate 			mp->adj_virt = 0;
1140Sstevel@tonic-gate 			mp->adj_length = 0;
1150Sstevel@tonic-gate 			return;
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: delete_mapping: invalid"
1190Sstevel@tonic-gate 		" mcookie: %llx\n", (uint64_t)mcookie);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate int
is_mcookie(fstack_t mcookie)1230Sstevel@tonic-gate is_mcookie(fstack_t mcookie)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	struct map_table *mp;
1260Sstevel@tonic-gate 	int i;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
1290Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1300Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1310Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size)
1320Sstevel@tonic-gate 			return (1);
1330Sstevel@tonic-gate 	return (0);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate uint64_t
mcookie_to_addr(fstack_t mcookie)1370Sstevel@tonic-gate mcookie_to_addr(fstack_t mcookie)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	return (mcookie);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate fstack_t
mcookie_to_rlen(fstack_t mcookie)1430Sstevel@tonic-gate mcookie_to_rlen(fstack_t mcookie)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	int i;
1460Sstevel@tonic-gate 	struct map_table *mp;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
1490Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1500Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1510Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
1520Sstevel@tonic-gate 			return (mp->map_size);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: mcookie_to_rlen: invalid"
1560Sstevel@tonic-gate 	    " mcookie: %llx\n", (uint64_t)mcookie);
157796Smathue 
158796Smathue 	return (0);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate fstack_t
mcookie_to_rvirt(fstack_t mcookie)1620Sstevel@tonic-gate mcookie_to_rvirt(fstack_t mcookie)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	int i;
1650Sstevel@tonic-gate 	struct map_table *mp;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
1680Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1690Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1700Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
1710Sstevel@tonic-gate 			return (mp->map_add);
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: mcookie_to_rvirt: invalid"
1750Sstevel@tonic-gate 	    " mcookie: %llx\n", (uint64_t)mcookie);
176796Smathue 
177796Smathue 	return (0);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static void
dot_maps(fcode_env_t * env)1810Sstevel@tonic-gate dot_maps(fcode_env_t *env)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	int i;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	log_message(MSG_DEBUG, "idx     base-addr        size\n");
1860Sstevel@tonic-gate 	for (i = 0; i < MAX_MAPS; i++) {
1870Sstevel@tonic-gate 		if (map_table[i].map_flags & MAP_IS_VALID)
1880Sstevel@tonic-gate 			log_message(MSG_DEBUG, "%3d %016llx %8x\n", i,
1890Sstevel@tonic-gate 			    map_table[i].map_add, map_table[i].map_size);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static void
map_qmark(fcode_env_t * env)1940Sstevel@tonic-gate map_qmark(fcode_env_t *env)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	fstack_t d = POP(DS);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (!is_mcookie(d))
1990Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx: not mcookie\n", (uint64_t)d);
2000Sstevel@tonic-gate 	else
2010Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx -> %llx\n", (uint64_t)d,
2020Sstevel@tonic-gate 		    mcookie_to_addr(d));
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static void
add_map(fcode_env_t * env)2060Sstevel@tonic-gate add_map(fcode_env_t *env)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	fstack_t size, addr;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	size = POP(DS);
2110Sstevel@tonic-gate 	addr = POP(DS);
2120Sstevel@tonic-gate 	addr = mapping_to_mcookie(addr, size, NULL, NULL);
2130Sstevel@tonic-gate 	PUSH(DS, addr);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate static void
del_map(fcode_env_t * env)2170Sstevel@tonic-gate del_map(fcode_env_t *env)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	fstack_t addr;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	addr = POP(DS);
2220Sstevel@tonic-gate 	delete_mapping(addr);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate #pragma init(_init)
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static void
_init(void)2290Sstevel@tonic-gate _init(void)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	ASSERT(env);
2340Sstevel@tonic-gate 	NOTICE;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	FORTH(0,	".maps",		dot_maps);
2370Sstevel@tonic-gate 	FORTH(0,	"map?",			map_qmark);
2380Sstevel@tonic-gate 	FORTH(0,	"add-map",		add_map);
2390Sstevel@tonic-gate 	FORTH(0,	"del-map",		del_map);
2400Sstevel@tonic-gate }
241