xref: /titanic_44/usr/src/lib/efcode/engine/mcookie.c (revision f1a9a1f881e55d2d4a8ba89b25efea86686dc261)
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
5*f1a9a1f8Sbm42561  * Common Development and Distribution License (the "License").
6*f1a9a1f8Sbm42561  * 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*f1a9a1f8Sbm42561  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <fcode/private.h>
347c478bd9Sstevel@tonic-gate #include <fcode/log.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
377c478bd9Sstevel@tonic-gate 
38*f1a9a1f8Sbm42561 #define	MAX_MAPS	256
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	MAP_IS_VALID		0x01
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate struct map_table {
437c478bd9Sstevel@tonic-gate 	int map_flags;
447c478bd9Sstevel@tonic-gate 	uint64_t map_add;
457c478bd9Sstevel@tonic-gate 	size_t map_size;
467c478bd9Sstevel@tonic-gate 	uint64_t adj_virt;
477c478bd9Sstevel@tonic-gate 	size_t adj_length;
487c478bd9Sstevel@tonic-gate } map_table[MAX_MAPS];
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Originally, this code translated kernel supplied virtual addresses into
527c478bd9Sstevel@tonic-gate  * "memory cookies", which was a 32-bit number with ascii-M in the upper 8
537c478bd9Sstevel@tonic-gate  * bits, a 4-bit index and a 20-bit offset.  However, this caused two
547c478bd9Sstevel@tonic-gate  * problems:  1) the 20-bit offset was too small for some devices, esp. some
557c478bd9Sstevel@tonic-gate  * with frame-buffers;  2) if the fcode used the cookie to program the
567c478bd9Sstevel@tonic-gate  * hardware, there was no easy way for the software to detect that a
577c478bd9Sstevel@tonic-gate  * translation needed to be done.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * For that reason, "memory cookies" are now just the kernel-supplied
607c478bd9Sstevel@tonic-gate  * virtual address, and we now check each memory access to see if it's
617c478bd9Sstevel@tonic-gate  * attempting to access kernel-supplied memory.  The only important thing
627c478bd9Sstevel@tonic-gate  * now is that "is_mcookie" returns 1 (or true) if the tested mcookie
637c478bd9Sstevel@tonic-gate  * is a kernel virtual address.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * There is a potential bug if the kernel virtual address happens to
667c478bd9Sstevel@tonic-gate  * conflict with a user virtual address.  However, the current implementation
677c478bd9Sstevel@tonic-gate  * of Solaris avoids this conflict.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate fstack_t
mapping_to_mcookie(uint64_t req_add,size_t req_size,uint64_t adj_virt,size_t adj_length)717c478bd9Sstevel@tonic-gate mapping_to_mcookie(uint64_t req_add, size_t req_size, uint64_t adj_virt,
727c478bd9Sstevel@tonic-gate     size_t adj_length)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	int i;
757c478bd9Sstevel@tonic-gate 	struct map_table *mp;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
787c478bd9Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) == 0)
797c478bd9Sstevel@tonic-gate 			break;
807c478bd9Sstevel@tonic-gate 	if (i == MAX_MAPS) {
817c478bd9Sstevel@tonic-gate 		log_message(MSG_WARN, "Warning: too many mappings\n");
827c478bd9Sstevel@tonic-gate 		return (0);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_REG_ACCESS, "Allocating mapping: %d add: 0x%llx"
857c478bd9Sstevel@tonic-gate 	    " size: 0x%x\n", i, req_add, req_size);
867c478bd9Sstevel@tonic-gate 	mp->map_flags |= MAP_IS_VALID;
877c478bd9Sstevel@tonic-gate 	mp->map_add = req_add;
887c478bd9Sstevel@tonic-gate 	mp->map_size = req_size;
897c478bd9Sstevel@tonic-gate 	mp->adj_virt = adj_virt;
907c478bd9Sstevel@tonic-gate 	mp->adj_length = adj_length;
917c478bd9Sstevel@tonic-gate 	if (mp->adj_length != 0)
927c478bd9Sstevel@tonic-gate 		return (adj_virt);
937c478bd9Sstevel@tonic-gate 	else
947c478bd9Sstevel@tonic-gate 		return (req_add);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate void
delete_mapping(fstack_t mcookie)987c478bd9Sstevel@tonic-gate delete_mapping(fstack_t mcookie)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	int i;
1017c478bd9Sstevel@tonic-gate 	struct map_table *mp;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
1047c478bd9Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1057c478bd9Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1067c478bd9Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
1077c478bd9Sstevel@tonic-gate 			debug_msg(DEBUG_REG_ACCESS, "Deallocating mapping: %d"
1087c478bd9Sstevel@tonic-gate 			    " add: 0x%llx size: 0x%x\n", i, mp->map_add,
1097c478bd9Sstevel@tonic-gate 			    mp->map_size);
1107c478bd9Sstevel@tonic-gate 			mp->map_flags &= ~MAP_IS_VALID;
1117c478bd9Sstevel@tonic-gate 			mp->map_add = 0;
1127c478bd9Sstevel@tonic-gate 			mp->map_size = 0;
1137c478bd9Sstevel@tonic-gate 			mp->adj_virt = 0;
1147c478bd9Sstevel@tonic-gate 			mp->adj_length = 0;
1157c478bd9Sstevel@tonic-gate 			return;
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: delete_mapping: invalid"
1197c478bd9Sstevel@tonic-gate 		" mcookie: %llx\n", (uint64_t)mcookie);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int
is_mcookie(fstack_t mcookie)1237c478bd9Sstevel@tonic-gate is_mcookie(fstack_t mcookie)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	struct map_table *mp;
1267c478bd9Sstevel@tonic-gate 	int i;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
1297c478bd9Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1307c478bd9Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1317c478bd9Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size)
1327c478bd9Sstevel@tonic-gate 			return (1);
1337c478bd9Sstevel@tonic-gate 	return (0);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate uint64_t
mcookie_to_addr(fstack_t mcookie)1377c478bd9Sstevel@tonic-gate mcookie_to_addr(fstack_t mcookie)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	return (mcookie);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate fstack_t
mcookie_to_rlen(fstack_t mcookie)1437c478bd9Sstevel@tonic-gate mcookie_to_rlen(fstack_t mcookie)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	int i;
1467c478bd9Sstevel@tonic-gate 	struct map_table *mp;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
1497c478bd9Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1507c478bd9Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1517c478bd9Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
1527c478bd9Sstevel@tonic-gate 			return (mp->map_size);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: mcookie_to_rlen: invalid"
1567c478bd9Sstevel@tonic-gate 	    " mcookie: %llx\n", (uint64_t)mcookie);
157360e6f5eSmathue 
158360e6f5eSmathue 	return (0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate fstack_t
mcookie_to_rvirt(fstack_t mcookie)1627c478bd9Sstevel@tonic-gate mcookie_to_rvirt(fstack_t mcookie)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	int i;
1657c478bd9Sstevel@tonic-gate 	struct map_table *mp;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
1687c478bd9Sstevel@tonic-gate 		if ((mp->map_flags & MAP_IS_VALID) &&
1697c478bd9Sstevel@tonic-gate 		    mcookie >= mp->map_add &&
1707c478bd9Sstevel@tonic-gate 		    mcookie < mp->map_add + mp->map_size) {
1717c478bd9Sstevel@tonic-gate 			return (mp->map_add);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: mcookie_to_rvirt: invalid"
1757c478bd9Sstevel@tonic-gate 	    " mcookie: %llx\n", (uint64_t)mcookie);
176360e6f5eSmathue 
177360e6f5eSmathue 	return (0);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static void
dot_maps(fcode_env_t * env)1817c478bd9Sstevel@tonic-gate dot_maps(fcode_env_t *env)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int i;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "idx     base-addr        size\n");
1867c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MAPS; i++) {
1877c478bd9Sstevel@tonic-gate 		if (map_table[i].map_flags & MAP_IS_VALID)
1887c478bd9Sstevel@tonic-gate 			log_message(MSG_DEBUG, "%3d %016llx %8x\n", i,
1897c478bd9Sstevel@tonic-gate 			    map_table[i].map_add, map_table[i].map_size);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static void
map_qmark(fcode_env_t * env)1947c478bd9Sstevel@tonic-gate map_qmark(fcode_env_t *env)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	fstack_t d = POP(DS);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (!is_mcookie(d))
1997c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx: not mcookie\n", (uint64_t)d);
2007c478bd9Sstevel@tonic-gate 	else
2017c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx -> %llx\n", (uint64_t)d,
2027c478bd9Sstevel@tonic-gate 		    mcookie_to_addr(d));
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static void
add_map(fcode_env_t * env)2067c478bd9Sstevel@tonic-gate add_map(fcode_env_t *env)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	fstack_t size, addr;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	size = POP(DS);
2117c478bd9Sstevel@tonic-gate 	addr = POP(DS);
2127c478bd9Sstevel@tonic-gate 	addr = mapping_to_mcookie(addr, size, NULL, NULL);
2137c478bd9Sstevel@tonic-gate 	PUSH(DS, addr);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static void
del_map(fcode_env_t * env)2177c478bd9Sstevel@tonic-gate del_map(fcode_env_t *env)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	fstack_t addr;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	addr = POP(DS);
2227c478bd9Sstevel@tonic-gate 	delete_mapping(addr);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #pragma init(_init)
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static void
_init(void)2297c478bd9Sstevel@tonic-gate _init(void)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	ASSERT(env);
2347c478bd9Sstevel@tonic-gate 	NOTICE;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	FORTH(0,	".maps",		dot_maps);
2377c478bd9Sstevel@tonic-gate 	FORTH(0,	"map?",			map_qmark);
2387c478bd9Sstevel@tonic-gate 	FORTH(0,	"add-map",		add_map);
2397c478bd9Sstevel@tonic-gate 	FORTH(0,	"del-map",		del_map);
2407c478bd9Sstevel@tonic-gate }
241