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