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