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