xref: /illumos-gate/usr/src/lib/efcode/engine/package.c (revision 09e6639b)
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
525cf1a30Sjl  * Common Development and Distribution License (the "License").
625cf1a30Sjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2225cf1a30Sjl  * Copyright 2006 Sun Microsystems, Inc.   All rights reserved.
2325cf1a30Sjl  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
252a1fd0ffSPeter Tribble /*
262a1fd0ffSPeter Tribble  * Copyright 2019 Peter Tribble.
272a1fd0ffSPeter Tribble  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stddef.h>
327c478bd9Sstevel@tonic-gate #include <string.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	MIN_VALUES	100
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static void
check_my_self(fcode_env_t * env,char * fn)427c478bd9Sstevel@tonic-gate check_my_self(fcode_env_t *env, char *fn)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	if (!MYSELF)
457c478bd9Sstevel@tonic-gate 		forth_abort(env, "%s: MYSELF is NULL", fn);
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate uint_t
get_number_of_parent_address_cells(fcode_env_t * env)497c478bd9Sstevel@tonic-gate get_number_of_parent_address_cells(fcode_env_t *env)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	uint_t ncells;
527c478bd9Sstevel@tonic-gate 	device_t *d;
537c478bd9Sstevel@tonic-gate 	static char func_name[] = "get_number_of_parent_address_cells";
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (MYSELF == NULL)	/* Kludge for testing */
567c478bd9Sstevel@tonic-gate 		return (2);
577c478bd9Sstevel@tonic-gate 	d = MYSELF->device;
587c478bd9Sstevel@tonic-gate 	ncells = d->parent_adr_cells;
597c478bd9Sstevel@tonic-gate 	if (ncells == 0) {
607c478bd9Sstevel@tonic-gate 		ncells = get_default_intprop(env, "#address-cells", d->parent,
617c478bd9Sstevel@tonic-gate 		    2);
627c478bd9Sstevel@tonic-gate 		if (ncells > MAX_MY_ADDR) {
637c478bd9Sstevel@tonic-gate 			log_message(MSG_ERROR, "%s: %s:"
647c478bd9Sstevel@tonic-gate 			    " ncells (%d) > MAX_MY_ADDR (%d)\n", func_name,
657c478bd9Sstevel@tonic-gate 			    get_path(env, d->parent), ncells, MAX_MY_ADDR);
667c478bd9Sstevel@tonic-gate 			ncells = MAX_MY_ADDR;
677c478bd9Sstevel@tonic-gate 		}
687c478bd9Sstevel@tonic-gate 		d->parent_adr_cells = ncells;
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 	return (ncells);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate instance_t *
create_ihandle(fcode_env_t * env,device_t * phandle,instance_t * parent)747c478bd9Sstevel@tonic-gate create_ihandle(fcode_env_t *env, device_t *phandle, instance_t *parent)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
777c478bd9Sstevel@tonic-gate 	int i;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	ihandle = MALLOC(sizeof (instance_t));
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	i = max(phandle->data_size[INIT_DATA], MIN_VALUES);
827c478bd9Sstevel@tonic-gate 	ihandle->data[INIT_DATA] = MALLOC(sizeof (fstack_t) * i);
837c478bd9Sstevel@tonic-gate 	memcpy(ihandle->data[INIT_DATA], phandle->init_data,
847c478bd9Sstevel@tonic-gate 	    (size_t) (sizeof (fstack_t) * i));
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	i = max(phandle->data_size[UINIT_DATA], MIN_VALUES);
877c478bd9Sstevel@tonic-gate 	ihandle->data[UINIT_DATA] = MALLOC(sizeof (fstack_t) * i);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	ihandle->my_space = phandle->my_space;
907c478bd9Sstevel@tonic-gate 	memcpy(ihandle->my_addr, phandle->my_addr, sizeof (ihandle->my_addr));
917c478bd9Sstevel@tonic-gate 	ihandle->parent = parent;
927c478bd9Sstevel@tonic-gate 	ihandle->device = phandle;
937c478bd9Sstevel@tonic-gate 	return (ihandle);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate device_t *
create_phandle(fcode_env_t * env,device_t * parent)977c478bd9Sstevel@tonic-gate create_phandle(fcode_env_t *env, device_t *parent)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	device_t *phandle;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	phandle = MALLOC(sizeof (device_t));
1027c478bd9Sstevel@tonic-gate 	phandle->init_data = MALLOC(sizeof (fstack_t) * MIN_VALUES);
1037c478bd9Sstevel@tonic-gate 	phandle->data_size[INIT_DATA] = 0;
1047c478bd9Sstevel@tonic-gate 	phandle->data_size[UINIT_DATA] = 0;
1057c478bd9Sstevel@tonic-gate 	phandle->parent = parent;
1067c478bd9Sstevel@tonic-gate 	return (phandle);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static void
do_push_package(fcode_env_t * env,device_t * d)1117c478bd9Sstevel@tonic-gate do_push_package(fcode_env_t *env, device_t *d)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	do_previous(env);
1147c478bd9Sstevel@tonic-gate 	do_also(env);
1157c478bd9Sstevel@tonic-gate 	if (d != NULL) {
1167c478bd9Sstevel@tonic-gate 		CONTEXT = (token_t *)(&d->vocabulary);
1177c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_CONTEXT, "CONTEXT:push_package: %s%d/%p/%p\n",
1187c478bd9Sstevel@tonic-gate 		    get_path(env, d), env->order_depth, CONTEXT, env->current);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static void
push_package(fcode_env_t * env)1237c478bd9Sstevel@tonic-gate push_package(fcode_env_t *env)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	device_t *d;
1267c478bd9Sstevel@tonic-gate 	phandle_t ph;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "push-package");
1297c478bd9Sstevel@tonic-gate 	ph = POP(DS);
1307c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, ph);
1317c478bd9Sstevel@tonic-gate 	do_push_package(env, d);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static void
pop_package(fcode_env_t * env)1357c478bd9Sstevel@tonic-gate pop_package(fcode_env_t *env)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	do_previous(env);
1387c478bd9Sstevel@tonic-gate 	do_definitions(env);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static void
interpose(fcode_env_t * env)1427c478bd9Sstevel@tonic-gate interpose(fcode_env_t *env)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	TODO;	/* interpose - not yet implemented */
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate void
activate_device(fcode_env_t * env,device_t * d)1487c478bd9Sstevel@tonic-gate activate_device(fcode_env_t *env, device_t *d)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	env->current_device = d;
1517c478bd9Sstevel@tonic-gate 	do_push_package(env, d);
1527c478bd9Sstevel@tonic-gate 	do_definitions(env);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate void
deactivate_device(fcode_env_t * env,device_t * d)1567c478bd9Sstevel@tonic-gate deactivate_device(fcode_env_t *env, device_t *d)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	env->current_device = d;
1597c478bd9Sstevel@tonic-gate 	do_previous(env);
1607c478bd9Sstevel@tonic-gate 	if (d != NULL) {
1617c478bd9Sstevel@tonic-gate 		CONTEXT = (token_t *)(&d->vocabulary);
1627c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_CONTEXT, "CONTEXT:deactivate_device:"
1637c478bd9Sstevel@tonic-gate 		    " %s%d/%p/%p\n", get_path(env, d), env->order_depth,
1647c478bd9Sstevel@tonic-gate 		    CONTEXT, env->current);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	do_definitions(env);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate void
root_node(fcode_env_t * env)1707c478bd9Sstevel@tonic-gate root_node(fcode_env_t *env)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	do_also(env);
1737c478bd9Sstevel@tonic-gate 	activate_device(env, env->root_node);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate void
child_node(fcode_env_t * env)1777c478bd9Sstevel@tonic-gate child_node(fcode_env_t *env)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	device_t *d;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "child");
1827c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, TOS);
1837c478bd9Sstevel@tonic-gate 	TOS = (fstack_t)d->child;
1847c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, d->child);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate void
peer_node(fcode_env_t * env)1887c478bd9Sstevel@tonic-gate peer_node(fcode_env_t *env)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	device_t *d;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "peer");
1937c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, TOS);
1947c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, d->peer);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate void
new_device(fcode_env_t * env)1987c478bd9Sstevel@tonic-gate new_device(fcode_env_t *env)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	device_t *phandle, *parent;
2017c478bd9Sstevel@tonic-gate 	device_t *peer;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	check_my_self(env, "new-device");
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	parent = MYSELF->device;
2067c478bd9Sstevel@tonic-gate 	phandle = create_phandle(env, parent);
2077c478bd9Sstevel@tonic-gate 	MYSELF = create_ihandle(env, phandle, MYSELF);
2087c478bd9Sstevel@tonic-gate 	activate_device(env, phandle);
2097c478bd9Sstevel@tonic-gate 	if (parent->child) {
2107c478bd9Sstevel@tonic-gate 		/* Insert new child at end of peer list */
2117c478bd9Sstevel@tonic-gate 		for (peer = parent->child; peer->peer; peer = peer->peer)
2127c478bd9Sstevel@tonic-gate 			;
2137c478bd9Sstevel@tonic-gate 		peer->peer = phandle;
2147c478bd9Sstevel@tonic-gate 	} else
2157c478bd9Sstevel@tonic-gate 		parent->child = phandle;	/* First child */
2167c478bd9Sstevel@tonic-gate 	ALLOCATE_PHANDLE(env);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate void
finish_device(fcode_env_t * env)2207c478bd9Sstevel@tonic-gate finish_device(fcode_env_t *env)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	fstack_t *mem;
2237c478bd9Sstevel@tonic-gate 	device_t *my_dev, *parent_dev;
2247c478bd9Sstevel@tonic-gate 	instance_t *parent, *myself = MYSELF;
2257c478bd9Sstevel@tonic-gate 	int  n;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	check_my_self(env, "finish-device");
2287c478bd9Sstevel@tonic-gate 	ASSERT(myself->device);
2297c478bd9Sstevel@tonic-gate 	ASSERT(env->current_device);
2307c478bd9Sstevel@tonic-gate 	n = myself->device->data_size[INIT_DATA];
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * Paranoia.. reserve a little more instance data than we need
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	mem = MALLOC(sizeof (fstack_t) * (n+8));
2367c478bd9Sstevel@tonic-gate 	memcpy(mem, MYSELF->device->init_data, sizeof (fstack_t) * n);
2377c478bd9Sstevel@tonic-gate 	FREE(myself->device->init_data);
2387c478bd9Sstevel@tonic-gate 	my_dev = myself->device;
2397c478bd9Sstevel@tonic-gate 	my_dev->init_data = mem;
2407c478bd9Sstevel@tonic-gate 	parent = MYSELF->parent;
2417c478bd9Sstevel@tonic-gate 	parent_dev = env->current_device->parent;
2427c478bd9Sstevel@tonic-gate 	FREE(MYSELF);
2437c478bd9Sstevel@tonic-gate 	MYSELF = parent;
2447c478bd9Sstevel@tonic-gate 	activate_device(env, parent_dev);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static void
create_internal_value(fcode_env_t * env,char * name,int offset,int token)2487c478bd9Sstevel@tonic-gate create_internal_value(fcode_env_t *env, char *name, int offset, int token)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	header(env, name, strlen(name), 0);
2517c478bd9Sstevel@tonic-gate 	COMPILE_TOKEN(&noop);
2527c478bd9Sstevel@tonic-gate 	EXPOSE_ACF;
2537c478bd9Sstevel@tonic-gate 	if (token) {
2547c478bd9Sstevel@tonic-gate 		SET_TOKEN(token, 0, name, LINK_TO_ACF(env->lastlink));
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 	PUSH(DS, offset);
2577c478bd9Sstevel@tonic-gate 	lcomma(env);
2587c478bd9Sstevel@tonic-gate 	set_internal_value_actions(env);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static void
create_my_self(fcode_env_t * env)2627c478bd9Sstevel@tonic-gate create_my_self(fcode_env_t *env)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	int offset = offsetof(fcode_env_t, my_self);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	create_internal_value(env, "my-self", offset, 0x203);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static void
create_my_space(fcode_env_t * env)2707c478bd9Sstevel@tonic-gate create_my_space(fcode_env_t *env)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	int offset = offsetof(instance_t, my_space);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	create_internal_value(env, "my-space", -offset, 0x103);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate void
my_address(fcode_env_t * env)2787c478bd9Sstevel@tonic-gate my_address(fcode_env_t *env)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	fstack_t *adr_ptr;
2817c478bd9Sstevel@tonic-gate 	uint_t ncells;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-address");
2847c478bd9Sstevel@tonic-gate 	ncells = get_number_of_parent_address_cells(env);
2857c478bd9Sstevel@tonic-gate 	adr_ptr = MYSELF->my_addr;
2867c478bd9Sstevel@tonic-gate 	while (--ncells) {
2877c478bd9Sstevel@tonic-gate 		PUSH(DS, *adr_ptr);
2887c478bd9Sstevel@tonic-gate 		adr_ptr++;
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate void
my_unit(fcode_env_t * env)2937c478bd9Sstevel@tonic-gate my_unit(fcode_env_t *env)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-unit");
2967c478bd9Sstevel@tonic-gate 	my_address(env);
2977c478bd9Sstevel@tonic-gate 	PUSH(DS, MYSELF->my_space);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate static void
my_args(fcode_env_t * env)3017c478bd9Sstevel@tonic-gate my_args(fcode_env_t *env)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-args");
3047c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->my_args);
3057c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->my_args_len);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate int
call_my_parent(fcode_env_t * env,char * method)3097c478bd9Sstevel@tonic-gate call_my_parent(fcode_env_t *env, char *method)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	push_a_string(env, method);
3127c478bd9Sstevel@tonic-gate 	dollar_call_parent(env);
3137c478bd9Sstevel@tonic-gate 	return (env->last_error);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate void
set_args(fcode_env_t * env)3177c478bd9Sstevel@tonic-gate set_args(fcode_env_t *env)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	int args_len;
3207c478bd9Sstevel@tonic-gate 	common_data_t *cdp;
3217c478bd9Sstevel@tonic-gate 	uint_t ncells;
3227c478bd9Sstevel@tonic-gate 	fstack_t *adr_ptr, *adr_ptr1, space;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 4, "set-args");
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	check_my_self(env, "set-args");
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Handle args argument of set-args.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	if (MYSELF->my_args) {
3327c478bd9Sstevel@tonic-gate 		FREE(MYSELF->my_args);
3337c478bd9Sstevel@tonic-gate 		MYSELF->my_args = NULL;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	two_swap(env);
3367c478bd9Sstevel@tonic-gate 	MYSELF->my_args = pop_a_duped_string(env, &args_len);
3377c478bd9Sstevel@tonic-gate 	MYSELF->my_args_len = args_len;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (call_my_parent(env, "decode-unit"))
3407c478bd9Sstevel@tonic-gate 		forth_abort(env, "set-args: decode-unit failed");
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	ncells = get_number_of_parent_address_cells(env);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * Kludge: For GP2, my-space comes from decode-unit hi.address.
3467c478bd9Sstevel@tonic-gate 	 * for PCI, my-space from decode-unit won't have the bus#, so we need
3477c478bd9Sstevel@tonic-gate 	 * to get it from config_address.  Unfortunately, there is no easy
3487c478bd9Sstevel@tonic-gate 	 * way to figure out here which one we're looking at.  We take the
3497c478bd9Sstevel@tonic-gate 	 * expediant of or'ing the two values together.
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	space = POP(DS);	/* pop phys.hi */
3527c478bd9Sstevel@tonic-gate 	if ((cdp = (common_data_t *)env->private) != NULL)
3537c478bd9Sstevel@tonic-gate 		space |= cdp->fc.config_address;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	MYSELF->device->my_space = MYSELF->my_space = space;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	adr_ptr = MYSELF->my_addr;
3587c478bd9Sstevel@tonic-gate 	adr_ptr1 = MYSELF->device->my_addr;
3597c478bd9Sstevel@tonic-gate 	while (--ncells) {
3607c478bd9Sstevel@tonic-gate 		*adr_ptr++ = *adr_ptr1++ = POP(DS);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate void
my_parent(fcode_env_t * env)3657c478bd9Sstevel@tonic-gate my_parent(fcode_env_t *env)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-parent");
3687c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->parent);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate instance_t *
open_instance_chain(fcode_env_t * env,device_t * phandle,int exec)3727c478bd9Sstevel@tonic-gate open_instance_chain(fcode_env_t *env, device_t *phandle, int exec)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	instance_t *parent;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (!phandle)
3777c478bd9Sstevel@tonic-gate 		return (NULL);
3787c478bd9Sstevel@tonic-gate 	parent = open_instance_chain(env, phandle->parent, exec);
3797c478bd9Sstevel@tonic-gate 	return (create_ihandle(env, phandle, parent));
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate void
close_instance_chain(fcode_env_t * env,instance_t * ihandle,int exec)3837c478bd9Sstevel@tonic-gate close_instance_chain(fcode_env_t *env, instance_t *ihandle, int exec)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	instance_t *parent;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	if (ihandle) {
3887c478bd9Sstevel@tonic-gate 		parent = ihandle->parent;
3897c478bd9Sstevel@tonic-gate 		close_instance_chain(env, parent, exec);
3907c478bd9Sstevel@tonic-gate 		if (ihandle->my_args)
3917c478bd9Sstevel@tonic-gate 			FREE(ihandle->my_args);
3927c478bd9Sstevel@tonic-gate 		FREE(ihandle);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate void
begin_package(fcode_env_t * env)3977c478bd9Sstevel@tonic-gate begin_package(fcode_env_t *env)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	fstack_t ok;
4007c478bd9Sstevel@tonic-gate 	char *name;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 6, "begin-package");
4037c478bd9Sstevel@tonic-gate 	two_dup(env);
4047c478bd9Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
4057c478bd9Sstevel@tonic-gate 	find_package(env);
4067c478bd9Sstevel@tonic-gate 	ok = POP(DS);
4077c478bd9Sstevel@tonic-gate 	if (ok) {
4087c478bd9Sstevel@tonic-gate 		PUSH(DS, 0);
4097c478bd9Sstevel@tonic-gate 		PUSH(DS, 0);
4107c478bd9Sstevel@tonic-gate 		rot(env);
4117c478bd9Sstevel@tonic-gate 		open_package(env);
4127c478bd9Sstevel@tonic-gate 		MYSELF = (instance_t *)POP(DS);
4137c478bd9Sstevel@tonic-gate 		check_my_self(env, "begin-package");
4147c478bd9Sstevel@tonic-gate 		new_device(env);
4157c478bd9Sstevel@tonic-gate 		set_args(env);
4167c478bd9Sstevel@tonic-gate 	} else {
4177c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "Package '%s' not found\n", name);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate void
open_package(fcode_env_t * env)4227c478bd9Sstevel@tonic-gate open_package(fcode_env_t *env)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	device_t *phandle;
4257c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
4267c478bd9Sstevel@tonic-gate 	int len;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "open-package");
4297c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, phandle, POP(DS));
4307c478bd9Sstevel@tonic-gate 	ihandle = open_instance_chain(env, phandle, 1);
4317c478bd9Sstevel@tonic-gate 	ihandle->my_args = pop_a_duped_string(env, &len);
4327c478bd9Sstevel@tonic-gate 	ihandle->my_args_len = len;
4337c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)ihandle);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate void
dollar_open_package(fcode_env_t * env)4377c478bd9Sstevel@tonic-gate dollar_open_package(fcode_env_t *env)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	fstack_t ok;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 4, "$open-package");
4427c478bd9Sstevel@tonic-gate 	find_package(env);
4437c478bd9Sstevel@tonic-gate 	ok = POP(DS);
4447c478bd9Sstevel@tonic-gate 	if (ok) {
4457c478bd9Sstevel@tonic-gate 		open_package(env);
4467c478bd9Sstevel@tonic-gate 	} else {
44725cf1a30Sjl 		(void) POP(DS);
44825cf1a30Sjl 		(void) POP(DS);
4497c478bd9Sstevel@tonic-gate 		PUSH(DS, 0);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate void
close_package(fcode_env_t * env)4547c478bd9Sstevel@tonic-gate close_package(fcode_env_t *env)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "close-package");
4597c478bd9Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
4607c478bd9Sstevel@tonic-gate 	close_instance_chain(env, ihandle, 1);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate static void (*find_method_hook)(fcode_env_t *);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate void
set_find_method_hook(fcode_env_t * env,void (* hook)(fcode_env_t *))4667c478bd9Sstevel@tonic-gate set_find_method_hook(fcode_env_t *env, void (*hook)(fcode_env_t *))
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	find_method_hook = hook;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate void
find_method(fcode_env_t * env)4727c478bd9Sstevel@tonic-gate find_method(fcode_env_t *env)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	fstack_t d;
4757c478bd9Sstevel@tonic-gate 	device_t *device;
4767c478bd9Sstevel@tonic-gate 	acf_t acf = 0;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "find-method");
4797c478bd9Sstevel@tonic-gate 	if (find_method_hook) {
4807c478bd9Sstevel@tonic-gate 		(*find_method_hook)(env);
4817c478bd9Sstevel@tonic-gate 		if (TOS)		/* Found it */
4827c478bd9Sstevel@tonic-gate 			return;
4837c478bd9Sstevel@tonic-gate 		POP(DS);
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	d = POP(DS);
4877c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, device, d);
4887c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&device->vocabulary);
4897c478bd9Sstevel@tonic-gate 	acf = voc_find(env);
4907c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)acf);
4917c478bd9Sstevel@tonic-gate 	if (acf) {
4927c478bd9Sstevel@tonic-gate 		PUSH(DS, TRUE);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * 'call-package' Fcode
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate void
call_package(fcode_env_t * env)5007c478bd9Sstevel@tonic-gate call_package(fcode_env_t *env)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	instance_t *ihandle, *saved_myself;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "call-package");
5057c478bd9Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
5067c478bd9Sstevel@tonic-gate 	saved_myself = MYSELF;
5077c478bd9Sstevel@tonic-gate 	MYSELF = ihandle;
5087c478bd9Sstevel@tonic-gate 	execute(env);
5097c478bd9Sstevel@tonic-gate 	MYSELF = saved_myself;
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate void
ihandle_to_phandle(fcode_env_t * env)5137c478bd9Sstevel@tonic-gate ihandle_to_phandle(fcode_env_t *env)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	instance_t *i;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "ihandle>phandle");
5187c478bd9Sstevel@tonic-gate 	i = (instance_t *)TOS;
5197c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, i->device);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate char *
get_package_name(fcode_env_t * env,device_t * d)5237c478bd9Sstevel@tonic-gate get_package_name(fcode_env_t *env, device_t *d)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	char *name;
5267c478bd9Sstevel@tonic-gate 	prop_t *prop;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	prop = lookup_package_property(env, "name", d);
5297c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
5307c478bd9Sstevel@tonic-gate 		name = "<Unnamed>";
5317c478bd9Sstevel@tonic-gate 	} else {
5327c478bd9Sstevel@tonic-gate 		name = (char *)prop->data;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 	return (name);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static char *package_search_path = "/packages:/openprom";
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate device_t *
match_package_path(fcode_env_t * env,char * path)5407c478bd9Sstevel@tonic-gate match_package_path(fcode_env_t *env, char *path)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	device_t *d;
5437c478bd9Sstevel@tonic-gate 	char *name;
5447c478bd9Sstevel@tonic-gate 	int len;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if (*path == '/') {
5477c478bd9Sstevel@tonic-gate 		d = env->root_node->child;
5487c478bd9Sstevel@tonic-gate 		path++;
5497c478bd9Sstevel@tonic-gate 	} else
5507c478bd9Sstevel@tonic-gate 		d = env->current_device;
5517c478bd9Sstevel@tonic-gate 	while (*path != '\0' && d != NULL) {
5527c478bd9Sstevel@tonic-gate 		name = get_package_name(env, d);
5537c478bd9Sstevel@tonic-gate 		len = strlen(name);
5547c478bd9Sstevel@tonic-gate 		if (strncmp(name, path, len) == 0) {
5557c478bd9Sstevel@tonic-gate 			path += len;
5567c478bd9Sstevel@tonic-gate 			if (*path == '\0') {
5577c478bd9Sstevel@tonic-gate 				return (d);
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 			/* skip the '/' */
5607c478bd9Sstevel@tonic-gate 			if (*path++ != '/')
5617c478bd9Sstevel@tonic-gate 				break;
5627c478bd9Sstevel@tonic-gate 			d = d->child;
5637c478bd9Sstevel@tonic-gate 		} else {
5647c478bd9Sstevel@tonic-gate 			d = d->peer;
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	return (NULL);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate device_t *
locate_package(fcode_env_t * env,char * start)5717c478bd9Sstevel@tonic-gate locate_package(fcode_env_t *env, char *start)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	device_t *d;
5747c478bd9Sstevel@tonic-gate 	char *p, *next_p;
5757c478bd9Sstevel@tonic-gate 	char *tpath, *fpath;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if ((d = match_package_path(env, start)) != NULL)
5787c478bd9Sstevel@tonic-gate 		return (d);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * ignore starting '/'
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	if (*start == '/')
5847c478bd9Sstevel@tonic-gate 		*start++;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	fpath = STRDUP(package_search_path);
5877c478bd9Sstevel@tonic-gate 	for (p = fpath; p != NULL; p = next_p) {
5887c478bd9Sstevel@tonic-gate 		if ((next_p = strchr(p, ':')) != NULL)
5897c478bd9Sstevel@tonic-gate 			*next_p++ = '\0';
5907c478bd9Sstevel@tonic-gate 		tpath = MALLOC(strlen(p) + strlen(start) + 2);
591*09e6639bSToomas Soome 		(void) sprintf(tpath, "%s/%s", p, start);
5927c478bd9Sstevel@tonic-gate 		if ((d = match_package_path(env, tpath)) != NULL) {
5937c478bd9Sstevel@tonic-gate 			FREE(fpath);
5947c478bd9Sstevel@tonic-gate 			FREE(tpath);
5957c478bd9Sstevel@tonic-gate 			return (d);
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 		FREE(tpath);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 	FREE(fpath);
6007c478bd9Sstevel@tonic-gate 	return (NULL);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate void
find_package(fcode_env_t * env)6047c478bd9Sstevel@tonic-gate find_package(fcode_env_t *env)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	char *path;
6077c478bd9Sstevel@tonic-gate 	device_t *package;
6087c478bd9Sstevel@tonic-gate 	fstack_t ph = 0;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "find-package");
6117c478bd9Sstevel@tonic-gate 	if ((path = pop_a_duped_string(env, NULL)) != NULL) {
6127c478bd9Sstevel@tonic-gate 		if (strcmp(path, "/") == 0)
6137c478bd9Sstevel@tonic-gate 			package = env->root_node;
6147c478bd9Sstevel@tonic-gate 		else
6157c478bd9Sstevel@tonic-gate 			package = locate_package(env, path);
6167c478bd9Sstevel@tonic-gate 		FREE(path);
6177c478bd9Sstevel@tonic-gate 		REVERT_PHANDLE(env, ph, package);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	PUSH(DS, ph);
6207c478bd9Sstevel@tonic-gate 	if (package)
6217c478bd9Sstevel@tonic-gate 		PUSH(DS, TRUE);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate static void
encode_unit_hack(fcode_env_t * env)6257c478bd9Sstevel@tonic-gate encode_unit_hack(fcode_env_t *env)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	int hi, i;
6287c478bd9Sstevel@tonic-gate 	uint_t ncells = get_number_of_parent_address_cells(env);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncells; i++)
6317c478bd9Sstevel@tonic-gate 		POP(DS);
6327c478bd9Sstevel@tonic-gate 	push_a_string(env, NULL);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate void
dollar_call_method(fcode_env_t * env)6367c478bd9Sstevel@tonic-gate dollar_call_method(fcode_env_t *env)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	instance_t *old_myself;
6397c478bd9Sstevel@tonic-gate 	instance_t *myself;
6407c478bd9Sstevel@tonic-gate 	device_t *device;
6417c478bd9Sstevel@tonic-gate 	char *method;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "$call-method");
6447c478bd9Sstevel@tonic-gate 	check_my_self(env, "$call-method");
6457c478bd9Sstevel@tonic-gate 	old_myself = MYSELF;
6467c478bd9Sstevel@tonic-gate 	myself = (instance_t *)POP(DS);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	method = (char *)DS[-1];
6497c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_CALL_METHOD, "$call_method %s\n", method);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (old_myself && !myself) {
6527c478bd9Sstevel@tonic-gate 		/* We hit the root of our tree */
6537c478bd9Sstevel@tonic-gate 		device = old_myself->device;
6547c478bd9Sstevel@tonic-gate 		return;
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	MYSELF = myself;
6587c478bd9Sstevel@tonic-gate 	check_my_self(env, "$call-method");
6597c478bd9Sstevel@tonic-gate 	device = MYSELF->device;
6607c478bd9Sstevel@tonic-gate 	do_push_package(env, device);
6617c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)device);
6627c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, device);
6637c478bd9Sstevel@tonic-gate 	find_method(env);
6647c478bd9Sstevel@tonic-gate 	if (TOS) {
6657c478bd9Sstevel@tonic-gate 		(void) POP(DS);
6667c478bd9Sstevel@tonic-gate 		execute(env);
6677c478bd9Sstevel@tonic-gate 	} else if (strcmp(method, "encode-unit") == 0) {
6687c478bd9Sstevel@tonic-gate 		encode_unit_hack(env);
6697c478bd9Sstevel@tonic-gate 	} else {
6707c478bd9Sstevel@tonic-gate 		throw_from_fclib(env, 1, "Unimplemented package method: %s%s",
6717c478bd9Sstevel@tonic-gate 		    get_path(env, device), method);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	MYSELF = old_myself;
6747c478bd9Sstevel@tonic-gate 	do_push_package(env, MYSELF->device);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate void
dollar_call_parent(fcode_env_t * env)6787c478bd9Sstevel@tonic-gate dollar_call_parent(fcode_env_t *env)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "$call-parent");
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	check_my_self(env, "$call-parent");
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->parent);
6857c478bd9Sstevel@tonic-gate 	dollar_call_method(env);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate #ifdef DEBUG
6897c478bd9Sstevel@tonic-gate void
current_device(fcode_env_t * env)6907c478bd9Sstevel@tonic-gate current_device(fcode_env_t *env)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&env->current_device);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate char *
get_path(fcode_env_t * env,device_t * d)6967c478bd9Sstevel@tonic-gate get_path(fcode_env_t *env, device_t *d)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	char *pre_path, *name, *path;
6997c478bd9Sstevel@tonic-gate 	int n;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (d->parent)
7027c478bd9Sstevel@tonic-gate 		pre_path = get_path(env, d->parent);
7037c478bd9Sstevel@tonic-gate 	else
7047c478bd9Sstevel@tonic-gate 		pre_path = STRDUP("");
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	name = get_package_name(env, d);
7077c478bd9Sstevel@tonic-gate 	n = strlen(pre_path) + strlen(name) + 1;
7087c478bd9Sstevel@tonic-gate 	path = MALLOC(n);
709*09e6639bSToomas Soome 	(void) strcpy(path, pre_path);
710*09e6639bSToomas Soome 	(void) strcat(path, name);
7117c478bd9Sstevel@tonic-gate 	if (d->child && d->parent)
712*09e6639bSToomas Soome 		(void) strcat(path, "/");
7137c478bd9Sstevel@tonic-gate 	FREE(pre_path);
7147c478bd9Sstevel@tonic-gate 	return (path);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate static void
pwd_dollar(fcode_env_t * env)7187c478bd9Sstevel@tonic-gate pwd_dollar(fcode_env_t *env)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate 	if (env->current_device)
7217c478bd9Sstevel@tonic-gate 		push_a_string(env, get_path(env, env->current_device));
7227c478bd9Sstevel@tonic-gate 	else
7237c478bd9Sstevel@tonic-gate 		push_a_string(env, NULL);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate void
pwd(fcode_env_t * env)7277c478bd9Sstevel@tonic-gate pwd(fcode_env_t *env)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	if (env->current_device) {
7307c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "%s\n",
7317c478bd9Sstevel@tonic-gate 		    get_path(env, env->current_device));
7327c478bd9Sstevel@tonic-gate 	} else {
7337c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate void
do_ls(fcode_env_t * env)7387c478bd9Sstevel@tonic-gate do_ls(fcode_env_t *env)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	device_t *d;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (env->current_device == NULL) {
7437c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
7447c478bd9Sstevel@tonic-gate 		return;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	d = env->current_device->child;
7487c478bd9Sstevel@tonic-gate 	while (d) {
7497c478bd9Sstevel@tonic-gate 		char *name;
7507c478bd9Sstevel@tonic-gate 		fstack_t ph;
7517c478bd9Sstevel@tonic-gate 		name = get_package_name(env, d);
7527c478bd9Sstevel@tonic-gate 		REVERT_PHANDLE(env, ph, d);
7537c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx %s\n", (uint64_t)ph, name);
7547c478bd9Sstevel@tonic-gate 		d = d->peer;
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate void
paren_cd(fcode_env_t * env)7597c478bd9Sstevel@tonic-gate paren_cd(fcode_env_t *env)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	char *str;
7627c478bd9Sstevel@tonic-gate 	device_t *p;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	str = pop_a_string(env, NULL);
7657c478bd9Sstevel@tonic-gate 	if (strcmp(str, "/") == 0) {
7667c478bd9Sstevel@tonic-gate 		root_node(env);
7677c478bd9Sstevel@tonic-gate 		return;
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (env->current_device == NULL) {
7717c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
7727c478bd9Sstevel@tonic-gate 		return;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (strcmp(str, "..") == 0)
7767c478bd9Sstevel@tonic-gate 		p = env->current_device->parent;
7777c478bd9Sstevel@tonic-gate 	else {
7787c478bd9Sstevel@tonic-gate 		device_t *n = env->current_device->child;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		p = NULL;
7817c478bd9Sstevel@tonic-gate 		while (n) {
7827c478bd9Sstevel@tonic-gate 			char *name;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 			name = get_package_name(env, n);
7857c478bd9Sstevel@tonic-gate 			if (strcmp(name, str) == 0) {
7867c478bd9Sstevel@tonic-gate 				p = n;
7877c478bd9Sstevel@tonic-gate 				break;
7887c478bd9Sstevel@tonic-gate 			}
7897c478bd9Sstevel@tonic-gate 			n = n->peer;
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (p) {
7947c478bd9Sstevel@tonic-gate 		activate_device(env, p);
7957c478bd9Sstevel@tonic-gate 	} else {
7967c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No such node: %s\n", str);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate void
do_cd(fcode_env_t * env)8017c478bd9Sstevel@tonic-gate do_cd(fcode_env_t *env)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate 	parse_word(env);
8047c478bd9Sstevel@tonic-gate 	paren_cd(env);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate void
do_unselect_dev(fcode_env_t * env)8087c478bd9Sstevel@tonic-gate do_unselect_dev(fcode_env_t *env)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	check_my_self(env, "unselect-dev");
8117c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF);
8127c478bd9Sstevel@tonic-gate 	close_package(env);
8137c478bd9Sstevel@tonic-gate 	deactivate_device(env, NULL);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate void
do_select_dev(fcode_env_t * env)8177c478bd9Sstevel@tonic-gate do_select_dev(fcode_env_t *env)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	PUSH(DS, 0);
8207c478bd9Sstevel@tonic-gate 	PUSH(DS, 0);
8217c478bd9Sstevel@tonic-gate 	two_swap(env);
8227c478bd9Sstevel@tonic-gate 	dollar_open_package(env);
8237c478bd9Sstevel@tonic-gate 	if (TOS) {
8247c478bd9Sstevel@tonic-gate 		MYSELF = (instance_t *)POP(DS);
8257c478bd9Sstevel@tonic-gate 		check_my_self(env, "select-dev");
8267c478bd9Sstevel@tonic-gate 		activate_device(env, MYSELF->device);
8277c478bd9Sstevel@tonic-gate 	} else {
8287c478bd9Sstevel@tonic-gate 		drop(env);
8297c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "Can't open package\n");
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate void
device_end(fcode_env_t * env)8347c478bd9Sstevel@tonic-gate device_end(fcode_env_t *env)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	if (env->current_device) {
8377c478bd9Sstevel@tonic-gate 		deactivate_device(env, NULL);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate void
end_package(fcode_env_t * env)8427c478bd9Sstevel@tonic-gate end_package(fcode_env_t *env)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	finish_device(env);
8457c478bd9Sstevel@tonic-gate 	close_instance_chain(env, MYSELF, 0);
8467c478bd9Sstevel@tonic-gate 	device_end(env);
8477c478bd9Sstevel@tonic-gate 	MYSELF = NULL;
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate void
exec_parent_method(fcode_env_t * env)8517c478bd9Sstevel@tonic-gate exec_parent_method(fcode_env_t *env)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	instance_t *old_myself;
8547c478bd9Sstevel@tonic-gate 	instance_t *myself;
8557c478bd9Sstevel@tonic-gate 	device_t *device;
8567c478bd9Sstevel@tonic-gate 	char *method;
8577c478bd9Sstevel@tonic-gate 	fstack_t d;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	check_my_self(env, "exec-parent-method");
8607c478bd9Sstevel@tonic-gate 	old_myself = MYSELF;
8617c478bd9Sstevel@tonic-gate 	MYSELF = MYSELF->parent;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	method = (char *)DS[-1];
8647c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_FIND_FCODE, "exec_parent_method: '%s'\n", method);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	check_my_self(env, "exec-parent-method");
8677c478bd9Sstevel@tonic-gate 	device = MYSELF->device;
8687c478bd9Sstevel@tonic-gate 	do_push_package(env, device);
8697c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)device);
8707c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, device);
8717c478bd9Sstevel@tonic-gate 	find_method(env);
8727c478bd9Sstevel@tonic-gate 	d = POP(DS);
8737c478bd9Sstevel@tonic-gate 	if (d) {
8747c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_FIND_FCODE, "exec-parent-method: '%s'/%x"
8757c478bd9Sstevel@tonic-gate 		    " execute\n", method, (int)TOS);
8767c478bd9Sstevel@tonic-gate 		execute(env);
8777c478bd9Sstevel@tonic-gate 		PUSH(DS, TRUE);
8787c478bd9Sstevel@tonic-gate 	} else {
8797c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_FIND_FCODE, "exec-parent-method: '%s'"
8807c478bd9Sstevel@tonic-gate 		    " not found\n", method);
8817c478bd9Sstevel@tonic-gate 		PUSH(DS, FALSE);
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 	MYSELF = old_myself;
8847c478bd9Sstevel@tonic-gate 	do_push_package(env, MYSELF->device);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate void
dump_device(fcode_env_t * env)8887c478bd9Sstevel@tonic-gate dump_device(fcode_env_t *env)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	device_t *phandle;
8917c478bd9Sstevel@tonic-gate 	int i;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, phandle, POP(DS));
8947c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Node:      %p\n", phandle);
8957c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Parent:  (%8p) %p\n",
8967c478bd9Sstevel@tonic-gate 	    &phandle->parent, phandle->parent);
8977c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Child:   (%8p) %p\n",
8987c478bd9Sstevel@tonic-gate 	    &phandle->child, phandle->child);
8997c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Peer:    (%8p) %p\n",
9007c478bd9Sstevel@tonic-gate 	    &phandle->peer, phandle->peer);
9017c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Private: (%8p) %p\n",
9027c478bd9Sstevel@tonic-gate 	    &phandle->private, phandle->private);
9037c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Props:   (%8p) %p\n",
9047c478bd9Sstevel@tonic-gate 	    &phandle->properties, phandle->properties);
9057c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Voc:     (%8p) %p\n",
9067c478bd9Sstevel@tonic-gate 	    &phandle->vocabulary, phandle->vocabulary);
9077c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  sizes:   (%8p) %d %d\n",
9087c478bd9Sstevel@tonic-gate 	    &phandle->data_size,
9097c478bd9Sstevel@tonic-gate 	    phandle->data_size[INIT_DATA],
9107c478bd9Sstevel@tonic-gate 	    phandle->data_size[UINIT_DATA]);
9117c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_space: %x\n", phandle->my_space);
9127c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_addr :");
9137c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MY_ADDR; i++)
9147c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, " %x", (int)phandle->my_addr[i]);
9157c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "\n");
9167c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  data:    (%8p)\n", phandle->init_data);
9177c478bd9Sstevel@tonic-gate 	for (i = 0; i < phandle->data_size[INIT_DATA]; i++) {
9187c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
9197c478bd9Sstevel@tonic-gate 		    &phandle->init_data[i], phandle->init_data[i]);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate void
dump_instance(fcode_env_t * env)9247c478bd9Sstevel@tonic-gate dump_instance(fcode_env_t *env)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate 	int i;
9277c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
9307c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Ihandle:      %p\n", ihandle);
9317c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Parent:  (%8p) %p\n",
9327c478bd9Sstevel@tonic-gate 	    &ihandle->parent, ihandle->parent);
9337c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Device:  (%8p) %p\n",
9347c478bd9Sstevel@tonic-gate 	    &ihandle->device, ihandle->device);
9357c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  args:     '%s'\n",
9367c478bd9Sstevel@tonic-gate 	    ((ihandle->my_args) ? ihandle->my_args : ""));
9377c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my-space: %x\n", ihandle->my_space);
9387c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_addr :");
9397c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MY_ADDR; i++)
9407c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, " %x", (int)ihandle->my_addr[i]);
9417c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "\n");
9427c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  sizes:   %d %d\n",
9437c478bd9Sstevel@tonic-gate 	    ihandle->device->data_size[INIT_DATA],
9447c478bd9Sstevel@tonic-gate 	    ihandle->device->data_size[UINIT_DATA]);
9457c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  data:    (%8p) %x %x\n",
9467c478bd9Sstevel@tonic-gate 	    ihandle->data, ihandle->data[0], ihandle->data[1]);
9477c478bd9Sstevel@tonic-gate 	if (ihandle->device->data_size[INIT_DATA]) {
9487c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, "  Initialised:\n");
9497c478bd9Sstevel@tonic-gate 		for (i = 0; i < ihandle->device->data_size[INIT_DATA]; i++) {
9507c478bd9Sstevel@tonic-gate 			log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
9517c478bd9Sstevel@tonic-gate 			    &ihandle->data[INIT_DATA][i],
9527c478bd9Sstevel@tonic-gate 			    ihandle->data[INIT_DATA][i]);
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	if (ihandle->device->data_size[INIT_DATA]) {
9567c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, "  UnInitialised:\n");
9577c478bd9Sstevel@tonic-gate 		for (i = 0; i < ihandle->device->data_size[UINIT_DATA]; i++) {
9587c478bd9Sstevel@tonic-gate 			log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
9597c478bd9Sstevel@tonic-gate 			    &ihandle->data[UINIT_DATA][i],
9607c478bd9Sstevel@tonic-gate 			    ihandle->data[UINIT_DATA][i]);
9617c478bd9Sstevel@tonic-gate 		}
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate #endif
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate #pragma init(_init)
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate #ifdef CONVERT_HANDLES
9707c478bd9Sstevel@tonic-gate static device_t	*
safe_convert_phandle(fcode_env_t * env,fstack_t d)9717c478bd9Sstevel@tonic-gate safe_convert_phandle(fcode_env_t *env, fstack_t d)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	return ((device_t *)d);
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate static fstack_t
safe_revert_phandle(fcode_env_t * env,device_t * d)9777c478bd9Sstevel@tonic-gate safe_revert_phandle(fcode_env_t *env, device_t *d)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	return ((fstack_t)d);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate static void
safe_allocate_phandle(fcode_env_t * env)9837c478bd9Sstevel@tonic-gate safe_allocate_phandle(fcode_env_t *env)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate #endif
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate static void
_init(void)9907c478bd9Sstevel@tonic-gate _init(void)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
9937c478bd9Sstevel@tonic-gate 	char *name = "/";
9947c478bd9Sstevel@tonic-gate 	device_t *d;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	ASSERT(env);
9977c478bd9Sstevel@tonic-gate 	NOTICE;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate #ifdef CONVERT_HANDLES
10007c478bd9Sstevel@tonic-gate 	env->convert_phandle = safe_convert_phandle;
10017c478bd9Sstevel@tonic-gate 	env->revert_phandle = safe_revert_phandle;
10027c478bd9Sstevel@tonic-gate 	env->allocate_phandle = safe_allocate_phandle;
10037c478bd9Sstevel@tonic-gate #endif
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/* build the root node */
10067c478bd9Sstevel@tonic-gate 	d = create_phandle(env, NULL);
10077c478bd9Sstevel@tonic-gate 	env->current_device = d;
10087c478bd9Sstevel@tonic-gate 	env->root_node = d;
10097c478bd9Sstevel@tonic-gate 	push_a_string(env, name);
10107c478bd9Sstevel@tonic-gate 	device_name(env);
10117c478bd9Sstevel@tonic-gate 	env->current_device = NULL;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	create_my_self(env);
10147c478bd9Sstevel@tonic-gate 	create_my_space(env);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	P1275(0x102, 0,		"my-address",		my_address);
10177c478bd9Sstevel@tonic-gate 	/* Fcode 0x103 "my-space" is created using create_internal_value */
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	P1275(0x11f, 0,		"new-device",		new_device);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	P1275(0x127, 0,		"finish-device",	finish_device);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	FCODE(0x129, 0,		"push-package",		push_package);
10247c478bd9Sstevel@tonic-gate 	FCODE(0x12a, 0,		"pop-package",		pop_package);
10257c478bd9Sstevel@tonic-gate 	FCODE(0x12b, 0,		"interpose",		interpose);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	P1275(0x202, 0,		"my-args",		my_args);
10287c478bd9Sstevel@tonic-gate 	/* Fcode 0x203 "my-self" is created using create_internal_value */
10297c478bd9Sstevel@tonic-gate 	P1275(0x204, 0,		"find-package",		find_package);
10307c478bd9Sstevel@tonic-gate 	P1275(0x205, 0,		"open-package",		open_package);
10317c478bd9Sstevel@tonic-gate 	P1275(0x206, 0,		"close-package",	close_package);
10327c478bd9Sstevel@tonic-gate 	P1275(0x207, 0,		"find-method",		find_method);
10337c478bd9Sstevel@tonic-gate 	P1275(0x208, 0,		"call-package",		call_package);
10347c478bd9Sstevel@tonic-gate 	P1275(0x209, 0,		"$call-parent",		dollar_call_parent);
10357c478bd9Sstevel@tonic-gate 	P1275(0x20a, 0,		"my-parent",		my_parent);
10367c478bd9Sstevel@tonic-gate 	P1275(0x20b, 0,		"ihandle>phandle",	ihandle_to_phandle);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	P1275(0x20d, 0,		"my-unit",		my_unit);
10397c478bd9Sstevel@tonic-gate 	P1275(0x20e, 0,		"$call-method",		dollar_call_method);
10407c478bd9Sstevel@tonic-gate 	P1275(0x20f, 0,		"$open-package",	dollar_open_package);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	P1275(0x23b, 0,		"child",		child_node);
10437c478bd9Sstevel@tonic-gate 	P1275(0x23c, 0,		"peer",			peer_node);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	P1275(0x23f, 0,		"set-args",		set_args);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"root-node",		root_node);
10487c478bd9Sstevel@tonic-gate 	FORTH(0,		"current-device",	current_device);
10497c478bd9Sstevel@tonic-gate 	FORTH(0,		"pwd$",			pwd_dollar);
10507c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"pwd",			pwd);
10517c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"ls",			do_ls);
10527c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"(cd)",			paren_cd);
10537c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"cd",			do_cd);
10547c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"device-end",		device_end);
10557c478bd9Sstevel@tonic-gate 	FORTH(0,		"select-dev",		do_select_dev);
10567c478bd9Sstevel@tonic-gate 	FORTH(0,		"unselect-dev",		do_unselect_dev);
10577c478bd9Sstevel@tonic-gate 	FORTH(0,		"begin-package",	begin_package);
10587c478bd9Sstevel@tonic-gate 	FORTH(0,		"end-package",		end_package);
10597c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-device",		dump_device);
10607c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-instance",	dump_instance);
10617c478bd9Sstevel@tonic-gate 	FORTH(0,		"exec-parent-method",	exec_parent_method);
10627c478bd9Sstevel@tonic-gate }
1063