xref: /onnv-gate/usr/src/lib/efcode/engine/package.c (revision 1772:78cca3d2cc4b)
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
5*1772Sjl139090  * Common Development and Distribution License (the "License").
6*1772Sjl139090  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1772Sjl139090  * Copyright 2006 Sun Microsystems, Inc.   All rights reserved.
23*1772Sjl139090  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <stddef.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <fcode/private.h>
340Sstevel@tonic-gate #include <fcode/log.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	MIN_VALUES	100
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static void
check_my_self(fcode_env_t * env,char * fn)410Sstevel@tonic-gate check_my_self(fcode_env_t *env, char *fn)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	if (!MYSELF)
440Sstevel@tonic-gate 		forth_abort(env, "%s: MYSELF is NULL", fn);
450Sstevel@tonic-gate }
460Sstevel@tonic-gate 
470Sstevel@tonic-gate uint_t
get_number_of_parent_address_cells(fcode_env_t * env)480Sstevel@tonic-gate get_number_of_parent_address_cells(fcode_env_t *env)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	uint_t ncells;
510Sstevel@tonic-gate 	device_t *d;
520Sstevel@tonic-gate 	static char func_name[] = "get_number_of_parent_address_cells";
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if (MYSELF == NULL)	/* Kludge for testing */
550Sstevel@tonic-gate 		return (2);
560Sstevel@tonic-gate 	d = MYSELF->device;
570Sstevel@tonic-gate 	ncells = d->parent_adr_cells;
580Sstevel@tonic-gate 	if (ncells == 0) {
590Sstevel@tonic-gate 		ncells = get_default_intprop(env, "#address-cells", d->parent,
600Sstevel@tonic-gate 		    2);
610Sstevel@tonic-gate 		if (ncells > MAX_MY_ADDR) {
620Sstevel@tonic-gate 			log_message(MSG_ERROR, "%s: %s:"
630Sstevel@tonic-gate 			    " ncells (%d) > MAX_MY_ADDR (%d)\n", func_name,
640Sstevel@tonic-gate 			    get_path(env, d->parent), ncells, MAX_MY_ADDR);
650Sstevel@tonic-gate 			ncells = MAX_MY_ADDR;
660Sstevel@tonic-gate 		}
670Sstevel@tonic-gate 		d->parent_adr_cells = ncells;
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	return (ncells);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate instance_t *
create_ihandle(fcode_env_t * env,device_t * phandle,instance_t * parent)730Sstevel@tonic-gate create_ihandle(fcode_env_t *env, device_t *phandle, instance_t *parent)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	instance_t *ihandle;
760Sstevel@tonic-gate 	int i;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	ihandle = MALLOC(sizeof (instance_t));
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	i = max(phandle->data_size[INIT_DATA], MIN_VALUES);
810Sstevel@tonic-gate 	ihandle->data[INIT_DATA] = MALLOC(sizeof (fstack_t) * i);
820Sstevel@tonic-gate 	memcpy(ihandle->data[INIT_DATA], phandle->init_data,
830Sstevel@tonic-gate 	    (size_t) (sizeof (fstack_t) * i));
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	i = max(phandle->data_size[UINIT_DATA], MIN_VALUES);
860Sstevel@tonic-gate 	ihandle->data[UINIT_DATA] = MALLOC(sizeof (fstack_t) * i);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	ihandle->my_space = phandle->my_space;
890Sstevel@tonic-gate 	memcpy(ihandle->my_addr, phandle->my_addr, sizeof (ihandle->my_addr));
900Sstevel@tonic-gate 	ihandle->parent = parent;
910Sstevel@tonic-gate 	ihandle->device = phandle;
920Sstevel@tonic-gate 	return (ihandle);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate device_t *
create_phandle(fcode_env_t * env,device_t * parent)960Sstevel@tonic-gate create_phandle(fcode_env_t *env, device_t *parent)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	device_t *phandle;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	phandle = MALLOC(sizeof (device_t));
1010Sstevel@tonic-gate 	phandle->init_data = MALLOC(sizeof (fstack_t) * MIN_VALUES);
1020Sstevel@tonic-gate 	phandle->data_size[INIT_DATA] = 0;
1030Sstevel@tonic-gate 	phandle->data_size[UINIT_DATA] = 0;
1040Sstevel@tonic-gate 	phandle->parent = parent;
1050Sstevel@tonic-gate 	return (phandle);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static void
do_push_package(fcode_env_t * env,device_t * d)1100Sstevel@tonic-gate do_push_package(fcode_env_t *env, device_t *d)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	do_previous(env);
1130Sstevel@tonic-gate 	do_also(env);
1140Sstevel@tonic-gate 	if (d != NULL) {
1150Sstevel@tonic-gate 		CONTEXT = (token_t *)(&d->vocabulary);
1160Sstevel@tonic-gate 		debug_msg(DEBUG_CONTEXT, "CONTEXT:push_package: %s%d/%p/%p\n",
1170Sstevel@tonic-gate 		    get_path(env, d), env->order_depth, CONTEXT, env->current);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static void
push_package(fcode_env_t * env)1220Sstevel@tonic-gate push_package(fcode_env_t *env)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	device_t *d;
1250Sstevel@tonic-gate 	phandle_t ph;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "push-package");
1280Sstevel@tonic-gate 	ph = POP(DS);
1290Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, ph);
1300Sstevel@tonic-gate 	do_push_package(env, d);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static void
pop_package(fcode_env_t * env)1340Sstevel@tonic-gate pop_package(fcode_env_t *env)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	do_previous(env);
1370Sstevel@tonic-gate 	do_definitions(env);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static void
interpose(fcode_env_t * env)1410Sstevel@tonic-gate interpose(fcode_env_t *env)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	TODO;	/* interpose - not yet implemented */
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate void
activate_device(fcode_env_t * env,device_t * d)1470Sstevel@tonic-gate activate_device(fcode_env_t *env, device_t *d)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	env->current_device = d;
1500Sstevel@tonic-gate 	do_push_package(env, d);
1510Sstevel@tonic-gate 	do_definitions(env);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate void
deactivate_device(fcode_env_t * env,device_t * d)1550Sstevel@tonic-gate deactivate_device(fcode_env_t *env, device_t *d)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	env->current_device = d;
1580Sstevel@tonic-gate 	do_previous(env);
1590Sstevel@tonic-gate 	if (d != NULL) {
1600Sstevel@tonic-gate 		CONTEXT = (token_t *)(&d->vocabulary);
1610Sstevel@tonic-gate 		debug_msg(DEBUG_CONTEXT, "CONTEXT:deactivate_device:"
1620Sstevel@tonic-gate 		    " %s%d/%p/%p\n", get_path(env, d), env->order_depth,
1630Sstevel@tonic-gate 		    CONTEXT, env->current);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 	do_definitions(env);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * Starfire hack to set '/' device_type to 'upa'
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate #include <sys/systeminfo.h>
1720Sstevel@tonic-gate static void
starfire_hack(fcode_env_t * env)1730Sstevel@tonic-gate starfire_hack(fcode_env_t *env)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	char platform[100];
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	sysinfo(SI_PLATFORM, platform, sizeof (platform));
1780Sstevel@tonic-gate 	if (strcmp(platform, "SUNW,Ultra-Enterprise-10000") == 0 &&
1790Sstevel@tonic-gate 	    find_property(env->root_node, "device_type") == NULL) {
1800Sstevel@tonic-gate 		create_string_prop(env, "device_type", "upa");
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate void
root_node(fcode_env_t * env)1850Sstevel@tonic-gate root_node(fcode_env_t *env)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	do_also(env);
1880Sstevel@tonic-gate 	activate_device(env, env->root_node);
1890Sstevel@tonic-gate 	starfire_hack(env);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate void
child_node(fcode_env_t * env)1930Sstevel@tonic-gate child_node(fcode_env_t *env)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	device_t *d;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "child");
1980Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, TOS);
1990Sstevel@tonic-gate 	TOS = (fstack_t)d->child;
2000Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, d->child);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate void
peer_node(fcode_env_t * env)2040Sstevel@tonic-gate peer_node(fcode_env_t *env)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	device_t *d;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "peer");
2090Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, TOS);
2100Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, d->peer);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate void
new_device(fcode_env_t * env)2140Sstevel@tonic-gate new_device(fcode_env_t *env)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	device_t *phandle, *parent;
2170Sstevel@tonic-gate 	device_t *peer;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	check_my_self(env, "new-device");
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	parent = MYSELF->device;
2220Sstevel@tonic-gate 	phandle = create_phandle(env, parent);
2230Sstevel@tonic-gate 	MYSELF = create_ihandle(env, phandle, MYSELF);
2240Sstevel@tonic-gate 	activate_device(env, phandle);
2250Sstevel@tonic-gate 	if (parent->child) {
2260Sstevel@tonic-gate 		/* Insert new child at end of peer list */
2270Sstevel@tonic-gate 		for (peer = parent->child; peer->peer; peer = peer->peer)
2280Sstevel@tonic-gate 			;
2290Sstevel@tonic-gate 		peer->peer = phandle;
2300Sstevel@tonic-gate 	} else
2310Sstevel@tonic-gate 		parent->child = phandle;	/* First child */
2320Sstevel@tonic-gate 	ALLOCATE_PHANDLE(env);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate void
finish_device(fcode_env_t * env)2360Sstevel@tonic-gate finish_device(fcode_env_t *env)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	fstack_t *mem;
2390Sstevel@tonic-gate 	device_t *my_dev, *parent_dev;
2400Sstevel@tonic-gate 	instance_t *parent, *myself = MYSELF;
2410Sstevel@tonic-gate 	int  n;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	check_my_self(env, "finish-device");
2440Sstevel@tonic-gate 	ASSERT(myself->device);
2450Sstevel@tonic-gate 	ASSERT(env->current_device);
2460Sstevel@tonic-gate 	n = myself->device->data_size[INIT_DATA];
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * Paranoia.. reserve a little more instance data than we need
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	mem = MALLOC(sizeof (fstack_t) * (n+8));
2520Sstevel@tonic-gate 	memcpy(mem, MYSELF->device->init_data, sizeof (fstack_t) * n);
2530Sstevel@tonic-gate 	FREE(myself->device->init_data);
2540Sstevel@tonic-gate 	my_dev = myself->device;
2550Sstevel@tonic-gate 	my_dev->init_data = mem;
2560Sstevel@tonic-gate 	parent = MYSELF->parent;
2570Sstevel@tonic-gate 	parent_dev = env->current_device->parent;
2580Sstevel@tonic-gate 	FREE(MYSELF);
2590Sstevel@tonic-gate 	MYSELF = parent;
2600Sstevel@tonic-gate 	activate_device(env, parent_dev);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate static void
create_internal_value(fcode_env_t * env,char * name,int offset,int token)2640Sstevel@tonic-gate create_internal_value(fcode_env_t *env, char *name, int offset, int token)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	header(env, name, strlen(name), 0);
2670Sstevel@tonic-gate 	COMPILE_TOKEN(&noop);
2680Sstevel@tonic-gate 	EXPOSE_ACF;
2690Sstevel@tonic-gate 	if (token) {
2700Sstevel@tonic-gate 		SET_TOKEN(token, 0, name, LINK_TO_ACF(env->lastlink));
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	PUSH(DS, offset);
2730Sstevel@tonic-gate 	lcomma(env);
2740Sstevel@tonic-gate 	set_internal_value_actions(env);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate static void
create_my_self(fcode_env_t * env)2780Sstevel@tonic-gate create_my_self(fcode_env_t *env)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	int offset = offsetof(fcode_env_t, my_self);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	create_internal_value(env, "my-self", offset, 0x203);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate static void
create_my_space(fcode_env_t * env)2860Sstevel@tonic-gate create_my_space(fcode_env_t *env)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	int offset = offsetof(instance_t, my_space);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	create_internal_value(env, "my-space", -offset, 0x103);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate void
my_address(fcode_env_t * env)2940Sstevel@tonic-gate my_address(fcode_env_t *env)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	fstack_t *adr_ptr;
2970Sstevel@tonic-gate 	uint_t ncells;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	check_my_self(env, "my-address");
3000Sstevel@tonic-gate 	ncells = get_number_of_parent_address_cells(env);
3010Sstevel@tonic-gate 	adr_ptr = MYSELF->my_addr;
3020Sstevel@tonic-gate 	while (--ncells) {
3030Sstevel@tonic-gate 		PUSH(DS, *adr_ptr);
3040Sstevel@tonic-gate 		adr_ptr++;
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate void
my_unit(fcode_env_t * env)3090Sstevel@tonic-gate my_unit(fcode_env_t *env)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	check_my_self(env, "my-unit");
3120Sstevel@tonic-gate 	my_address(env);
3130Sstevel@tonic-gate 	PUSH(DS, MYSELF->my_space);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static void
my_args(fcode_env_t * env)3170Sstevel@tonic-gate my_args(fcode_env_t *env)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	check_my_self(env, "my-args");
3200Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->my_args);
3210Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->my_args_len);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate int
call_my_parent(fcode_env_t * env,char * method)3250Sstevel@tonic-gate call_my_parent(fcode_env_t *env, char *method)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	push_a_string(env, method);
3280Sstevel@tonic-gate 	dollar_call_parent(env);
3290Sstevel@tonic-gate 	return (env->last_error);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate void
set_args(fcode_env_t * env)3330Sstevel@tonic-gate set_args(fcode_env_t *env)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	int args_len;
3360Sstevel@tonic-gate 	common_data_t *cdp;
3370Sstevel@tonic-gate 	uint_t ncells;
3380Sstevel@tonic-gate 	fstack_t *adr_ptr, *adr_ptr1, space;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	CHECK_DEPTH(env, 4, "set-args");
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	check_my_self(env, "set-args");
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Handle args argument of set-args.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	if (MYSELF->my_args) {
3480Sstevel@tonic-gate 		FREE(MYSELF->my_args);
3490Sstevel@tonic-gate 		MYSELF->my_args = NULL;
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	two_swap(env);
3520Sstevel@tonic-gate 	MYSELF->my_args = pop_a_duped_string(env, &args_len);
3530Sstevel@tonic-gate 	MYSELF->my_args_len = args_len;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if (call_my_parent(env, "decode-unit"))
3560Sstevel@tonic-gate 		forth_abort(env, "set-args: decode-unit failed");
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	ncells = get_number_of_parent_address_cells(env);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * Kludge: For GP2, my-space comes from decode-unit hi.address.
3620Sstevel@tonic-gate 	 * for PCI, my-space from decode-unit won't have the bus#, so we need
3630Sstevel@tonic-gate 	 * to get it from config_address.  Unfortunately, there is no easy
3640Sstevel@tonic-gate 	 * way to figure out here which one we're looking at.  We take the
3650Sstevel@tonic-gate 	 * expediant of or'ing the two values together.
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 	space = POP(DS);	/* pop phys.hi */
3680Sstevel@tonic-gate 	if ((cdp = (common_data_t *)env->private) != NULL)
3690Sstevel@tonic-gate 		space |= cdp->fc.config_address;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	MYSELF->device->my_space = MYSELF->my_space = space;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	adr_ptr = MYSELF->my_addr;
3740Sstevel@tonic-gate 	adr_ptr1 = MYSELF->device->my_addr;
3750Sstevel@tonic-gate 	while (--ncells) {
3760Sstevel@tonic-gate 		*adr_ptr++ = *adr_ptr1++ = POP(DS);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate void
my_parent(fcode_env_t * env)3810Sstevel@tonic-gate my_parent(fcode_env_t *env)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	check_my_self(env, "my-parent");
3840Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->parent);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate instance_t *
open_instance_chain(fcode_env_t * env,device_t * phandle,int exec)3880Sstevel@tonic-gate open_instance_chain(fcode_env_t *env, device_t *phandle, int exec)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	instance_t *parent;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (!phandle)
3930Sstevel@tonic-gate 		return (NULL);
3940Sstevel@tonic-gate 	parent = open_instance_chain(env, phandle->parent, exec);
3950Sstevel@tonic-gate 	return (create_ihandle(env, phandle, parent));
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate void
close_instance_chain(fcode_env_t * env,instance_t * ihandle,int exec)3990Sstevel@tonic-gate close_instance_chain(fcode_env_t *env, instance_t *ihandle, int exec)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	instance_t *parent;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if (ihandle) {
4040Sstevel@tonic-gate 		parent = ihandle->parent;
4050Sstevel@tonic-gate 		close_instance_chain(env, parent, exec);
4060Sstevel@tonic-gate 		if (ihandle->my_args)
4070Sstevel@tonic-gate 			FREE(ihandle->my_args);
4080Sstevel@tonic-gate 		FREE(ihandle);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate void
begin_package(fcode_env_t * env)4130Sstevel@tonic-gate begin_package(fcode_env_t *env)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	fstack_t ok;
4160Sstevel@tonic-gate 	char *name;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	CHECK_DEPTH(env, 6, "begin-package");
4190Sstevel@tonic-gate 	two_dup(env);
4200Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
4210Sstevel@tonic-gate 	find_package(env);
4220Sstevel@tonic-gate 	ok = POP(DS);
4230Sstevel@tonic-gate 	if (ok) {
4240Sstevel@tonic-gate 		PUSH(DS, 0);
4250Sstevel@tonic-gate 		PUSH(DS, 0);
4260Sstevel@tonic-gate 		rot(env);
4270Sstevel@tonic-gate 		open_package(env);
4280Sstevel@tonic-gate 		MYSELF = (instance_t *)POP(DS);
4290Sstevel@tonic-gate 		check_my_self(env, "begin-package");
4300Sstevel@tonic-gate 		new_device(env);
4310Sstevel@tonic-gate 		set_args(env);
4320Sstevel@tonic-gate 	} else {
4330Sstevel@tonic-gate 		log_message(MSG_INFO, "Package '%s' not found\n", name);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate void
open_package(fcode_env_t * env)4380Sstevel@tonic-gate open_package(fcode_env_t *env)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	device_t *phandle;
4410Sstevel@tonic-gate 	instance_t *ihandle;
4420Sstevel@tonic-gate 	int len;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "open-package");
4450Sstevel@tonic-gate 	CONVERT_PHANDLE(env, phandle, POP(DS));
4460Sstevel@tonic-gate 	ihandle = open_instance_chain(env, phandle, 1);
4470Sstevel@tonic-gate 	ihandle->my_args = pop_a_duped_string(env, &len);
4480Sstevel@tonic-gate 	ihandle->my_args_len = len;
4490Sstevel@tonic-gate 	PUSH(DS, (fstack_t)ihandle);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate void
dollar_open_package(fcode_env_t * env)4530Sstevel@tonic-gate dollar_open_package(fcode_env_t *env)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	fstack_t ok;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	CHECK_DEPTH(env, 4, "$open-package");
4580Sstevel@tonic-gate 	find_package(env);
4590Sstevel@tonic-gate 	ok = POP(DS);
4600Sstevel@tonic-gate 	if (ok) {
4610Sstevel@tonic-gate 		open_package(env);
4620Sstevel@tonic-gate 	} else {
463*1772Sjl139090 		(void) POP(DS);
464*1772Sjl139090 		(void) POP(DS);
4650Sstevel@tonic-gate 		PUSH(DS, 0);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate void
close_package(fcode_env_t * env)4700Sstevel@tonic-gate close_package(fcode_env_t *env)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	instance_t *ihandle;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "close-package");
4750Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
4760Sstevel@tonic-gate 	close_instance_chain(env, ihandle, 1);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate static void (*find_method_hook)(fcode_env_t *);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate void
set_find_method_hook(fcode_env_t * env,void (* hook)(fcode_env_t *))4820Sstevel@tonic-gate set_find_method_hook(fcode_env_t *env, void (*hook)(fcode_env_t *))
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	find_method_hook = hook;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate void
find_method(fcode_env_t * env)4880Sstevel@tonic-gate find_method(fcode_env_t *env)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	fstack_t d;
4910Sstevel@tonic-gate 	device_t *device;
4920Sstevel@tonic-gate 	acf_t acf = 0;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "find-method");
4950Sstevel@tonic-gate 	if (find_method_hook) {
4960Sstevel@tonic-gate 		(*find_method_hook)(env);
4970Sstevel@tonic-gate 		if (TOS)		/* Found it */
4980Sstevel@tonic-gate 			return;
4990Sstevel@tonic-gate 		POP(DS);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	d = POP(DS);
5030Sstevel@tonic-gate 	CONVERT_PHANDLE(env, device, d);
5040Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&device->vocabulary);
5050Sstevel@tonic-gate 	acf = voc_find(env);
5060Sstevel@tonic-gate 	PUSH(DS, (fstack_t)acf);
5070Sstevel@tonic-gate 	if (acf) {
5080Sstevel@tonic-gate 		PUSH(DS, TRUE);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * 'call-package' Fcode
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate void
call_package(fcode_env_t * env)5160Sstevel@tonic-gate call_package(fcode_env_t *env)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	instance_t *ihandle, *saved_myself;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "call-package");
5210Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
5220Sstevel@tonic-gate 	saved_myself = MYSELF;
5230Sstevel@tonic-gate 	MYSELF = ihandle;
5240Sstevel@tonic-gate 	execute(env);
5250Sstevel@tonic-gate 	MYSELF = saved_myself;
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate void
ihandle_to_phandle(fcode_env_t * env)5290Sstevel@tonic-gate ihandle_to_phandle(fcode_env_t *env)
5300Sstevel@tonic-gate {
5310Sstevel@tonic-gate 	instance_t *i;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "ihandle>phandle");
5340Sstevel@tonic-gate 	i = (instance_t *)TOS;
5350Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, i->device);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate char *
get_package_name(fcode_env_t * env,device_t * d)5390Sstevel@tonic-gate get_package_name(fcode_env_t *env, device_t *d)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	char *name;
5420Sstevel@tonic-gate 	prop_t *prop;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	prop = lookup_package_property(env, "name", d);
5450Sstevel@tonic-gate 	if (prop == NULL) {
5460Sstevel@tonic-gate 		name = "<Unnamed>";
5470Sstevel@tonic-gate 	} else {
5480Sstevel@tonic-gate 		name = (char *)prop->data;
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 	return (name);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate static char *package_search_path = "/packages:/openprom";
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate device_t *
match_package_path(fcode_env_t * env,char * path)5560Sstevel@tonic-gate match_package_path(fcode_env_t *env, char *path)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	device_t *d;
5590Sstevel@tonic-gate 	char *name;
5600Sstevel@tonic-gate 	int len;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (*path == '/') {
5630Sstevel@tonic-gate 		d = env->root_node->child;
5640Sstevel@tonic-gate 		path++;
5650Sstevel@tonic-gate 	} else
5660Sstevel@tonic-gate 		d = env->current_device;
5670Sstevel@tonic-gate 	while (*path != '\0' && d != NULL) {
5680Sstevel@tonic-gate 		name = get_package_name(env, d);
5690Sstevel@tonic-gate 		len = strlen(name);
5700Sstevel@tonic-gate 		if (strncmp(name, path, len) == 0) {
5710Sstevel@tonic-gate 			path += len;
5720Sstevel@tonic-gate 			if (*path == '\0') {
5730Sstevel@tonic-gate 				return (d);
5740Sstevel@tonic-gate 			}
5750Sstevel@tonic-gate 			/* skip the '/' */
5760Sstevel@tonic-gate 			if (*path++ != '/')
5770Sstevel@tonic-gate 				break;
5780Sstevel@tonic-gate 			d = d->child;
5790Sstevel@tonic-gate 		} else {
5800Sstevel@tonic-gate 			d = d->peer;
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	return (NULL);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate device_t *
locate_package(fcode_env_t * env,char * start)5870Sstevel@tonic-gate locate_package(fcode_env_t *env, char *start)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	device_t *d;
5900Sstevel@tonic-gate 	char *p, *next_p;
5910Sstevel@tonic-gate 	char *tpath, *fpath;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if ((d = match_package_path(env, start)) != NULL)
5940Sstevel@tonic-gate 		return (d);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	/*
5970Sstevel@tonic-gate 	 * ignore starting '/'
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	if (*start == '/')
6000Sstevel@tonic-gate 		*start++;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	fpath = STRDUP(package_search_path);
6030Sstevel@tonic-gate 	for (p = fpath; p != NULL; p = next_p) {
6040Sstevel@tonic-gate 		if ((next_p = strchr(p, ':')) != NULL)
6050Sstevel@tonic-gate 			*next_p++ = '\0';
6060Sstevel@tonic-gate 		tpath = MALLOC(strlen(p) + strlen(start) + 2);
6070Sstevel@tonic-gate 		sprintf(tpath, "%s/%s", p, start);
6080Sstevel@tonic-gate 		if ((d = match_package_path(env, tpath)) != NULL) {
6090Sstevel@tonic-gate 			FREE(fpath);
6100Sstevel@tonic-gate 			FREE(tpath);
6110Sstevel@tonic-gate 			return (d);
6120Sstevel@tonic-gate 		}
6130Sstevel@tonic-gate 		FREE(tpath);
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 	FREE(fpath);
6160Sstevel@tonic-gate 	return (NULL);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate void
find_package(fcode_env_t * env)6200Sstevel@tonic-gate find_package(fcode_env_t *env)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	char *path;
6230Sstevel@tonic-gate 	device_t *package;
6240Sstevel@tonic-gate 	fstack_t ph = 0;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "find-package");
6270Sstevel@tonic-gate 	if ((path = pop_a_duped_string(env, NULL)) != NULL) {
6280Sstevel@tonic-gate 		if (strcmp(path, "/") == 0)
6290Sstevel@tonic-gate 			package = env->root_node;
6300Sstevel@tonic-gate 		else
6310Sstevel@tonic-gate 			package = locate_package(env, path);
6320Sstevel@tonic-gate 		FREE(path);
6330Sstevel@tonic-gate 		REVERT_PHANDLE(env, ph, package);
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 	PUSH(DS, ph);
6360Sstevel@tonic-gate 	if (package)
6370Sstevel@tonic-gate 		PUSH(DS, TRUE);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate static void
encode_unit_hack(fcode_env_t * env)6410Sstevel@tonic-gate encode_unit_hack(fcode_env_t *env)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	int hi, i;
6440Sstevel@tonic-gate 	uint_t ncells = get_number_of_parent_address_cells(env);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	for (i = 0; i < ncells; i++)
6470Sstevel@tonic-gate 		POP(DS);
6480Sstevel@tonic-gate 	push_a_string(env, NULL);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate void
dollar_call_method(fcode_env_t * env)6520Sstevel@tonic-gate dollar_call_method(fcode_env_t *env)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	instance_t *old_myself;
6550Sstevel@tonic-gate 	instance_t *myself;
6560Sstevel@tonic-gate 	device_t *device;
6570Sstevel@tonic-gate 	char *method;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "$call-method");
6600Sstevel@tonic-gate 	check_my_self(env, "$call-method");
6610Sstevel@tonic-gate 	old_myself = MYSELF;
6620Sstevel@tonic-gate 	myself = (instance_t *)POP(DS);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	method = (char *)DS[-1];
6650Sstevel@tonic-gate 	debug_msg(DEBUG_CALL_METHOD, "$call_method %s\n", method);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	if (old_myself && !myself) {
6680Sstevel@tonic-gate 		/* We hit the root of our tree */
6690Sstevel@tonic-gate 		device = old_myself->device;
6700Sstevel@tonic-gate 		return;
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	MYSELF = myself;
6740Sstevel@tonic-gate 	check_my_self(env, "$call-method");
6750Sstevel@tonic-gate 	device = MYSELF->device;
6760Sstevel@tonic-gate 	do_push_package(env, device);
6770Sstevel@tonic-gate 	PUSH(DS, (fstack_t)device);
6780Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, device);
6790Sstevel@tonic-gate 	find_method(env);
6800Sstevel@tonic-gate 	if (TOS) {
6810Sstevel@tonic-gate 		(void) POP(DS);
6820Sstevel@tonic-gate 		execute(env);
6830Sstevel@tonic-gate 	} else if (strcmp(method, "encode-unit") == 0) {
6840Sstevel@tonic-gate 		encode_unit_hack(env);
6850Sstevel@tonic-gate 	} else {
6860Sstevel@tonic-gate 		throw_from_fclib(env, 1, "Unimplemented package method: %s%s",
6870Sstevel@tonic-gate 		    get_path(env, device), method);
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 	MYSELF = old_myself;
6900Sstevel@tonic-gate 	do_push_package(env, MYSELF->device);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate void
dollar_call_parent(fcode_env_t * env)6940Sstevel@tonic-gate dollar_call_parent(fcode_env_t *env)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "$call-parent");
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	check_my_self(env, "$call-parent");
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->parent);
7010Sstevel@tonic-gate 	dollar_call_method(env);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate #ifdef DEBUG
7050Sstevel@tonic-gate void
current_device(fcode_env_t * env)7060Sstevel@tonic-gate current_device(fcode_env_t *env)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&env->current_device);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate char *
get_path(fcode_env_t * env,device_t * d)7120Sstevel@tonic-gate get_path(fcode_env_t *env, device_t *d)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	char *pre_path, *name, *path;
7150Sstevel@tonic-gate 	int n;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	if (d->parent)
7180Sstevel@tonic-gate 		pre_path = get_path(env, d->parent);
7190Sstevel@tonic-gate 	else
7200Sstevel@tonic-gate 		pre_path = STRDUP("");
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	name = get_package_name(env, d);
7230Sstevel@tonic-gate 	n = strlen(pre_path) + strlen(name) + 1;
7240Sstevel@tonic-gate 	path = MALLOC(n);
7250Sstevel@tonic-gate 	strcpy(path, pre_path);
7260Sstevel@tonic-gate 	strcat(path, name);
7270Sstevel@tonic-gate 	if (d->child && d->parent)
7280Sstevel@tonic-gate 		strcat(path, "/");
7290Sstevel@tonic-gate 	FREE(pre_path);
7300Sstevel@tonic-gate 	return (path);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate static void
pwd_dollar(fcode_env_t * env)7340Sstevel@tonic-gate pwd_dollar(fcode_env_t *env)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	if (env->current_device)
7370Sstevel@tonic-gate 		push_a_string(env, get_path(env, env->current_device));
7380Sstevel@tonic-gate 	else
7390Sstevel@tonic-gate 		push_a_string(env, NULL);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate void
pwd(fcode_env_t * env)7430Sstevel@tonic-gate pwd(fcode_env_t *env)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	if (env->current_device) {
7460Sstevel@tonic-gate 		log_message(MSG_INFO, "%s\n",
7470Sstevel@tonic-gate 		    get_path(env, env->current_device));
7480Sstevel@tonic-gate 	} else {
7490Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate void
do_ls(fcode_env_t * env)7540Sstevel@tonic-gate do_ls(fcode_env_t *env)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	device_t *d;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	if (env->current_device == NULL) {
7590Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
7600Sstevel@tonic-gate 		return;
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	d = env->current_device->child;
7640Sstevel@tonic-gate 	while (d) {
7650Sstevel@tonic-gate 		char *name;
7660Sstevel@tonic-gate 		fstack_t ph;
7670Sstevel@tonic-gate 		name = get_package_name(env, d);
7680Sstevel@tonic-gate 		REVERT_PHANDLE(env, ph, d);
7690Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx %s\n", (uint64_t)ph, name);
7700Sstevel@tonic-gate 		d = d->peer;
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate void
paren_cd(fcode_env_t * env)7750Sstevel@tonic-gate paren_cd(fcode_env_t *env)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate 	char *str;
7780Sstevel@tonic-gate 	device_t *p;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	str = pop_a_string(env, NULL);
7810Sstevel@tonic-gate 	if (strcmp(str, "/") == 0) {
7820Sstevel@tonic-gate 		root_node(env);
7830Sstevel@tonic-gate 		return;
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	if (env->current_device == NULL) {
7870Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
7880Sstevel@tonic-gate 		return;
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	if (strcmp(str, "..") == 0)
7920Sstevel@tonic-gate 		p = env->current_device->parent;
7930Sstevel@tonic-gate 	else {
7940Sstevel@tonic-gate 		device_t *n = env->current_device->child;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		p = NULL;
7970Sstevel@tonic-gate 		while (n) {
7980Sstevel@tonic-gate 			char *name;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 			name = get_package_name(env, n);
8010Sstevel@tonic-gate 			if (strcmp(name, str) == 0) {
8020Sstevel@tonic-gate 				p = n;
8030Sstevel@tonic-gate 				break;
8040Sstevel@tonic-gate 			}
8050Sstevel@tonic-gate 			n = n->peer;
8060Sstevel@tonic-gate 		}
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (p) {
8100Sstevel@tonic-gate 		activate_device(env, p);
8110Sstevel@tonic-gate 	} else {
8120Sstevel@tonic-gate 		log_message(MSG_INFO, "No such node: %s\n", str);
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate void
do_cd(fcode_env_t * env)8170Sstevel@tonic-gate do_cd(fcode_env_t *env)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	parse_word(env);
8200Sstevel@tonic-gate 	paren_cd(env);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate void
do_unselect_dev(fcode_env_t * env)8240Sstevel@tonic-gate do_unselect_dev(fcode_env_t *env)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	check_my_self(env, "unselect-dev");
8270Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF);
8280Sstevel@tonic-gate 	close_package(env);
8290Sstevel@tonic-gate 	deactivate_device(env, NULL);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate void
do_select_dev(fcode_env_t * env)8330Sstevel@tonic-gate do_select_dev(fcode_env_t *env)
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate 	PUSH(DS, 0);
8360Sstevel@tonic-gate 	PUSH(DS, 0);
8370Sstevel@tonic-gate 	two_swap(env);
8380Sstevel@tonic-gate 	dollar_open_package(env);
8390Sstevel@tonic-gate 	if (TOS) {
8400Sstevel@tonic-gate 		MYSELF = (instance_t *)POP(DS);
8410Sstevel@tonic-gate 		check_my_self(env, "select-dev");
8420Sstevel@tonic-gate 		activate_device(env, MYSELF->device);
8430Sstevel@tonic-gate 	} else {
8440Sstevel@tonic-gate 		drop(env);
8450Sstevel@tonic-gate 		log_message(MSG_INFO, "Can't open package\n");
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate void
device_end(fcode_env_t * env)8500Sstevel@tonic-gate device_end(fcode_env_t *env)
8510Sstevel@tonic-gate {
8520Sstevel@tonic-gate 	if (env->current_device) {
8530Sstevel@tonic-gate 		deactivate_device(env, NULL);
8540Sstevel@tonic-gate 	}
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate void
end_package(fcode_env_t * env)8580Sstevel@tonic-gate end_package(fcode_env_t *env)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	finish_device(env);
8610Sstevel@tonic-gate 	close_instance_chain(env, MYSELF, 0);
8620Sstevel@tonic-gate 	device_end(env);
8630Sstevel@tonic-gate 	MYSELF = NULL;
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate void
exec_parent_method(fcode_env_t * env)8670Sstevel@tonic-gate exec_parent_method(fcode_env_t *env)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	instance_t *old_myself;
8700Sstevel@tonic-gate 	instance_t *myself;
8710Sstevel@tonic-gate 	device_t *device;
8720Sstevel@tonic-gate 	char *method;
8730Sstevel@tonic-gate 	fstack_t d;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	check_my_self(env, "exec-parent-method");
8760Sstevel@tonic-gate 	old_myself = MYSELF;
8770Sstevel@tonic-gate 	MYSELF = MYSELF->parent;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	method = (char *)DS[-1];
8800Sstevel@tonic-gate 	debug_msg(DEBUG_FIND_FCODE, "exec_parent_method: '%s'\n", method);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	check_my_self(env, "exec-parent-method");
8830Sstevel@tonic-gate 	device = MYSELF->device;
8840Sstevel@tonic-gate 	do_push_package(env, device);
8850Sstevel@tonic-gate 	PUSH(DS, (fstack_t)device);
8860Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, device);
8870Sstevel@tonic-gate 	find_method(env);
8880Sstevel@tonic-gate 	d = POP(DS);
8890Sstevel@tonic-gate 	if (d) {
8900Sstevel@tonic-gate 		debug_msg(DEBUG_FIND_FCODE, "exec-parent-method: '%s'/%x"
8910Sstevel@tonic-gate 		    " execute\n", method, (int)TOS);
8920Sstevel@tonic-gate 		execute(env);
8930Sstevel@tonic-gate 		PUSH(DS, TRUE);
8940Sstevel@tonic-gate 	} else {
8950Sstevel@tonic-gate 		debug_msg(DEBUG_FIND_FCODE, "exec-parent-method: '%s'"
8960Sstevel@tonic-gate 		    " not found\n", method);
8970Sstevel@tonic-gate 		PUSH(DS, FALSE);
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 	MYSELF = old_myself;
9000Sstevel@tonic-gate 	do_push_package(env, MYSELF->device);
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate void
dump_device(fcode_env_t * env)9040Sstevel@tonic-gate dump_device(fcode_env_t *env)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	device_t *phandle;
9070Sstevel@tonic-gate 	int i;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	CONVERT_PHANDLE(env, phandle, POP(DS));
9100Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Node:      %p\n", phandle);
9110Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Parent:  (%8p) %p\n",
9120Sstevel@tonic-gate 	    &phandle->parent, phandle->parent);
9130Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Child:   (%8p) %p\n",
9140Sstevel@tonic-gate 	    &phandle->child, phandle->child);
9150Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Peer:    (%8p) %p\n",
9160Sstevel@tonic-gate 	    &phandle->peer, phandle->peer);
9170Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Private: (%8p) %p\n",
9180Sstevel@tonic-gate 	    &phandle->private, phandle->private);
9190Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Props:   (%8p) %p\n",
9200Sstevel@tonic-gate 	    &phandle->properties, phandle->properties);
9210Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Voc:     (%8p) %p\n",
9220Sstevel@tonic-gate 	    &phandle->vocabulary, phandle->vocabulary);
9230Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  sizes:   (%8p) %d %d\n",
9240Sstevel@tonic-gate 	    &phandle->data_size,
9250Sstevel@tonic-gate 	    phandle->data_size[INIT_DATA],
9260Sstevel@tonic-gate 	    phandle->data_size[UINIT_DATA]);
9270Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_space: %x\n", phandle->my_space);
9280Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_addr :");
9290Sstevel@tonic-gate 	for (i = 0; i < MAX_MY_ADDR; i++)
9300Sstevel@tonic-gate 		log_message(MSG_DEBUG, " %x", (int)phandle->my_addr[i]);
9310Sstevel@tonic-gate 	log_message(MSG_DEBUG, "\n");
9320Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  data:    (%8p)\n", phandle->init_data);
9330Sstevel@tonic-gate 	for (i = 0; i < phandle->data_size[INIT_DATA]; i++) {
9340Sstevel@tonic-gate 		log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
9350Sstevel@tonic-gate 		    &phandle->init_data[i], phandle->init_data[i]);
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate void
dump_instance(fcode_env_t * env)9400Sstevel@tonic-gate dump_instance(fcode_env_t *env)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	int i;
9430Sstevel@tonic-gate 	instance_t *ihandle;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
9460Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Ihandle:      %p\n", ihandle);
9470Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Parent:  (%8p) %p\n",
9480Sstevel@tonic-gate 	    &ihandle->parent, ihandle->parent);
9490Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Device:  (%8p) %p\n",
9500Sstevel@tonic-gate 	    &ihandle->device, ihandle->device);
9510Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  args:     '%s'\n",
9520Sstevel@tonic-gate 	    ((ihandle->my_args) ? ihandle->my_args : ""));
9530Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my-space: %x\n", ihandle->my_space);
9540Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_addr :");
9550Sstevel@tonic-gate 	for (i = 0; i < MAX_MY_ADDR; i++)
9560Sstevel@tonic-gate 		log_message(MSG_DEBUG, " %x", (int)ihandle->my_addr[i]);
9570Sstevel@tonic-gate 	log_message(MSG_DEBUG, "\n");
9580Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  sizes:   %d %d\n",
9590Sstevel@tonic-gate 	    ihandle->device->data_size[INIT_DATA],
9600Sstevel@tonic-gate 	    ihandle->device->data_size[UINIT_DATA]);
9610Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  data:    (%8p) %x %x\n",
9620Sstevel@tonic-gate 	    ihandle->data, ihandle->data[0], ihandle->data[1]);
9630Sstevel@tonic-gate 	if (ihandle->device->data_size[INIT_DATA]) {
9640Sstevel@tonic-gate 		log_message(MSG_DEBUG, "  Initialised:\n");
9650Sstevel@tonic-gate 		for (i = 0; i < ihandle->device->data_size[INIT_DATA]; i++) {
9660Sstevel@tonic-gate 			log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
9670Sstevel@tonic-gate 			    &ihandle->data[INIT_DATA][i],
9680Sstevel@tonic-gate 			    ihandle->data[INIT_DATA][i]);
9690Sstevel@tonic-gate 		}
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 	if (ihandle->device->data_size[INIT_DATA]) {
9720Sstevel@tonic-gate 		log_message(MSG_DEBUG, "  UnInitialised:\n");
9730Sstevel@tonic-gate 		for (i = 0; i < ihandle->device->data_size[UINIT_DATA]; i++) {
9740Sstevel@tonic-gate 			log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
9750Sstevel@tonic-gate 			    &ihandle->data[UINIT_DATA][i],
9760Sstevel@tonic-gate 			    ihandle->data[UINIT_DATA][i]);
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate #endif
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate #pragma init(_init)
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate #ifdef CONVERT_HANDLES
9860Sstevel@tonic-gate static device_t	*
safe_convert_phandle(fcode_env_t * env,fstack_t d)9870Sstevel@tonic-gate safe_convert_phandle(fcode_env_t *env, fstack_t d)
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate 	return ((device_t *)d);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate static fstack_t
safe_revert_phandle(fcode_env_t * env,device_t * d)9930Sstevel@tonic-gate safe_revert_phandle(fcode_env_t *env, device_t *d)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	return ((fstack_t)d);
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate static void
safe_allocate_phandle(fcode_env_t * env)9990Sstevel@tonic-gate safe_allocate_phandle(fcode_env_t *env)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate #endif
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate static void
_init(void)10060Sstevel@tonic-gate _init(void)
10070Sstevel@tonic-gate {
10080Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
10090Sstevel@tonic-gate 	char *name = "/";
10100Sstevel@tonic-gate 	device_t *d;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	ASSERT(env);
10130Sstevel@tonic-gate 	NOTICE;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate #ifdef CONVERT_HANDLES
10160Sstevel@tonic-gate 	env->convert_phandle = safe_convert_phandle;
10170Sstevel@tonic-gate 	env->revert_phandle = safe_revert_phandle;
10180Sstevel@tonic-gate 	env->allocate_phandle = safe_allocate_phandle;
10190Sstevel@tonic-gate #endif
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	/* build the root node */
10220Sstevel@tonic-gate 	d = create_phandle(env, NULL);
10230Sstevel@tonic-gate 	env->current_device = d;
10240Sstevel@tonic-gate 	env->root_node = d;
10250Sstevel@tonic-gate 	push_a_string(env, name);
10260Sstevel@tonic-gate 	device_name(env);
10270Sstevel@tonic-gate 	env->current_device = NULL;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	create_my_self(env);
10300Sstevel@tonic-gate 	create_my_space(env);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	P1275(0x102, 0,		"my-address",		my_address);
10330Sstevel@tonic-gate 	/* Fcode 0x103 "my-space" is created using create_internal_value */
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	P1275(0x11f, 0,		"new-device",		new_device);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	P1275(0x127, 0,		"finish-device",	finish_device);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	FCODE(0x129, 0,		"push-package",		push_package);
10400Sstevel@tonic-gate 	FCODE(0x12a, 0,		"pop-package",		pop_package);
10410Sstevel@tonic-gate 	FCODE(0x12b, 0,		"interpose",		interpose);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	P1275(0x202, 0,		"my-args",		my_args);
10440Sstevel@tonic-gate 	/* Fcode 0x203 "my-self" is created using create_internal_value */
10450Sstevel@tonic-gate 	P1275(0x204, 0,		"find-package",		find_package);
10460Sstevel@tonic-gate 	P1275(0x205, 0,		"open-package",		open_package);
10470Sstevel@tonic-gate 	P1275(0x206, 0,		"close-package",	close_package);
10480Sstevel@tonic-gate 	P1275(0x207, 0,		"find-method",		find_method);
10490Sstevel@tonic-gate 	P1275(0x208, 0,		"call-package",		call_package);
10500Sstevel@tonic-gate 	P1275(0x209, 0,		"$call-parent",		dollar_call_parent);
10510Sstevel@tonic-gate 	P1275(0x20a, 0,		"my-parent",		my_parent);
10520Sstevel@tonic-gate 	P1275(0x20b, 0,		"ihandle>phandle",	ihandle_to_phandle);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	P1275(0x20d, 0,		"my-unit",		my_unit);
10550Sstevel@tonic-gate 	P1275(0x20e, 0,		"$call-method",		dollar_call_method);
10560Sstevel@tonic-gate 	P1275(0x20f, 0,		"$open-package",	dollar_open_package);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	P1275(0x23b, 0,		"child",		child_node);
10590Sstevel@tonic-gate 	P1275(0x23c, 0,		"peer",			peer_node);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	P1275(0x23f, 0,		"set-args",		set_args);
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"root-node",		root_node);
10640Sstevel@tonic-gate 	FORTH(0,		"current-device",	current_device);
10650Sstevel@tonic-gate 	FORTH(0,		"pwd$",			pwd_dollar);
10660Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"pwd",			pwd);
10670Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"ls",			do_ls);
10680Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"(cd)",			paren_cd);
10690Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"cd",			do_cd);
10700Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"device-end",		device_end);
10710Sstevel@tonic-gate 	FORTH(0,		"select-dev",		do_select_dev);
10720Sstevel@tonic-gate 	FORTH(0,		"unselect-dev",		do_unselect_dev);
10730Sstevel@tonic-gate 	FORTH(0,		"begin-package",	begin_package);
10740Sstevel@tonic-gate 	FORTH(0,		"end-package",		end_package);
10750Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-device",		dump_device);
10760Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-instance",	dump_instance);
10770Sstevel@tonic-gate 	FORTH(0,		"exec-parent-method",	exec_parent_method);
10780Sstevel@tonic-gate }
1079