xref: /titanic_53/usr/src/lib/efcode/engine/package.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <stddef.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <fcode/private.h>
35*7c478bd9Sstevel@tonic-gate #include <fcode/log.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #define	MIN_VALUES	100
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static void
42*7c478bd9Sstevel@tonic-gate check_my_self(fcode_env_t *env, char *fn)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	if (!MYSELF)
45*7c478bd9Sstevel@tonic-gate 		forth_abort(env, "%s: MYSELF is NULL", fn);
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate uint_t
49*7c478bd9Sstevel@tonic-gate get_number_of_parent_address_cells(fcode_env_t *env)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	uint_t ncells;
52*7c478bd9Sstevel@tonic-gate 	device_t *d;
53*7c478bd9Sstevel@tonic-gate 	static char func_name[] = "get_number_of_parent_address_cells";
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (MYSELF == NULL)	/* Kludge for testing */
56*7c478bd9Sstevel@tonic-gate 		return (2);
57*7c478bd9Sstevel@tonic-gate 	d = MYSELF->device;
58*7c478bd9Sstevel@tonic-gate 	ncells = d->parent_adr_cells;
59*7c478bd9Sstevel@tonic-gate 	if (ncells == 0) {
60*7c478bd9Sstevel@tonic-gate 		ncells = get_default_intprop(env, "#address-cells", d->parent,
61*7c478bd9Sstevel@tonic-gate 		    2);
62*7c478bd9Sstevel@tonic-gate 		if (ncells > MAX_MY_ADDR) {
63*7c478bd9Sstevel@tonic-gate 			log_message(MSG_ERROR, "%s: %s:"
64*7c478bd9Sstevel@tonic-gate 			    " ncells (%d) > MAX_MY_ADDR (%d)\n", func_name,
65*7c478bd9Sstevel@tonic-gate 			    get_path(env, d->parent), ncells, MAX_MY_ADDR);
66*7c478bd9Sstevel@tonic-gate 			ncells = MAX_MY_ADDR;
67*7c478bd9Sstevel@tonic-gate 		}
68*7c478bd9Sstevel@tonic-gate 		d->parent_adr_cells = ncells;
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 	return (ncells);
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate instance_t *
74*7c478bd9Sstevel@tonic-gate create_ihandle(fcode_env_t *env, device_t *phandle, instance_t *parent)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
77*7c478bd9Sstevel@tonic-gate 	int i;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	ihandle = MALLOC(sizeof (instance_t));
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	i = max(phandle->data_size[INIT_DATA], MIN_VALUES);
82*7c478bd9Sstevel@tonic-gate 	ihandle->data[INIT_DATA] = MALLOC(sizeof (fstack_t) * i);
83*7c478bd9Sstevel@tonic-gate 	memcpy(ihandle->data[INIT_DATA], phandle->init_data,
84*7c478bd9Sstevel@tonic-gate 	    (size_t) (sizeof (fstack_t) * i));
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	i = max(phandle->data_size[UINIT_DATA], MIN_VALUES);
87*7c478bd9Sstevel@tonic-gate 	ihandle->data[UINIT_DATA] = MALLOC(sizeof (fstack_t) * i);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	ihandle->my_space = phandle->my_space;
90*7c478bd9Sstevel@tonic-gate 	memcpy(ihandle->my_addr, phandle->my_addr, sizeof (ihandle->my_addr));
91*7c478bd9Sstevel@tonic-gate 	ihandle->parent = parent;
92*7c478bd9Sstevel@tonic-gate 	ihandle->device = phandle;
93*7c478bd9Sstevel@tonic-gate 	return (ihandle);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate device_t *
97*7c478bd9Sstevel@tonic-gate create_phandle(fcode_env_t *env, device_t *parent)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	device_t *phandle;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	phandle = MALLOC(sizeof (device_t));
102*7c478bd9Sstevel@tonic-gate 	phandle->init_data = MALLOC(sizeof (fstack_t) * MIN_VALUES);
103*7c478bd9Sstevel@tonic-gate 	phandle->data_size[INIT_DATA] = 0;
104*7c478bd9Sstevel@tonic-gate 	phandle->data_size[UINIT_DATA] = 0;
105*7c478bd9Sstevel@tonic-gate 	phandle->parent = parent;
106*7c478bd9Sstevel@tonic-gate 	return (phandle);
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static void
111*7c478bd9Sstevel@tonic-gate do_push_package(fcode_env_t *env, device_t *d)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	do_previous(env);
114*7c478bd9Sstevel@tonic-gate 	do_also(env);
115*7c478bd9Sstevel@tonic-gate 	if (d != NULL) {
116*7c478bd9Sstevel@tonic-gate 		CONTEXT = (token_t *)(&d->vocabulary);
117*7c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_CONTEXT, "CONTEXT:push_package: %s%d/%p/%p\n",
118*7c478bd9Sstevel@tonic-gate 		    get_path(env, d), env->order_depth, CONTEXT, env->current);
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate static void
123*7c478bd9Sstevel@tonic-gate push_package(fcode_env_t *env)
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	device_t *d;
126*7c478bd9Sstevel@tonic-gate 	phandle_t ph;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "push-package");
129*7c478bd9Sstevel@tonic-gate 	ph = POP(DS);
130*7c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, ph);
131*7c478bd9Sstevel@tonic-gate 	do_push_package(env, d);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate static void
135*7c478bd9Sstevel@tonic-gate pop_package(fcode_env_t *env)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	do_previous(env);
138*7c478bd9Sstevel@tonic-gate 	do_definitions(env);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate static void
142*7c478bd9Sstevel@tonic-gate interpose(fcode_env_t *env)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	TODO;	/* interpose - not yet implemented */
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate void
148*7c478bd9Sstevel@tonic-gate activate_device(fcode_env_t *env, device_t *d)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	env->current_device = d;
151*7c478bd9Sstevel@tonic-gate 	do_push_package(env, d);
152*7c478bd9Sstevel@tonic-gate 	do_definitions(env);
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate void
156*7c478bd9Sstevel@tonic-gate deactivate_device(fcode_env_t *env, device_t *d)
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate 	env->current_device = d;
159*7c478bd9Sstevel@tonic-gate 	do_previous(env);
160*7c478bd9Sstevel@tonic-gate 	if (d != NULL) {
161*7c478bd9Sstevel@tonic-gate 		CONTEXT = (token_t *)(&d->vocabulary);
162*7c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_CONTEXT, "CONTEXT:deactivate_device:"
163*7c478bd9Sstevel@tonic-gate 		    " %s%d/%p/%p\n", get_path(env, d), env->order_depth,
164*7c478bd9Sstevel@tonic-gate 		    CONTEXT, env->current);
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 	do_definitions(env);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  * Starfire hack to set '/' device_type to 'upa'
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
173*7c478bd9Sstevel@tonic-gate static void
174*7c478bd9Sstevel@tonic-gate starfire_hack(fcode_env_t *env)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	char platform[100];
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	sysinfo(SI_PLATFORM, platform, sizeof (platform));
179*7c478bd9Sstevel@tonic-gate 	if (strcmp(platform, "SUNW,Ultra-Enterprise-10000") == 0 &&
180*7c478bd9Sstevel@tonic-gate 	    find_property(env->root_node, "device_type") == NULL) {
181*7c478bd9Sstevel@tonic-gate 		create_string_prop(env, "device_type", "upa");
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate void
186*7c478bd9Sstevel@tonic-gate root_node(fcode_env_t *env)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	do_also(env);
189*7c478bd9Sstevel@tonic-gate 	activate_device(env, env->root_node);
190*7c478bd9Sstevel@tonic-gate 	starfire_hack(env);
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate void
194*7c478bd9Sstevel@tonic-gate child_node(fcode_env_t *env)
195*7c478bd9Sstevel@tonic-gate {
196*7c478bd9Sstevel@tonic-gate 	device_t *d;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "child");
199*7c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, TOS);
200*7c478bd9Sstevel@tonic-gate 	TOS = (fstack_t)d->child;
201*7c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, d->child);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate void
205*7c478bd9Sstevel@tonic-gate peer_node(fcode_env_t *env)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	device_t *d;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "peer");
210*7c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, d, TOS);
211*7c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, d->peer);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate void
215*7c478bd9Sstevel@tonic-gate new_device(fcode_env_t *env)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	device_t *phandle, *parent;
218*7c478bd9Sstevel@tonic-gate 	device_t *peer;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "new-device");
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	parent = MYSELF->device;
223*7c478bd9Sstevel@tonic-gate 	phandle = create_phandle(env, parent);
224*7c478bd9Sstevel@tonic-gate 	MYSELF = create_ihandle(env, phandle, MYSELF);
225*7c478bd9Sstevel@tonic-gate 	activate_device(env, phandle);
226*7c478bd9Sstevel@tonic-gate 	if (parent->child) {
227*7c478bd9Sstevel@tonic-gate 		/* Insert new child at end of peer list */
228*7c478bd9Sstevel@tonic-gate 		for (peer = parent->child; peer->peer; peer = peer->peer)
229*7c478bd9Sstevel@tonic-gate 			;
230*7c478bd9Sstevel@tonic-gate 		peer->peer = phandle;
231*7c478bd9Sstevel@tonic-gate 	} else
232*7c478bd9Sstevel@tonic-gate 		parent->child = phandle;	/* First child */
233*7c478bd9Sstevel@tonic-gate 	ALLOCATE_PHANDLE(env);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate void
237*7c478bd9Sstevel@tonic-gate finish_device(fcode_env_t *env)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	fstack_t *mem;
240*7c478bd9Sstevel@tonic-gate 	device_t *my_dev, *parent_dev;
241*7c478bd9Sstevel@tonic-gate 	instance_t *parent, *myself = MYSELF;
242*7c478bd9Sstevel@tonic-gate 	int  n;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "finish-device");
245*7c478bd9Sstevel@tonic-gate 	ASSERT(myself->device);
246*7c478bd9Sstevel@tonic-gate 	ASSERT(env->current_device);
247*7c478bd9Sstevel@tonic-gate 	n = myself->device->data_size[INIT_DATA];
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	/*
250*7c478bd9Sstevel@tonic-gate 	 * Paranoia.. reserve a little more instance data than we need
251*7c478bd9Sstevel@tonic-gate 	 */
252*7c478bd9Sstevel@tonic-gate 	mem = MALLOC(sizeof (fstack_t) * (n+8));
253*7c478bd9Sstevel@tonic-gate 	memcpy(mem, MYSELF->device->init_data, sizeof (fstack_t) * n);
254*7c478bd9Sstevel@tonic-gate 	FREE(myself->device->init_data);
255*7c478bd9Sstevel@tonic-gate 	my_dev = myself->device;
256*7c478bd9Sstevel@tonic-gate 	my_dev->init_data = mem;
257*7c478bd9Sstevel@tonic-gate 	parent = MYSELF->parent;
258*7c478bd9Sstevel@tonic-gate 	parent_dev = env->current_device->parent;
259*7c478bd9Sstevel@tonic-gate 	FREE(MYSELF);
260*7c478bd9Sstevel@tonic-gate 	MYSELF = parent;
261*7c478bd9Sstevel@tonic-gate 	activate_device(env, parent_dev);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate static void
265*7c478bd9Sstevel@tonic-gate create_internal_value(fcode_env_t *env, char *name, int offset, int token)
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate 	header(env, name, strlen(name), 0);
268*7c478bd9Sstevel@tonic-gate 	COMPILE_TOKEN(&noop);
269*7c478bd9Sstevel@tonic-gate 	EXPOSE_ACF;
270*7c478bd9Sstevel@tonic-gate 	if (token) {
271*7c478bd9Sstevel@tonic-gate 		SET_TOKEN(token, 0, name, LINK_TO_ACF(env->lastlink));
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 	PUSH(DS, offset);
274*7c478bd9Sstevel@tonic-gate 	lcomma(env);
275*7c478bd9Sstevel@tonic-gate 	set_internal_value_actions(env);
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate static void
279*7c478bd9Sstevel@tonic-gate create_my_self(fcode_env_t *env)
280*7c478bd9Sstevel@tonic-gate {
281*7c478bd9Sstevel@tonic-gate 	int offset = offsetof(fcode_env_t, my_self);
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	create_internal_value(env, "my-self", offset, 0x203);
284*7c478bd9Sstevel@tonic-gate }
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate static void
287*7c478bd9Sstevel@tonic-gate create_my_space(fcode_env_t *env)
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate 	int offset = offsetof(instance_t, my_space);
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	create_internal_value(env, "my-space", -offset, 0x103);
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate void
295*7c478bd9Sstevel@tonic-gate my_address(fcode_env_t *env)
296*7c478bd9Sstevel@tonic-gate {
297*7c478bd9Sstevel@tonic-gate 	fstack_t *adr_ptr;
298*7c478bd9Sstevel@tonic-gate 	uint_t ncells;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-address");
301*7c478bd9Sstevel@tonic-gate 	ncells = get_number_of_parent_address_cells(env);
302*7c478bd9Sstevel@tonic-gate 	adr_ptr = MYSELF->my_addr;
303*7c478bd9Sstevel@tonic-gate 	while (--ncells) {
304*7c478bd9Sstevel@tonic-gate 		PUSH(DS, *adr_ptr);
305*7c478bd9Sstevel@tonic-gate 		adr_ptr++;
306*7c478bd9Sstevel@tonic-gate 	}
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate void
310*7c478bd9Sstevel@tonic-gate my_unit(fcode_env_t *env)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-unit");
313*7c478bd9Sstevel@tonic-gate 	my_address(env);
314*7c478bd9Sstevel@tonic-gate 	PUSH(DS, MYSELF->my_space);
315*7c478bd9Sstevel@tonic-gate }
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate static void
318*7c478bd9Sstevel@tonic-gate my_args(fcode_env_t *env)
319*7c478bd9Sstevel@tonic-gate {
320*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-args");
321*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->my_args);
322*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->my_args_len);
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate int
326*7c478bd9Sstevel@tonic-gate call_my_parent(fcode_env_t *env, char *method)
327*7c478bd9Sstevel@tonic-gate {
328*7c478bd9Sstevel@tonic-gate 	push_a_string(env, method);
329*7c478bd9Sstevel@tonic-gate 	dollar_call_parent(env);
330*7c478bd9Sstevel@tonic-gate 	return (env->last_error);
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate void
334*7c478bd9Sstevel@tonic-gate set_args(fcode_env_t *env)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	int args_len;
337*7c478bd9Sstevel@tonic-gate 	common_data_t *cdp;
338*7c478bd9Sstevel@tonic-gate 	uint_t ncells;
339*7c478bd9Sstevel@tonic-gate 	fstack_t *adr_ptr, *adr_ptr1, space;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 4, "set-args");
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "set-args");
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	/*
346*7c478bd9Sstevel@tonic-gate 	 * Handle args argument of set-args.
347*7c478bd9Sstevel@tonic-gate 	 */
348*7c478bd9Sstevel@tonic-gate 	if (MYSELF->my_args) {
349*7c478bd9Sstevel@tonic-gate 		FREE(MYSELF->my_args);
350*7c478bd9Sstevel@tonic-gate 		MYSELF->my_args = NULL;
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate 	two_swap(env);
353*7c478bd9Sstevel@tonic-gate 	MYSELF->my_args = pop_a_duped_string(env, &args_len);
354*7c478bd9Sstevel@tonic-gate 	MYSELF->my_args_len = args_len;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	if (call_my_parent(env, "decode-unit"))
357*7c478bd9Sstevel@tonic-gate 		forth_abort(env, "set-args: decode-unit failed");
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	ncells = get_number_of_parent_address_cells(env);
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	/*
362*7c478bd9Sstevel@tonic-gate 	 * Kludge: For GP2, my-space comes from decode-unit hi.address.
363*7c478bd9Sstevel@tonic-gate 	 * for PCI, my-space from decode-unit won't have the bus#, so we need
364*7c478bd9Sstevel@tonic-gate 	 * to get it from config_address.  Unfortunately, there is no easy
365*7c478bd9Sstevel@tonic-gate 	 * way to figure out here which one we're looking at.  We take the
366*7c478bd9Sstevel@tonic-gate 	 * expediant of or'ing the two values together.
367*7c478bd9Sstevel@tonic-gate 	 */
368*7c478bd9Sstevel@tonic-gate 	space = POP(DS);	/* pop phys.hi */
369*7c478bd9Sstevel@tonic-gate 	if ((cdp = (common_data_t *)env->private) != NULL)
370*7c478bd9Sstevel@tonic-gate 		space |= cdp->fc.config_address;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	MYSELF->device->my_space = MYSELF->my_space = space;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	adr_ptr = MYSELF->my_addr;
375*7c478bd9Sstevel@tonic-gate 	adr_ptr1 = MYSELF->device->my_addr;
376*7c478bd9Sstevel@tonic-gate 	while (--ncells) {
377*7c478bd9Sstevel@tonic-gate 		*adr_ptr++ = *adr_ptr1++ = POP(DS);
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate }
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate void
382*7c478bd9Sstevel@tonic-gate my_parent(fcode_env_t *env)
383*7c478bd9Sstevel@tonic-gate {
384*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "my-parent");
385*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->parent);
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate instance_t *
389*7c478bd9Sstevel@tonic-gate open_instance_chain(fcode_env_t *env, device_t *phandle, int exec)
390*7c478bd9Sstevel@tonic-gate {
391*7c478bd9Sstevel@tonic-gate 	instance_t *parent;
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	if (!phandle)
394*7c478bd9Sstevel@tonic-gate 		return (NULL);
395*7c478bd9Sstevel@tonic-gate 	parent = open_instance_chain(env, phandle->parent, exec);
396*7c478bd9Sstevel@tonic-gate 	return (create_ihandle(env, phandle, parent));
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate void
400*7c478bd9Sstevel@tonic-gate close_instance_chain(fcode_env_t *env, instance_t *ihandle, int exec)
401*7c478bd9Sstevel@tonic-gate {
402*7c478bd9Sstevel@tonic-gate 	instance_t *parent;
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	if (ihandle) {
405*7c478bd9Sstevel@tonic-gate 		parent = ihandle->parent;
406*7c478bd9Sstevel@tonic-gate 		close_instance_chain(env, parent, exec);
407*7c478bd9Sstevel@tonic-gate 		if (ihandle->my_args)
408*7c478bd9Sstevel@tonic-gate 			FREE(ihandle->my_args);
409*7c478bd9Sstevel@tonic-gate 		FREE(ihandle);
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate void
414*7c478bd9Sstevel@tonic-gate begin_package(fcode_env_t *env)
415*7c478bd9Sstevel@tonic-gate {
416*7c478bd9Sstevel@tonic-gate 	fstack_t ok;
417*7c478bd9Sstevel@tonic-gate 	char *name;
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 6, "begin-package");
420*7c478bd9Sstevel@tonic-gate 	two_dup(env);
421*7c478bd9Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
422*7c478bd9Sstevel@tonic-gate 	find_package(env);
423*7c478bd9Sstevel@tonic-gate 	ok = POP(DS);
424*7c478bd9Sstevel@tonic-gate 	if (ok) {
425*7c478bd9Sstevel@tonic-gate 		PUSH(DS, 0);
426*7c478bd9Sstevel@tonic-gate 		PUSH(DS, 0);
427*7c478bd9Sstevel@tonic-gate 		rot(env);
428*7c478bd9Sstevel@tonic-gate 		open_package(env);
429*7c478bd9Sstevel@tonic-gate 		MYSELF = (instance_t *)POP(DS);
430*7c478bd9Sstevel@tonic-gate 		check_my_self(env, "begin-package");
431*7c478bd9Sstevel@tonic-gate 		new_device(env);
432*7c478bd9Sstevel@tonic-gate 		set_args(env);
433*7c478bd9Sstevel@tonic-gate 	} else {
434*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "Package '%s' not found\n", name);
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate void
439*7c478bd9Sstevel@tonic-gate open_package(fcode_env_t *env)
440*7c478bd9Sstevel@tonic-gate {
441*7c478bd9Sstevel@tonic-gate 	device_t *phandle;
442*7c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
443*7c478bd9Sstevel@tonic-gate 	int len;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "open-package");
446*7c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, phandle, POP(DS));
447*7c478bd9Sstevel@tonic-gate 	ihandle = open_instance_chain(env, phandle, 1);
448*7c478bd9Sstevel@tonic-gate 	ihandle->my_args = pop_a_duped_string(env, &len);
449*7c478bd9Sstevel@tonic-gate 	ihandle->my_args_len = len;
450*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)ihandle);
451*7c478bd9Sstevel@tonic-gate }
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate void
454*7c478bd9Sstevel@tonic-gate dollar_open_package(fcode_env_t *env)
455*7c478bd9Sstevel@tonic-gate {
456*7c478bd9Sstevel@tonic-gate 	fstack_t ok;
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 4, "$open-package");
459*7c478bd9Sstevel@tonic-gate 	find_package(env);
460*7c478bd9Sstevel@tonic-gate 	ok = POP(DS);
461*7c478bd9Sstevel@tonic-gate 	if (ok) {
462*7c478bd9Sstevel@tonic-gate 		open_package(env);
463*7c478bd9Sstevel@tonic-gate 	} else {
464*7c478bd9Sstevel@tonic-gate 		PUSH(DS, 0);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate }
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate void
469*7c478bd9Sstevel@tonic-gate close_package(fcode_env_t *env)
470*7c478bd9Sstevel@tonic-gate {
471*7c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "close-package");
474*7c478bd9Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
475*7c478bd9Sstevel@tonic-gate 	close_instance_chain(env, ihandle, 1);
476*7c478bd9Sstevel@tonic-gate }
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate static void (*find_method_hook)(fcode_env_t *);
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate void
481*7c478bd9Sstevel@tonic-gate set_find_method_hook(fcode_env_t *env, void (*hook)(fcode_env_t *))
482*7c478bd9Sstevel@tonic-gate {
483*7c478bd9Sstevel@tonic-gate 	find_method_hook = hook;
484*7c478bd9Sstevel@tonic-gate }
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate void
487*7c478bd9Sstevel@tonic-gate find_method(fcode_env_t *env)
488*7c478bd9Sstevel@tonic-gate {
489*7c478bd9Sstevel@tonic-gate 	fstack_t d;
490*7c478bd9Sstevel@tonic-gate 	device_t *device;
491*7c478bd9Sstevel@tonic-gate 	acf_t acf = 0;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "find-method");
494*7c478bd9Sstevel@tonic-gate 	if (find_method_hook) {
495*7c478bd9Sstevel@tonic-gate 		(*find_method_hook)(env);
496*7c478bd9Sstevel@tonic-gate 		if (TOS)		/* Found it */
497*7c478bd9Sstevel@tonic-gate 			return;
498*7c478bd9Sstevel@tonic-gate 		POP(DS);
499*7c478bd9Sstevel@tonic-gate 	}
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	d = POP(DS);
502*7c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, device, d);
503*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&device->vocabulary);
504*7c478bd9Sstevel@tonic-gate 	acf = voc_find(env);
505*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)acf);
506*7c478bd9Sstevel@tonic-gate 	if (acf) {
507*7c478bd9Sstevel@tonic-gate 		PUSH(DS, TRUE);
508*7c478bd9Sstevel@tonic-gate 	}
509*7c478bd9Sstevel@tonic-gate }
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate /*
512*7c478bd9Sstevel@tonic-gate  * 'call-package' Fcode
513*7c478bd9Sstevel@tonic-gate  */
514*7c478bd9Sstevel@tonic-gate void
515*7c478bd9Sstevel@tonic-gate call_package(fcode_env_t *env)
516*7c478bd9Sstevel@tonic-gate {
517*7c478bd9Sstevel@tonic-gate 	instance_t *ihandle, *saved_myself;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "call-package");
520*7c478bd9Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
521*7c478bd9Sstevel@tonic-gate 	saved_myself = MYSELF;
522*7c478bd9Sstevel@tonic-gate 	MYSELF = ihandle;
523*7c478bd9Sstevel@tonic-gate 	execute(env);
524*7c478bd9Sstevel@tonic-gate 	MYSELF = saved_myself;
525*7c478bd9Sstevel@tonic-gate }
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate void
528*7c478bd9Sstevel@tonic-gate ihandle_to_phandle(fcode_env_t *env)
529*7c478bd9Sstevel@tonic-gate {
530*7c478bd9Sstevel@tonic-gate 	instance_t *i;
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "ihandle>phandle");
533*7c478bd9Sstevel@tonic-gate 	i = (instance_t *)TOS;
534*7c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, i->device);
535*7c478bd9Sstevel@tonic-gate }
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate char *
538*7c478bd9Sstevel@tonic-gate get_package_name(fcode_env_t *env, device_t *d)
539*7c478bd9Sstevel@tonic-gate {
540*7c478bd9Sstevel@tonic-gate 	char *name;
541*7c478bd9Sstevel@tonic-gate 	prop_t *prop;
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	prop = lookup_package_property(env, "name", d);
544*7c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
545*7c478bd9Sstevel@tonic-gate 		name = "<Unnamed>";
546*7c478bd9Sstevel@tonic-gate 	} else {
547*7c478bd9Sstevel@tonic-gate 		name = (char *)prop->data;
548*7c478bd9Sstevel@tonic-gate 	}
549*7c478bd9Sstevel@tonic-gate 	return (name);
550*7c478bd9Sstevel@tonic-gate }
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate static char *package_search_path = "/packages:/openprom";
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate device_t *
555*7c478bd9Sstevel@tonic-gate match_package_path(fcode_env_t *env, char *path)
556*7c478bd9Sstevel@tonic-gate {
557*7c478bd9Sstevel@tonic-gate 	device_t *d;
558*7c478bd9Sstevel@tonic-gate 	char *name;
559*7c478bd9Sstevel@tonic-gate 	int len;
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 	if (*path == '/') {
562*7c478bd9Sstevel@tonic-gate 		d = env->root_node->child;
563*7c478bd9Sstevel@tonic-gate 		path++;
564*7c478bd9Sstevel@tonic-gate 	} else
565*7c478bd9Sstevel@tonic-gate 		d = env->current_device;
566*7c478bd9Sstevel@tonic-gate 	while (*path != '\0' && d != NULL) {
567*7c478bd9Sstevel@tonic-gate 		name = get_package_name(env, d);
568*7c478bd9Sstevel@tonic-gate 		len = strlen(name);
569*7c478bd9Sstevel@tonic-gate 		if (strncmp(name, path, len) == 0) {
570*7c478bd9Sstevel@tonic-gate 			path += len;
571*7c478bd9Sstevel@tonic-gate 			if (*path == '\0') {
572*7c478bd9Sstevel@tonic-gate 				return (d);
573*7c478bd9Sstevel@tonic-gate 			}
574*7c478bd9Sstevel@tonic-gate 			/* skip the '/' */
575*7c478bd9Sstevel@tonic-gate 			if (*path++ != '/')
576*7c478bd9Sstevel@tonic-gate 				break;
577*7c478bd9Sstevel@tonic-gate 			d = d->child;
578*7c478bd9Sstevel@tonic-gate 		} else {
579*7c478bd9Sstevel@tonic-gate 			d = d->peer;
580*7c478bd9Sstevel@tonic-gate 		}
581*7c478bd9Sstevel@tonic-gate 	}
582*7c478bd9Sstevel@tonic-gate 	return (NULL);
583*7c478bd9Sstevel@tonic-gate }
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate device_t *
586*7c478bd9Sstevel@tonic-gate locate_package(fcode_env_t *env, char *start)
587*7c478bd9Sstevel@tonic-gate {
588*7c478bd9Sstevel@tonic-gate 	device_t *d;
589*7c478bd9Sstevel@tonic-gate 	char *p, *next_p;
590*7c478bd9Sstevel@tonic-gate 	char *tpath, *fpath;
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	if ((d = match_package_path(env, start)) != NULL)
593*7c478bd9Sstevel@tonic-gate 		return (d);
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate 	/*
596*7c478bd9Sstevel@tonic-gate 	 * ignore starting '/'
597*7c478bd9Sstevel@tonic-gate 	 */
598*7c478bd9Sstevel@tonic-gate 	if (*start == '/')
599*7c478bd9Sstevel@tonic-gate 		*start++;
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	fpath = STRDUP(package_search_path);
602*7c478bd9Sstevel@tonic-gate 	for (p = fpath; p != NULL; p = next_p) {
603*7c478bd9Sstevel@tonic-gate 		if ((next_p = strchr(p, ':')) != NULL)
604*7c478bd9Sstevel@tonic-gate 			*next_p++ = '\0';
605*7c478bd9Sstevel@tonic-gate 		tpath = MALLOC(strlen(p) + strlen(start) + 2);
606*7c478bd9Sstevel@tonic-gate 		sprintf(tpath, "%s/%s", p, start);
607*7c478bd9Sstevel@tonic-gate 		if ((d = match_package_path(env, tpath)) != NULL) {
608*7c478bd9Sstevel@tonic-gate 			FREE(fpath);
609*7c478bd9Sstevel@tonic-gate 			FREE(tpath);
610*7c478bd9Sstevel@tonic-gate 			return (d);
611*7c478bd9Sstevel@tonic-gate 		}
612*7c478bd9Sstevel@tonic-gate 		FREE(tpath);
613*7c478bd9Sstevel@tonic-gate 	}
614*7c478bd9Sstevel@tonic-gate 	FREE(fpath);
615*7c478bd9Sstevel@tonic-gate 	return (NULL);
616*7c478bd9Sstevel@tonic-gate }
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate void
619*7c478bd9Sstevel@tonic-gate find_package(fcode_env_t *env)
620*7c478bd9Sstevel@tonic-gate {
621*7c478bd9Sstevel@tonic-gate 	char *path;
622*7c478bd9Sstevel@tonic-gate 	device_t *package;
623*7c478bd9Sstevel@tonic-gate 	fstack_t ph = 0;
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "find-package");
626*7c478bd9Sstevel@tonic-gate 	if ((path = pop_a_duped_string(env, NULL)) != NULL) {
627*7c478bd9Sstevel@tonic-gate 		if (strcmp(path, "/") == 0)
628*7c478bd9Sstevel@tonic-gate 			package = env->root_node;
629*7c478bd9Sstevel@tonic-gate 		else
630*7c478bd9Sstevel@tonic-gate 			package = locate_package(env, path);
631*7c478bd9Sstevel@tonic-gate 		FREE(path);
632*7c478bd9Sstevel@tonic-gate 		REVERT_PHANDLE(env, ph, package);
633*7c478bd9Sstevel@tonic-gate 	}
634*7c478bd9Sstevel@tonic-gate 	PUSH(DS, ph);
635*7c478bd9Sstevel@tonic-gate 	if (package)
636*7c478bd9Sstevel@tonic-gate 		PUSH(DS, TRUE);
637*7c478bd9Sstevel@tonic-gate }
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate static void
640*7c478bd9Sstevel@tonic-gate encode_unit_hack(fcode_env_t *env)
641*7c478bd9Sstevel@tonic-gate {
642*7c478bd9Sstevel@tonic-gate 	int hi, i;
643*7c478bd9Sstevel@tonic-gate 	uint_t ncells = get_number_of_parent_address_cells(env);
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncells; i++)
646*7c478bd9Sstevel@tonic-gate 		POP(DS);
647*7c478bd9Sstevel@tonic-gate 	push_a_string(env, NULL);
648*7c478bd9Sstevel@tonic-gate }
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate void
651*7c478bd9Sstevel@tonic-gate dollar_call_method(fcode_env_t *env)
652*7c478bd9Sstevel@tonic-gate {
653*7c478bd9Sstevel@tonic-gate 	instance_t *old_myself;
654*7c478bd9Sstevel@tonic-gate 	instance_t *myself;
655*7c478bd9Sstevel@tonic-gate 	device_t *device;
656*7c478bd9Sstevel@tonic-gate 	char *method;
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "$call-method");
659*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "$call-method");
660*7c478bd9Sstevel@tonic-gate 	old_myself = MYSELF;
661*7c478bd9Sstevel@tonic-gate 	myself = (instance_t *)POP(DS);
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate 	method = (char *)DS[-1];
664*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_CALL_METHOD, "$call_method %s\n", method);
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	if (old_myself && !myself) {
667*7c478bd9Sstevel@tonic-gate 		/* We hit the root of our tree */
668*7c478bd9Sstevel@tonic-gate 		device = old_myself->device;
669*7c478bd9Sstevel@tonic-gate 		return;
670*7c478bd9Sstevel@tonic-gate 	}
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 	MYSELF = myself;
673*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "$call-method");
674*7c478bd9Sstevel@tonic-gate 	device = MYSELF->device;
675*7c478bd9Sstevel@tonic-gate 	do_push_package(env, device);
676*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)device);
677*7c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, device);
678*7c478bd9Sstevel@tonic-gate 	find_method(env);
679*7c478bd9Sstevel@tonic-gate 	if (TOS) {
680*7c478bd9Sstevel@tonic-gate 		(void) POP(DS);
681*7c478bd9Sstevel@tonic-gate 		execute(env);
682*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(method, "encode-unit") == 0) {
683*7c478bd9Sstevel@tonic-gate 		encode_unit_hack(env);
684*7c478bd9Sstevel@tonic-gate 	} else {
685*7c478bd9Sstevel@tonic-gate 		throw_from_fclib(env, 1, "Unimplemented package method: %s%s",
686*7c478bd9Sstevel@tonic-gate 		    get_path(env, device), method);
687*7c478bd9Sstevel@tonic-gate 	}
688*7c478bd9Sstevel@tonic-gate 	MYSELF = old_myself;
689*7c478bd9Sstevel@tonic-gate 	do_push_package(env, MYSELF->device);
690*7c478bd9Sstevel@tonic-gate }
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate void
693*7c478bd9Sstevel@tonic-gate dollar_call_parent(fcode_env_t *env)
694*7c478bd9Sstevel@tonic-gate {
695*7c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "$call-parent");
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "$call-parent");
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF->parent);
700*7c478bd9Sstevel@tonic-gate 	dollar_call_method(env);
701*7c478bd9Sstevel@tonic-gate }
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
704*7c478bd9Sstevel@tonic-gate void
705*7c478bd9Sstevel@tonic-gate current_device(fcode_env_t *env)
706*7c478bd9Sstevel@tonic-gate {
707*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&env->current_device);
708*7c478bd9Sstevel@tonic-gate }
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate char *
711*7c478bd9Sstevel@tonic-gate get_path(fcode_env_t *env, device_t *d)
712*7c478bd9Sstevel@tonic-gate {
713*7c478bd9Sstevel@tonic-gate 	char *pre_path, *name, *path;
714*7c478bd9Sstevel@tonic-gate 	int n;
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	if (d->parent)
717*7c478bd9Sstevel@tonic-gate 		pre_path = get_path(env, d->parent);
718*7c478bd9Sstevel@tonic-gate 	else
719*7c478bd9Sstevel@tonic-gate 		pre_path = STRDUP("");
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate 	name = get_package_name(env, d);
722*7c478bd9Sstevel@tonic-gate 	n = strlen(pre_path) + strlen(name) + 1;
723*7c478bd9Sstevel@tonic-gate 	path = MALLOC(n);
724*7c478bd9Sstevel@tonic-gate 	strcpy(path, pre_path);
725*7c478bd9Sstevel@tonic-gate 	strcat(path, name);
726*7c478bd9Sstevel@tonic-gate 	if (d->child && d->parent)
727*7c478bd9Sstevel@tonic-gate 		strcat(path, "/");
728*7c478bd9Sstevel@tonic-gate 	FREE(pre_path);
729*7c478bd9Sstevel@tonic-gate 	return (path);
730*7c478bd9Sstevel@tonic-gate }
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate static void
733*7c478bd9Sstevel@tonic-gate pwd_dollar(fcode_env_t *env)
734*7c478bd9Sstevel@tonic-gate {
735*7c478bd9Sstevel@tonic-gate 	if (env->current_device)
736*7c478bd9Sstevel@tonic-gate 		push_a_string(env, get_path(env, env->current_device));
737*7c478bd9Sstevel@tonic-gate 	else
738*7c478bd9Sstevel@tonic-gate 		push_a_string(env, NULL);
739*7c478bd9Sstevel@tonic-gate }
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate void
742*7c478bd9Sstevel@tonic-gate pwd(fcode_env_t *env)
743*7c478bd9Sstevel@tonic-gate {
744*7c478bd9Sstevel@tonic-gate 	if (env->current_device) {
745*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "%s\n",
746*7c478bd9Sstevel@tonic-gate 		    get_path(env, env->current_device));
747*7c478bd9Sstevel@tonic-gate 	} else {
748*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
749*7c478bd9Sstevel@tonic-gate 	}
750*7c478bd9Sstevel@tonic-gate }
751*7c478bd9Sstevel@tonic-gate 
752*7c478bd9Sstevel@tonic-gate void
753*7c478bd9Sstevel@tonic-gate do_ls(fcode_env_t *env)
754*7c478bd9Sstevel@tonic-gate {
755*7c478bd9Sstevel@tonic-gate 	device_t *d;
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 	if (env->current_device == NULL) {
758*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
759*7c478bd9Sstevel@tonic-gate 		return;
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	d = env->current_device->child;
763*7c478bd9Sstevel@tonic-gate 	while (d) {
764*7c478bd9Sstevel@tonic-gate 		char *name;
765*7c478bd9Sstevel@tonic-gate 		fstack_t ph;
766*7c478bd9Sstevel@tonic-gate 		name = get_package_name(env, d);
767*7c478bd9Sstevel@tonic-gate 		REVERT_PHANDLE(env, ph, d);
768*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "%llx %s\n", (uint64_t)ph, name);
769*7c478bd9Sstevel@tonic-gate 		d = d->peer;
770*7c478bd9Sstevel@tonic-gate 	}
771*7c478bd9Sstevel@tonic-gate }
772*7c478bd9Sstevel@tonic-gate 
773*7c478bd9Sstevel@tonic-gate void
774*7c478bd9Sstevel@tonic-gate paren_cd(fcode_env_t *env)
775*7c478bd9Sstevel@tonic-gate {
776*7c478bd9Sstevel@tonic-gate 	char *str;
777*7c478bd9Sstevel@tonic-gate 	device_t *p;
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	str = pop_a_string(env, NULL);
780*7c478bd9Sstevel@tonic-gate 	if (strcmp(str, "/") == 0) {
781*7c478bd9Sstevel@tonic-gate 		root_node(env);
782*7c478bd9Sstevel@tonic-gate 		return;
783*7c478bd9Sstevel@tonic-gate 	}
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate 	if (env->current_device == NULL) {
786*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No device context\n");
787*7c478bd9Sstevel@tonic-gate 		return;
788*7c478bd9Sstevel@tonic-gate 	}
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	if (strcmp(str, "..") == 0)
791*7c478bd9Sstevel@tonic-gate 		p = env->current_device->parent;
792*7c478bd9Sstevel@tonic-gate 	else {
793*7c478bd9Sstevel@tonic-gate 		device_t *n = env->current_device->child;
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 		p = NULL;
796*7c478bd9Sstevel@tonic-gate 		while (n) {
797*7c478bd9Sstevel@tonic-gate 			char *name;
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 			name = get_package_name(env, n);
800*7c478bd9Sstevel@tonic-gate 			if (strcmp(name, str) == 0) {
801*7c478bd9Sstevel@tonic-gate 				p = n;
802*7c478bd9Sstevel@tonic-gate 				break;
803*7c478bd9Sstevel@tonic-gate 			}
804*7c478bd9Sstevel@tonic-gate 			n = n->peer;
805*7c478bd9Sstevel@tonic-gate 		}
806*7c478bd9Sstevel@tonic-gate 	}
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate 	if (p) {
809*7c478bd9Sstevel@tonic-gate 		activate_device(env, p);
810*7c478bd9Sstevel@tonic-gate 	} else {
811*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "No such node: %s\n", str);
812*7c478bd9Sstevel@tonic-gate 	}
813*7c478bd9Sstevel@tonic-gate }
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate void
816*7c478bd9Sstevel@tonic-gate do_cd(fcode_env_t *env)
817*7c478bd9Sstevel@tonic-gate {
818*7c478bd9Sstevel@tonic-gate 	parse_word(env);
819*7c478bd9Sstevel@tonic-gate 	paren_cd(env);
820*7c478bd9Sstevel@tonic-gate }
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate void
823*7c478bd9Sstevel@tonic-gate do_unselect_dev(fcode_env_t *env)
824*7c478bd9Sstevel@tonic-gate {
825*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "unselect-dev");
826*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)MYSELF);
827*7c478bd9Sstevel@tonic-gate 	close_package(env);
828*7c478bd9Sstevel@tonic-gate 	deactivate_device(env, NULL);
829*7c478bd9Sstevel@tonic-gate }
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate void
832*7c478bd9Sstevel@tonic-gate do_select_dev(fcode_env_t *env)
833*7c478bd9Sstevel@tonic-gate {
834*7c478bd9Sstevel@tonic-gate 	PUSH(DS, 0);
835*7c478bd9Sstevel@tonic-gate 	PUSH(DS, 0);
836*7c478bd9Sstevel@tonic-gate 	two_swap(env);
837*7c478bd9Sstevel@tonic-gate 	dollar_open_package(env);
838*7c478bd9Sstevel@tonic-gate 	if (TOS) {
839*7c478bd9Sstevel@tonic-gate 		MYSELF = (instance_t *)POP(DS);
840*7c478bd9Sstevel@tonic-gate 		check_my_self(env, "select-dev");
841*7c478bd9Sstevel@tonic-gate 		activate_device(env, MYSELF->device);
842*7c478bd9Sstevel@tonic-gate 	} else {
843*7c478bd9Sstevel@tonic-gate 		drop(env);
844*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "Can't open package\n");
845*7c478bd9Sstevel@tonic-gate 	}
846*7c478bd9Sstevel@tonic-gate }
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate void
849*7c478bd9Sstevel@tonic-gate device_end(fcode_env_t *env)
850*7c478bd9Sstevel@tonic-gate {
851*7c478bd9Sstevel@tonic-gate 	if (env->current_device) {
852*7c478bd9Sstevel@tonic-gate 		deactivate_device(env, NULL);
853*7c478bd9Sstevel@tonic-gate 	}
854*7c478bd9Sstevel@tonic-gate }
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate void
857*7c478bd9Sstevel@tonic-gate end_package(fcode_env_t *env)
858*7c478bd9Sstevel@tonic-gate {
859*7c478bd9Sstevel@tonic-gate 	finish_device(env);
860*7c478bd9Sstevel@tonic-gate 	close_instance_chain(env, MYSELF, 0);
861*7c478bd9Sstevel@tonic-gate 	device_end(env);
862*7c478bd9Sstevel@tonic-gate 	MYSELF = NULL;
863*7c478bd9Sstevel@tonic-gate }
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate void
866*7c478bd9Sstevel@tonic-gate exec_parent_method(fcode_env_t *env)
867*7c478bd9Sstevel@tonic-gate {
868*7c478bd9Sstevel@tonic-gate 	instance_t *old_myself;
869*7c478bd9Sstevel@tonic-gate 	instance_t *myself;
870*7c478bd9Sstevel@tonic-gate 	device_t *device;
871*7c478bd9Sstevel@tonic-gate 	char *method;
872*7c478bd9Sstevel@tonic-gate 	fstack_t d;
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "exec-parent-method");
875*7c478bd9Sstevel@tonic-gate 	old_myself = MYSELF;
876*7c478bd9Sstevel@tonic-gate 	MYSELF = MYSELF->parent;
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate 	method = (char *)DS[-1];
879*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_FIND_FCODE, "exec_parent_method: '%s'\n", method);
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	check_my_self(env, "exec-parent-method");
882*7c478bd9Sstevel@tonic-gate 	device = MYSELF->device;
883*7c478bd9Sstevel@tonic-gate 	do_push_package(env, device);
884*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t)device);
885*7c478bd9Sstevel@tonic-gate 	REVERT_PHANDLE(env, TOS, device);
886*7c478bd9Sstevel@tonic-gate 	find_method(env);
887*7c478bd9Sstevel@tonic-gate 	d = POP(DS);
888*7c478bd9Sstevel@tonic-gate 	if (d) {
889*7c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_FIND_FCODE, "exec-parent-method: '%s'/%x"
890*7c478bd9Sstevel@tonic-gate 		    " execute\n", method, (int)TOS);
891*7c478bd9Sstevel@tonic-gate 		execute(env);
892*7c478bd9Sstevel@tonic-gate 		PUSH(DS, TRUE);
893*7c478bd9Sstevel@tonic-gate 	} else {
894*7c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_FIND_FCODE, "exec-parent-method: '%s'"
895*7c478bd9Sstevel@tonic-gate 		    " not found\n", method);
896*7c478bd9Sstevel@tonic-gate 		PUSH(DS, FALSE);
897*7c478bd9Sstevel@tonic-gate 	}
898*7c478bd9Sstevel@tonic-gate 	MYSELF = old_myself;
899*7c478bd9Sstevel@tonic-gate 	do_push_package(env, MYSELF->device);
900*7c478bd9Sstevel@tonic-gate }
901*7c478bd9Sstevel@tonic-gate 
902*7c478bd9Sstevel@tonic-gate void
903*7c478bd9Sstevel@tonic-gate dump_device(fcode_env_t *env)
904*7c478bd9Sstevel@tonic-gate {
905*7c478bd9Sstevel@tonic-gate 	device_t *phandle;
906*7c478bd9Sstevel@tonic-gate 	int i;
907*7c478bd9Sstevel@tonic-gate 
908*7c478bd9Sstevel@tonic-gate 	CONVERT_PHANDLE(env, phandle, POP(DS));
909*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Node:      %p\n", phandle);
910*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Parent:  (%8p) %p\n",
911*7c478bd9Sstevel@tonic-gate 	    &phandle->parent, phandle->parent);
912*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Child:   (%8p) %p\n",
913*7c478bd9Sstevel@tonic-gate 	    &phandle->child, phandle->child);
914*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Peer:    (%8p) %p\n",
915*7c478bd9Sstevel@tonic-gate 	    &phandle->peer, phandle->peer);
916*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Private: (%8p) %p\n",
917*7c478bd9Sstevel@tonic-gate 	    &phandle->private, phandle->private);
918*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Props:   (%8p) %p\n",
919*7c478bd9Sstevel@tonic-gate 	    &phandle->properties, phandle->properties);
920*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Voc:     (%8p) %p\n",
921*7c478bd9Sstevel@tonic-gate 	    &phandle->vocabulary, phandle->vocabulary);
922*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  sizes:   (%8p) %d %d\n",
923*7c478bd9Sstevel@tonic-gate 	    &phandle->data_size,
924*7c478bd9Sstevel@tonic-gate 	    phandle->data_size[INIT_DATA],
925*7c478bd9Sstevel@tonic-gate 	    phandle->data_size[UINIT_DATA]);
926*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_space: %x\n", phandle->my_space);
927*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_addr :");
928*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MY_ADDR; i++)
929*7c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, " %x", (int)phandle->my_addr[i]);
930*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "\n");
931*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  data:    (%8p)\n", phandle->init_data);
932*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < phandle->data_size[INIT_DATA]; i++) {
933*7c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
934*7c478bd9Sstevel@tonic-gate 		    &phandle->init_data[i], phandle->init_data[i]);
935*7c478bd9Sstevel@tonic-gate 	}
936*7c478bd9Sstevel@tonic-gate }
937*7c478bd9Sstevel@tonic-gate 
938*7c478bd9Sstevel@tonic-gate void
939*7c478bd9Sstevel@tonic-gate dump_instance(fcode_env_t *env)
940*7c478bd9Sstevel@tonic-gate {
941*7c478bd9Sstevel@tonic-gate 	int i;
942*7c478bd9Sstevel@tonic-gate 	instance_t *ihandle;
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 	ihandle = (instance_t *)POP(DS);
945*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Ihandle:      %p\n", ihandle);
946*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Parent:  (%8p) %p\n",
947*7c478bd9Sstevel@tonic-gate 	    &ihandle->parent, ihandle->parent);
948*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  Device:  (%8p) %p\n",
949*7c478bd9Sstevel@tonic-gate 	    &ihandle->device, ihandle->device);
950*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  args:     '%s'\n",
951*7c478bd9Sstevel@tonic-gate 	    ((ihandle->my_args) ? ihandle->my_args : ""));
952*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my-space: %x\n", ihandle->my_space);
953*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  my_addr :");
954*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_MY_ADDR; i++)
955*7c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, " %x", (int)ihandle->my_addr[i]);
956*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "\n");
957*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  sizes:   %d %d\n",
958*7c478bd9Sstevel@tonic-gate 	    ihandle->device->data_size[INIT_DATA],
959*7c478bd9Sstevel@tonic-gate 	    ihandle->device->data_size[UINIT_DATA]);
960*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, "  data:    (%8p) %x %x\n",
961*7c478bd9Sstevel@tonic-gate 	    ihandle->data, ihandle->data[0], ihandle->data[1]);
962*7c478bd9Sstevel@tonic-gate 	if (ihandle->device->data_size[INIT_DATA]) {
963*7c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, "  Initialised:\n");
964*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ihandle->device->data_size[INIT_DATA]; i++) {
965*7c478bd9Sstevel@tonic-gate 			log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
966*7c478bd9Sstevel@tonic-gate 			    &ihandle->data[INIT_DATA][i],
967*7c478bd9Sstevel@tonic-gate 			    ihandle->data[INIT_DATA][i]);
968*7c478bd9Sstevel@tonic-gate 		}
969*7c478bd9Sstevel@tonic-gate 	}
970*7c478bd9Sstevel@tonic-gate 	if (ihandle->device->data_size[INIT_DATA]) {
971*7c478bd9Sstevel@tonic-gate 		log_message(MSG_DEBUG, "  UnInitialised:\n");
972*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ihandle->device->data_size[UINIT_DATA]; i++) {
973*7c478bd9Sstevel@tonic-gate 			log_message(MSG_DEBUG, "    %3d  -> (%8p) %x\n", i,
974*7c478bd9Sstevel@tonic-gate 			    &ihandle->data[UINIT_DATA][i],
975*7c478bd9Sstevel@tonic-gate 			    ihandle->data[UINIT_DATA][i]);
976*7c478bd9Sstevel@tonic-gate 		}
977*7c478bd9Sstevel@tonic-gate 	}
978*7c478bd9Sstevel@tonic-gate }
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate #endif
981*7c478bd9Sstevel@tonic-gate 
982*7c478bd9Sstevel@tonic-gate #pragma init(_init)
983*7c478bd9Sstevel@tonic-gate 
984*7c478bd9Sstevel@tonic-gate #ifdef CONVERT_HANDLES
985*7c478bd9Sstevel@tonic-gate static device_t	*
986*7c478bd9Sstevel@tonic-gate safe_convert_phandle(fcode_env_t *env, fstack_t d)
987*7c478bd9Sstevel@tonic-gate {
988*7c478bd9Sstevel@tonic-gate 	return ((device_t *)d);
989*7c478bd9Sstevel@tonic-gate }
990*7c478bd9Sstevel@tonic-gate 
991*7c478bd9Sstevel@tonic-gate static fstack_t
992*7c478bd9Sstevel@tonic-gate safe_revert_phandle(fcode_env_t *env, device_t *d)
993*7c478bd9Sstevel@tonic-gate {
994*7c478bd9Sstevel@tonic-gate 	return ((fstack_t)d);
995*7c478bd9Sstevel@tonic-gate }
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate static void
998*7c478bd9Sstevel@tonic-gate safe_allocate_phandle(fcode_env_t *env)
999*7c478bd9Sstevel@tonic-gate {
1000*7c478bd9Sstevel@tonic-gate }
1001*7c478bd9Sstevel@tonic-gate 
1002*7c478bd9Sstevel@tonic-gate #endif
1003*7c478bd9Sstevel@tonic-gate 
1004*7c478bd9Sstevel@tonic-gate static void
1005*7c478bd9Sstevel@tonic-gate _init(void)
1006*7c478bd9Sstevel@tonic-gate {
1007*7c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
1008*7c478bd9Sstevel@tonic-gate 	char *name = "/";
1009*7c478bd9Sstevel@tonic-gate 	device_t *d;
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate 	ASSERT(env);
1012*7c478bd9Sstevel@tonic-gate 	NOTICE;
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate #ifdef CONVERT_HANDLES
1015*7c478bd9Sstevel@tonic-gate 	env->convert_phandle = safe_convert_phandle;
1016*7c478bd9Sstevel@tonic-gate 	env->revert_phandle = safe_revert_phandle;
1017*7c478bd9Sstevel@tonic-gate 	env->allocate_phandle = safe_allocate_phandle;
1018*7c478bd9Sstevel@tonic-gate #endif
1019*7c478bd9Sstevel@tonic-gate 
1020*7c478bd9Sstevel@tonic-gate 	/* build the root node */
1021*7c478bd9Sstevel@tonic-gate 	d = create_phandle(env, NULL);
1022*7c478bd9Sstevel@tonic-gate 	env->current_device = d;
1023*7c478bd9Sstevel@tonic-gate 	env->root_node = d;
1024*7c478bd9Sstevel@tonic-gate 	push_a_string(env, name);
1025*7c478bd9Sstevel@tonic-gate 	device_name(env);
1026*7c478bd9Sstevel@tonic-gate 	env->current_device = NULL;
1027*7c478bd9Sstevel@tonic-gate 
1028*7c478bd9Sstevel@tonic-gate 	create_my_self(env);
1029*7c478bd9Sstevel@tonic-gate 	create_my_space(env);
1030*7c478bd9Sstevel@tonic-gate 
1031*7c478bd9Sstevel@tonic-gate 	P1275(0x102, 0,		"my-address",		my_address);
1032*7c478bd9Sstevel@tonic-gate 	/* Fcode 0x103 "my-space" is created using create_internal_value */
1033*7c478bd9Sstevel@tonic-gate 
1034*7c478bd9Sstevel@tonic-gate 	P1275(0x11f, 0,		"new-device",		new_device);
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate 	P1275(0x127, 0,		"finish-device",	finish_device);
1037*7c478bd9Sstevel@tonic-gate 
1038*7c478bd9Sstevel@tonic-gate 	FCODE(0x129, 0,		"push-package",		push_package);
1039*7c478bd9Sstevel@tonic-gate 	FCODE(0x12a, 0,		"pop-package",		pop_package);
1040*7c478bd9Sstevel@tonic-gate 	FCODE(0x12b, 0,		"interpose",		interpose);
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate 	P1275(0x202, 0,		"my-args",		my_args);
1043*7c478bd9Sstevel@tonic-gate 	/* Fcode 0x203 "my-self" is created using create_internal_value */
1044*7c478bd9Sstevel@tonic-gate 	P1275(0x204, 0,		"find-package",		find_package);
1045*7c478bd9Sstevel@tonic-gate 	P1275(0x205, 0,		"open-package",		open_package);
1046*7c478bd9Sstevel@tonic-gate 	P1275(0x206, 0,		"close-package",	close_package);
1047*7c478bd9Sstevel@tonic-gate 	P1275(0x207, 0,		"find-method",		find_method);
1048*7c478bd9Sstevel@tonic-gate 	P1275(0x208, 0,		"call-package",		call_package);
1049*7c478bd9Sstevel@tonic-gate 	P1275(0x209, 0,		"$call-parent",		dollar_call_parent);
1050*7c478bd9Sstevel@tonic-gate 	P1275(0x20a, 0,		"my-parent",		my_parent);
1051*7c478bd9Sstevel@tonic-gate 	P1275(0x20b, 0,		"ihandle>phandle",	ihandle_to_phandle);
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate 	P1275(0x20d, 0,		"my-unit",		my_unit);
1054*7c478bd9Sstevel@tonic-gate 	P1275(0x20e, 0,		"$call-method",		dollar_call_method);
1055*7c478bd9Sstevel@tonic-gate 	P1275(0x20f, 0,		"$open-package",	dollar_open_package);
1056*7c478bd9Sstevel@tonic-gate 
1057*7c478bd9Sstevel@tonic-gate 	P1275(0x23b, 0,		"child",		child_node);
1058*7c478bd9Sstevel@tonic-gate 	P1275(0x23c, 0,		"peer",			peer_node);
1059*7c478bd9Sstevel@tonic-gate 
1060*7c478bd9Sstevel@tonic-gate 	P1275(0x23f, 0,		"set-args",		set_args);
1061*7c478bd9Sstevel@tonic-gate 
1062*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"root-node",		root_node);
1063*7c478bd9Sstevel@tonic-gate 	FORTH(0,		"current-device",	current_device);
1064*7c478bd9Sstevel@tonic-gate 	FORTH(0,		"pwd$",			pwd_dollar);
1065*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"pwd",			pwd);
1066*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"ls",			do_ls);
1067*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"(cd)",			paren_cd);
1068*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"cd",			do_cd);
1069*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"device-end",		device_end);
1070*7c478bd9Sstevel@tonic-gate 	FORTH(0,		"select-dev",		do_select_dev);
1071*7c478bd9Sstevel@tonic-gate 	FORTH(0,		"unselect-dev",		do_unselect_dev);
1072*7c478bd9Sstevel@tonic-gate 	FORTH(0,		"begin-package",	begin_package);
1073*7c478bd9Sstevel@tonic-gate 	FORTH(0,		"end-package",		end_package);
1074*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-device",		dump_device);
1075*7c478bd9Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-instance",	dump_instance);
1076*7c478bd9Sstevel@tonic-gate 	FORTH(0,		"exec-parent-method",	exec_parent_method);
1077*7c478bd9Sstevel@tonic-gate }
1078