2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <stdarg.h>
2N/A
2N/A#include <fcode/private.h>
2N/A#include <fcode/log.h>
2N/A
2N/A#include <fcdriver/fcdriver.h>
2N/A
2N/A#define MAX_MAPS 256
2N/A
2N/A#define MAP_IS_VALID 0x01
2N/A
2N/Astruct map_table {
2N/A int map_flags;
2N/A uint64_t map_add;
2N/A size_t map_size;
2N/A uint64_t adj_virt;
2N/A size_t adj_length;
2N/A} map_table[MAX_MAPS];
2N/A
2N/A/*
2N/A * Originally, this code translated kernel supplied virtual addresses into
2N/A * "memory cookies", which was a 32-bit number with ascii-M in the upper 8
2N/A * bits, a 4-bit index and a 20-bit offset. However, this caused two
2N/A * problems: 1) the 20-bit offset was too small for some devices, esp. some
2N/A * with frame-buffers; 2) if the fcode used the cookie to program the
2N/A * hardware, there was no easy way for the software to detect that a
2N/A * translation needed to be done.
2N/A *
2N/A * For that reason, "memory cookies" are now just the kernel-supplied
2N/A * virtual address, and we now check each memory access to see if it's
2N/A * attempting to access kernel-supplied memory. The only important thing
2N/A * now is that "is_mcookie" returns 1 (or true) if the tested mcookie
2N/A * is a kernel virtual address.
2N/A *
2N/A * There is a potential bug if the kernel virtual address happens to
2N/A * conflict with a user virtual address. However, the current implementation
2N/A * of Solaris avoids this conflict.
2N/A */
2N/A
2N/Afstack_t
2N/Amapping_to_mcookie(uint64_t req_add, size_t req_size, uint64_t adj_virt,
2N/A size_t adj_length)
2N/A{
2N/A int i;
2N/A struct map_table *mp;
2N/A
2N/A for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
2N/A if ((mp->map_flags & MAP_IS_VALID) == 0)
2N/A break;
2N/A if (i == MAX_MAPS) {
2N/A log_message(MSG_WARN, "Warning: too many mappings\n");
2N/A return (0);
2N/A }
2N/A debug_msg(DEBUG_REG_ACCESS, "Allocating mapping: %d add: 0x%llx"
2N/A " size: 0x%x\n", i, req_add, req_size);
2N/A mp->map_flags |= MAP_IS_VALID;
2N/A mp->map_add = req_add;
2N/A mp->map_size = req_size;
2N/A mp->adj_virt = adj_virt;
2N/A mp->adj_length = adj_length;
2N/A if (mp->adj_length != 0)
2N/A return (adj_virt);
2N/A else
2N/A return (req_add);
2N/A}
2N/A
2N/Avoid
2N/Adelete_mapping(fstack_t mcookie)
2N/A{
2N/A int i;
2N/A struct map_table *mp;
2N/A
2N/A for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
2N/A if ((mp->map_flags & MAP_IS_VALID) &&
2N/A mcookie >= mp->map_add &&
2N/A mcookie < mp->map_add + mp->map_size) {
2N/A debug_msg(DEBUG_REG_ACCESS, "Deallocating mapping: %d"
2N/A " add: 0x%llx size: 0x%x\n", i, mp->map_add,
2N/A mp->map_size);
2N/A mp->map_flags &= ~MAP_IS_VALID;
2N/A mp->map_add = 0;
2N/A mp->map_size = 0;
2N/A mp->adj_virt = 0;
2N/A mp->adj_length = 0;
2N/A return;
2N/A }
2N/A }
2N/A log_message(MSG_WARN, "Warning: delete_mapping: invalid"
2N/A " mcookie: %llx\n", (uint64_t)mcookie);
2N/A}
2N/A
2N/Aint
2N/Ais_mcookie(fstack_t mcookie)
2N/A{
2N/A struct map_table *mp;
2N/A int i;
2N/A
2N/A for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++)
2N/A if ((mp->map_flags & MAP_IS_VALID) &&
2N/A mcookie >= mp->map_add &&
2N/A mcookie < mp->map_add + mp->map_size)
2N/A return (1);
2N/A return (0);
2N/A}
2N/A
2N/Auint64_t
2N/Amcookie_to_addr(fstack_t mcookie)
2N/A{
2N/A return (mcookie);
2N/A}
2N/A
2N/Afstack_t
2N/Amcookie_to_rlen(fstack_t mcookie)
2N/A{
2N/A int i;
2N/A struct map_table *mp;
2N/A
2N/A for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
2N/A if ((mp->map_flags & MAP_IS_VALID) &&
2N/A mcookie >= mp->map_add &&
2N/A mcookie < mp->map_add + mp->map_size) {
2N/A return (mp->map_size);
2N/A }
2N/A }
2N/A log_message(MSG_WARN, "Warning: mcookie_to_rlen: invalid"
2N/A " mcookie: %llx\n", (uint64_t)mcookie);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Afstack_t
2N/Amcookie_to_rvirt(fstack_t mcookie)
2N/A{
2N/A int i;
2N/A struct map_table *mp;
2N/A
2N/A for (i = 0, mp = map_table; i < MAX_MAPS; i++, mp++) {
2N/A if ((mp->map_flags & MAP_IS_VALID) &&
2N/A mcookie >= mp->map_add &&
2N/A mcookie < mp->map_add + mp->map_size) {
2N/A return (mp->map_add);
2N/A }
2N/A }
2N/A log_message(MSG_WARN, "Warning: mcookie_to_rvirt: invalid"
2N/A " mcookie: %llx\n", (uint64_t)mcookie);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic void
2N/Adot_maps(fcode_env_t *env)
2N/A{
2N/A int i;
2N/A
2N/A log_message(MSG_DEBUG, "idx base-addr size\n");
2N/A for (i = 0; i < MAX_MAPS; i++) {
2N/A if (map_table[i].map_flags & MAP_IS_VALID)
2N/A log_message(MSG_DEBUG, "%3d %016llx %8x\n", i,
2N/A map_table[i].map_add, map_table[i].map_size);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Amap_qmark(fcode_env_t *env)
2N/A{
2N/A fstack_t d = POP(DS);
2N/A
2N/A if (!is_mcookie(d))
2N/A log_message(MSG_INFO, "%llx: not mcookie\n", (uint64_t)d);
2N/A else
2N/A log_message(MSG_INFO, "%llx -> %llx\n", (uint64_t)d,
2N/A mcookie_to_addr(d));
2N/A}
2N/A
2N/Astatic void
2N/Aadd_map(fcode_env_t *env)
2N/A{
2N/A fstack_t size, addr;
2N/A
2N/A size = POP(DS);
2N/A addr = POP(DS);
2N/A addr = mapping_to_mcookie(addr, size, NULL, NULL);
2N/A PUSH(DS, addr);
2N/A}
2N/A
2N/Astatic void
2N/Adel_map(fcode_env_t *env)
2N/A{
2N/A fstack_t addr;
2N/A
2N/A addr = POP(DS);
2N/A delete_mapping(addr);
2N/A}
2N/A
2N/A
2N/A#pragma init(_init)
2N/A
2N/Astatic void
2N/A_init(void)
2N/A{
2N/A fcode_env_t *env = initial_env;
2N/A
2N/A ASSERT(env);
2N/A NOTICE;
2N/A
2N/A FORTH(0, ".maps", dot_maps);
2N/A FORTH(0, "map?", map_qmark);
2N/A FORTH(0, "add-map", add_map);
2N/A FORTH(0, "del-map", del_map);
2N/A}