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