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 <strings.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <fcode/private.h>
34*0Sstevel@tonic-gate #include <fcode/log.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static fc_cell_t
39*0Sstevel@tonic-gate fc_reg_read(fcode_env_t *env, char *service, fstack_t virt, int *errp)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	fc_cell_t virtaddr, data;
42*0Sstevel@tonic-gate 	int error, nin;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	if (!is_mcookie(virt))
45*0Sstevel@tonic-gate 		forth_abort(env, "fc_reg_read: bad mcookie: 0x%x\n", virt);
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 	virtaddr = mcookie_to_addr(virt);
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	/* Supress fc_run_priv error msgs on peeks */
50*0Sstevel@tonic-gate 	nin = ((errp == NULL) ? 1 : (1 | FCRP_NOERROR));
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	error = fc_run_priv(env->private, service, nin, 1, virtaddr, &data);
53*0Sstevel@tonic-gate 	if (errp)
54*0Sstevel@tonic-gate 		/* Don't report error on peeks */
55*0Sstevel@tonic-gate 		*errp = error;
56*0Sstevel@tonic-gate 	else if (error) {
57*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "fc_read_reg: ERROR: cookie: %llx"
58*0Sstevel@tonic-gate 		    " virt: %llx\n", (uint64_t)virt, (uint64_t)virtaddr);
59*0Sstevel@tonic-gate 		data = 0;
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate 	return (data);
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static void
65*0Sstevel@tonic-gate fc_reg_write(fcode_env_t *env, char *service, fstack_t virt, fc_cell_t data,
66*0Sstevel@tonic-gate     int *errp)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	fc_cell_t virtaddr;
69*0Sstevel@tonic-gate 	int error, nin;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (!is_mcookie(virt))
72*0Sstevel@tonic-gate 		forth_abort(env, "fc_reg_write: bad mcookie: 0x%x\n", virt);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	virtaddr = mcookie_to_addr(virt);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	/* Supress fc_run_priv error msgs on pokes */
77*0Sstevel@tonic-gate 	nin = ((errp == NULL) ? 2 : (2 | FCRP_NOERROR));
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	error = fc_run_priv(env->private, service, nin, 0, virtaddr, data);
80*0Sstevel@tonic-gate 	if (errp)
81*0Sstevel@tonic-gate 		/* Don't report error on pokes */
82*0Sstevel@tonic-gate 		*errp = error;
83*0Sstevel@tonic-gate 	else if (error) {
84*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "fc_write_reg: ERROR: cookie: %llx"
85*0Sstevel@tonic-gate 		    " virt: %llx\n", (uint64_t)virt, (uint64_t)virtaddr);
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate static int
90*0Sstevel@tonic-gate check_address_abuse(fcode_env_t *env, fstack_t addr, char *type,
91*0Sstevel@tonic-gate     int want_mcookie, void (*alt)(fcode_env_t *))
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	if (is_mcookie(addr) != want_mcookie) {
94*0Sstevel@tonic-gate 		debug_msg(DEBUG_ADDR_ABUSE, "Warning: %s to %s address: %llx\n",
95*0Sstevel@tonic-gate 		    type, want_mcookie ? "unmapped" : "mapped",
96*0Sstevel@tonic-gate 		    (uint64_t)addr);
97*0Sstevel@tonic-gate 		(*alt)(env);
98*0Sstevel@tonic-gate 		return (1);
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 	return (0);
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate static void
104*0Sstevel@tonic-gate rlfetch(fcode_env_t *env)
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate 	fstack_t p;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "rl@");
109*0Sstevel@tonic-gate 	p = TOS;
110*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rl@", 1, lfetch))
111*0Sstevel@tonic-gate 		TOS = (lforth_t)fc_reg_read(env, "rl@", p, NULL);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static void
115*0Sstevel@tonic-gate rlstore(fcode_env_t *env)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	fstack_t p, d;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "rl!");
120*0Sstevel@tonic-gate 	p = TOS;
121*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rl!", 1, lstore)) {
122*0Sstevel@tonic-gate 		p = POP(DS);
123*0Sstevel@tonic-gate 		d = POP(DS);
124*0Sstevel@tonic-gate 		fc_reg_write(env, "rl!", p, d, NULL);
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static void
129*0Sstevel@tonic-gate rwfetch(fcode_env_t *env)
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	fstack_t p;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "rw@");
134*0Sstevel@tonic-gate 	p = TOS;
135*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rw@", 1, wfetch))
136*0Sstevel@tonic-gate 		TOS = (wforth_t)fc_reg_read(env, "rw@", p, NULL);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate static void
140*0Sstevel@tonic-gate rwstore(fcode_env_t *env)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	fstack_t p, d;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "rw!");
145*0Sstevel@tonic-gate 	p = TOS;
146*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rw!", 1, wstore)) {
147*0Sstevel@tonic-gate 		p = POP(DS);
148*0Sstevel@tonic-gate 		d = POP(DS);
149*0Sstevel@tonic-gate 		fc_reg_write(env, "rw!", p, d, NULL);
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate void
154*0Sstevel@tonic-gate rbfetch(fcode_env_t *env)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	fstack_t p;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "rb@");
159*0Sstevel@tonic-gate 	p = TOS;
160*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rb@", 1, cfetch)) {
161*0Sstevel@tonic-gate 		TOS = (uchar_t)fc_reg_read(env, "rb@", p, NULL);
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate static void
166*0Sstevel@tonic-gate rbstore(fcode_env_t *env)
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	fstack_t	p, d;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "rb!");
171*0Sstevel@tonic-gate 	p = TOS;
172*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rb!", 1, cstore)) {
173*0Sstevel@tonic-gate 		p = POP(DS);
174*0Sstevel@tonic-gate 		d = POP(DS);
175*0Sstevel@tonic-gate 		fc_reg_write(env, "rb!", p, d, NULL);
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * rx@        ( xa -- xv )
181*0Sstevel@tonic-gate  */
182*0Sstevel@tonic-gate static void
183*0Sstevel@tonic-gate rxfetch(fcode_env_t *env)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate 	fstack_t p;
186*0Sstevel@tonic-gate 	xforth_t x;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "rx@");
189*0Sstevel@tonic-gate 	p = TOS;
190*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rx@", 1, xfetch)) {
191*0Sstevel@tonic-gate 		p = POP(DS);
192*0Sstevel@tonic-gate 		push_xforth(env, (xforth_t)fc_reg_read(env, "rx@", p, NULL));
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate  * rx!        ( xv xa -- )
198*0Sstevel@tonic-gate  */
199*0Sstevel@tonic-gate static void
200*0Sstevel@tonic-gate rxstore(fcode_env_t *env)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	fstack_t p;
203*0Sstevel@tonic-gate 	xforth_t d;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "rx!");
206*0Sstevel@tonic-gate 	p = TOS;
207*0Sstevel@tonic-gate 	if (!check_address_abuse(env, p, "rx!", 1, xstore)) {
208*0Sstevel@tonic-gate 		p = POP(DS);
209*0Sstevel@tonic-gate 		d = pop_xforth(env);
210*0Sstevel@tonic-gate 		fc_reg_write(env, "rx!", p, d, NULL);
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate static void
215*0Sstevel@tonic-gate lpeek(fcode_env_t *env)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate 	fstack_t p;
218*0Sstevel@tonic-gate 	lforth_t r;
219*0Sstevel@tonic-gate 	int error;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "lpeek");
222*0Sstevel@tonic-gate 	p = POP(DS);
223*0Sstevel@tonic-gate 	r = (lforth_t)fc_reg_read(env, "rl@", p, &error);
224*0Sstevel@tonic-gate 	if (error)
225*0Sstevel@tonic-gate 		PUSH(DS, FALSE);
226*0Sstevel@tonic-gate 	else {
227*0Sstevel@tonic-gate 		PUSH(DS, r);
228*0Sstevel@tonic-gate 		PUSH(DS, TRUE);
229*0Sstevel@tonic-gate 	}
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate static void
233*0Sstevel@tonic-gate lpoke(fcode_env_t *env)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate 	fstack_t p, d;
236*0Sstevel@tonic-gate 	int error;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "lpoke");
239*0Sstevel@tonic-gate 	p = POP(DS);
240*0Sstevel@tonic-gate 	d = POP(DS);
241*0Sstevel@tonic-gate 	fc_reg_write(env, "rl!", p, d, &error);
242*0Sstevel@tonic-gate 	PUSH(DS, error ? FALSE : TRUE);
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate static void
246*0Sstevel@tonic-gate wpeek(fcode_env_t *env)
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 	fstack_t p;
249*0Sstevel@tonic-gate 	int error;
250*0Sstevel@tonic-gate 	wforth_t r;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "wpeek");
253*0Sstevel@tonic-gate 	p = POP(DS);
254*0Sstevel@tonic-gate 	r = (wforth_t)fc_reg_read(env, "rw@", p, &error);
255*0Sstevel@tonic-gate 	if (error)
256*0Sstevel@tonic-gate 		PUSH(DS, FALSE);
257*0Sstevel@tonic-gate 	else {
258*0Sstevel@tonic-gate 		PUSH(DS, r);
259*0Sstevel@tonic-gate 		PUSH(DS, TRUE);
260*0Sstevel@tonic-gate 	}
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate static void
264*0Sstevel@tonic-gate wpoke(fcode_env_t *env)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	fstack_t p, d;
267*0Sstevel@tonic-gate 	int error;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "wpoke");
270*0Sstevel@tonic-gate 	p = POP(DS);
271*0Sstevel@tonic-gate 	d = POP(DS);
272*0Sstevel@tonic-gate 	fc_reg_write(env, "rw!", p, d, &error);
273*0Sstevel@tonic-gate 	PUSH(DS, error ? FALSE : TRUE);
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate static void
277*0Sstevel@tonic-gate cpeek(fcode_env_t *env)
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate 	fstack_t	p;
280*0Sstevel@tonic-gate 	uchar_t r;
281*0Sstevel@tonic-gate 	int error;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "cpeek");
284*0Sstevel@tonic-gate 	p = POP(DS);
285*0Sstevel@tonic-gate 	r = (uchar_t)fc_reg_read(env, "rb@", p, &error);
286*0Sstevel@tonic-gate 	if (error)
287*0Sstevel@tonic-gate 		PUSH(DS, FALSE);
288*0Sstevel@tonic-gate 	else {
289*0Sstevel@tonic-gate 		PUSH(DS, r);
290*0Sstevel@tonic-gate 		PUSH(DS, TRUE);
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate }
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate static void
295*0Sstevel@tonic-gate cpoke(fcode_env_t *env)
296*0Sstevel@tonic-gate {
297*0Sstevel@tonic-gate 	fstack_t	p, d;
298*0Sstevel@tonic-gate 	int error;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "cpoke");
301*0Sstevel@tonic-gate 	p = POP(DS);
302*0Sstevel@tonic-gate 	d = POP(DS);
303*0Sstevel@tonic-gate 	fc_reg_write(env, "rb!", p, d, &error);
304*0Sstevel@tonic-gate 	PUSH(DS, error ? FALSE : TRUE);
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate /*
308*0Sstevel@tonic-gate  * fcdriver version of cfetch, replaces base 'c@'
309*0Sstevel@tonic-gate  */
310*0Sstevel@tonic-gate static void
311*0Sstevel@tonic-gate fcd_cfetch(fcode_env_t *env)
312*0Sstevel@tonic-gate {
313*0Sstevel@tonic-gate 	fstack_t addr = TOS;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "c@");
316*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "c@", 0, rbfetch))
317*0Sstevel@tonic-gate 		cfetch(env);
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate /*
321*0Sstevel@tonic-gate  * fcdriver version of cstore, replaces base 'c!'
322*0Sstevel@tonic-gate  */
323*0Sstevel@tonic-gate static void
324*0Sstevel@tonic-gate fcd_cstore(fcode_env_t *env)
325*0Sstevel@tonic-gate {
326*0Sstevel@tonic-gate 	fstack_t addr = TOS;
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "c!");
329*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "c!", 0, rbstore))
330*0Sstevel@tonic-gate 		cstore(env);
331*0Sstevel@tonic-gate }
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate /*
334*0Sstevel@tonic-gate  * fcdriver version of wfetch, replaces base 'w@'
335*0Sstevel@tonic-gate  */
336*0Sstevel@tonic-gate static void
337*0Sstevel@tonic-gate fcd_wfetch(fcode_env_t *env)
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate 	fstack_t addr = TOS;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "w@");
342*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "w@", 0, rwfetch))
343*0Sstevel@tonic-gate 		wfetch(env);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate /*
347*0Sstevel@tonic-gate  * fcdriver version of wstore, replaces base 'w!'
348*0Sstevel@tonic-gate  */
349*0Sstevel@tonic-gate static void
350*0Sstevel@tonic-gate fcd_wstore(fcode_env_t *env)
351*0Sstevel@tonic-gate {
352*0Sstevel@tonic-gate 	fstack_t addr = TOS;
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "w!");
355*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "w!", 0, rwstore))
356*0Sstevel@tonic-gate 		wstore(env);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate /*
360*0Sstevel@tonic-gate  * fcdriver version of lfetch, replaces base 'l@'
361*0Sstevel@tonic-gate  */
362*0Sstevel@tonic-gate static void
363*0Sstevel@tonic-gate fcd_lfetch(fcode_env_t *env)
364*0Sstevel@tonic-gate {
365*0Sstevel@tonic-gate 	fstack_t addr = TOS;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "l@");
368*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "l@", 0, rlfetch))
369*0Sstevel@tonic-gate 		lfetch(env);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate /*
373*0Sstevel@tonic-gate  * fcdriver version of lstore, replaces base 'l!'
374*0Sstevel@tonic-gate  */
375*0Sstevel@tonic-gate static void
376*0Sstevel@tonic-gate fcd_lstore(fcode_env_t *env)
377*0Sstevel@tonic-gate {
378*0Sstevel@tonic-gate 	fstack_t addr = TOS;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "l!");
381*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "l!", 0, rlstore))
382*0Sstevel@tonic-gate 		lstore(env);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate /*
386*0Sstevel@tonic-gate  * fcdriver version of xfetch, replaces base 'x@'
387*0Sstevel@tonic-gate  */
388*0Sstevel@tonic-gate static void
389*0Sstevel@tonic-gate fcd_xfetch(fcode_env_t *env)
390*0Sstevel@tonic-gate {
391*0Sstevel@tonic-gate 	fstack_t addr = TOS;
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "x@");
394*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "x@", 0, rxfetch))
395*0Sstevel@tonic-gate 		xfetch(env);
396*0Sstevel@tonic-gate }
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate /*
399*0Sstevel@tonic-gate  * fcdriver version of xstore, replaces base 'x!'
400*0Sstevel@tonic-gate  */
401*0Sstevel@tonic-gate static void
402*0Sstevel@tonic-gate fcd_xstore(fcode_env_t *env)
403*0Sstevel@tonic-gate {
404*0Sstevel@tonic-gate 	fstack_t addr = TOS;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "x!");
407*0Sstevel@tonic-gate 	if (!check_address_abuse(env, addr, "x!", 0, rxstore))
408*0Sstevel@tonic-gate 		xstore(env);
409*0Sstevel@tonic-gate }
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate /*
412*0Sstevel@tonic-gate  * fcdriver version of move, replaces base 'move'
413*0Sstevel@tonic-gate  */
414*0Sstevel@tonic-gate static void
415*0Sstevel@tonic-gate fcd_move(fcode_env_t *env)
416*0Sstevel@tonic-gate {
417*0Sstevel@tonic-gate 	size_t len;
418*0Sstevel@tonic-gate 	uchar_t *destaddr, *srcaddr;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "move");
421*0Sstevel@tonic-gate 	len = POP(DS);
422*0Sstevel@tonic-gate 	destaddr = ((uchar_t *)POP(DS));
423*0Sstevel@tonic-gate 	srcaddr = ((uchar_t *)POP(DS));
424*0Sstevel@tonic-gate 	for (; len > 0; len--, srcaddr++, destaddr++) {
425*0Sstevel@tonic-gate 		PUSH(DS, (fstack_t)srcaddr);
426*0Sstevel@tonic-gate 		fcd_cfetch(env);
427*0Sstevel@tonic-gate 		PUSH(DS, (fstack_t)destaddr);
428*0Sstevel@tonic-gate 		fcd_cstore(env);
429*0Sstevel@tonic-gate 	}
430*0Sstevel@tonic-gate }
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate static void
433*0Sstevel@tonic-gate fcd_comp(fcode_env_t *env)
434*0Sstevel@tonic-gate {
435*0Sstevel@tonic-gate 	char *str1, *str2, byte1, byte2;
436*0Sstevel@tonic-gate 	size_t len;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 3, "comp");
439*0Sstevel@tonic-gate 	len  = (size_t)POP(DS);
440*0Sstevel@tonic-gate 	str1 = (char *)POP(DS);
441*0Sstevel@tonic-gate 	str2 = (char *)POP(DS);
442*0Sstevel@tonic-gate 	for (; len > 0; len--, str1++, str2++) {
443*0Sstevel@tonic-gate 		PUSH(DS, (fstack_t)str1);
444*0Sstevel@tonic-gate 		fcd_cfetch(env);
445*0Sstevel@tonic-gate 		byte1 = POP(DS);
446*0Sstevel@tonic-gate 		PUSH(DS, (fstack_t)str2);
447*0Sstevel@tonic-gate 		fcd_cfetch(env);
448*0Sstevel@tonic-gate 		byte2 = POP(DS);
449*0Sstevel@tonic-gate 		if (byte1 > byte2) {
450*0Sstevel@tonic-gate 			PUSH(DS, -1);
451*0Sstevel@tonic-gate 			return;
452*0Sstevel@tonic-gate 		}
453*0Sstevel@tonic-gate 		if (byte1 < byte2) {
454*0Sstevel@tonic-gate 			PUSH(DS, 1);
455*0Sstevel@tonic-gate 			return;
456*0Sstevel@tonic-gate 		}
457*0Sstevel@tonic-gate 	}
458*0Sstevel@tonic-gate 	PUSH(DS, 0);
459*0Sstevel@tonic-gate }
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate char *
462*0Sstevel@tonic-gate get_eeprom_value(fcode_env_t *env, char *name)
463*0Sstevel@tonic-gate {
464*0Sstevel@tonic-gate 	FILE *fd;
465*0Sstevel@tonic-gate 	char buf[80], *p;
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	sprintf(buf, "eeprom '%s'", name);
468*0Sstevel@tonic-gate 	if ((fd = popen(buf, "r")) == NULL)
469*0Sstevel@tonic-gate 		return (NULL);
470*0Sstevel@tonic-gate 	fgets(buf, sizeof (buf), fd);
471*0Sstevel@tonic-gate 	pclose(fd);
472*0Sstevel@tonic-gate 	if ((p = strchr(buf, '\n')) != NULL)
473*0Sstevel@tonic-gate 		*p = '\0';
474*0Sstevel@tonic-gate 	if ((p = strchr(buf, '=')) != NULL)
475*0Sstevel@tonic-gate 		return (p + 1);
476*0Sstevel@tonic-gate 	return (NULL);
477*0Sstevel@tonic-gate }
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate static void
480*0Sstevel@tonic-gate local_mac_address(fcode_env_t *env)
481*0Sstevel@tonic-gate {
482*0Sstevel@tonic-gate 	char *mac_str;
483*0Sstevel@tonic-gate 	int mac_value;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	mac_str = get_eeprom_value(env, "local-mac-address?");
486*0Sstevel@tonic-gate 	if (mac_str != NULL && strcmp(mac_str, "true") == 0)
487*0Sstevel@tonic-gate 		mac_value = TRUE;
488*0Sstevel@tonic-gate 	else
489*0Sstevel@tonic-gate 		mac_value = FALSE;
490*0Sstevel@tonic-gate 	PUSH(DS, mac_value);
491*0Sstevel@tonic-gate }
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate /*
494*0Sstevel@tonic-gate  * Allow for programmatic over-ride of 'mac-address'
495*0Sstevel@tonic-gate  */
496*0Sstevel@tonic-gate #define	MAC_ADDR_SIZE	6
497*0Sstevel@tonic-gate static char *mac_addr;
498*0Sstevel@tonic-gate static int mac_addr_is_valid;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate void
501*0Sstevel@tonic-gate set_mac_address(char *macaddr)
502*0Sstevel@tonic-gate {
503*0Sstevel@tonic-gate 	mac_addr_is_valid = 1;
504*0Sstevel@tonic-gate 	memcpy(mac_addr, macaddr, MAC_ADDR_SIZE);
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate void
508*0Sstevel@tonic-gate push_mac_address(fcode_env_t *env)
509*0Sstevel@tonic-gate {
510*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)mac_addr);
511*0Sstevel@tonic-gate 	PUSH(DS, MAC_ADDR_SIZE);
512*0Sstevel@tonic-gate }
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate /*
515*0Sstevel@tonic-gate  * Does driver call to get this.
516*0Sstevel@tonic-gate  */
517*0Sstevel@tonic-gate static void
518*0Sstevel@tonic-gate local_ether_addr(fcode_env_t *env)
519*0Sstevel@tonic-gate {
520*0Sstevel@tonic-gate 	static fc_cell_t *mac_add;
521*0Sstevel@tonic-gate 	int error;
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 	mac_add = MALLOC(sizeof (fc_cell_t) * 2);
524*0Sstevel@tonic-gate 	error = fc_run_priv(env->private, "local-ether-addr", 0, 2, &mac_add[0],
525*0Sstevel@tonic-gate 	    &mac_add[1]);
526*0Sstevel@tonic-gate 	if (error) {
527*0Sstevel@tonic-gate 		bzero(mac_add, sizeof (mac_add));
528*0Sstevel@tonic-gate 	}
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&mac_add[0]);
531*0Sstevel@tonic-gate 	PUSH(DS, 6);
532*0Sstevel@tonic-gate }
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate /*
535*0Sstevel@tonic-gate  * 'mac-address' - complicated by 'local-mac-address' stuff.
536*0Sstevel@tonic-gate  */
537*0Sstevel@tonic-gate static void
538*0Sstevel@tonic-gate mac_address(fcode_env_t *env)
539*0Sstevel@tonic-gate {
540*0Sstevel@tonic-gate 	fstack_t d;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	if (mac_addr_is_valid) {
543*0Sstevel@tonic-gate 		push_mac_address(env);
544*0Sstevel@tonic-gate 		return;
545*0Sstevel@tonic-gate 	}
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	/*
548*0Sstevel@tonic-gate 	 * From here, we essentially re-implement OBP's 'mac-address' word.
549*0Sstevel@tonic-gate 	 * on some platforms, this may need to be re-implemented.
550*0Sstevel@tonic-gate 	 */
551*0Sstevel@tonic-gate 	local_mac_address(env);
552*0Sstevel@tonic-gate 	d = POP(DS);
553*0Sstevel@tonic-gate 	if (d) {
554*0Sstevel@tonic-gate 		push_a_string(env, "local-mac-address");
555*0Sstevel@tonic-gate 		get_inherited_prop(env);
556*0Sstevel@tonic-gate 		d = POP(DS);
557*0Sstevel@tonic-gate 		if (d == FALSE && TOS == 6)
558*0Sstevel@tonic-gate 			return;
559*0Sstevel@tonic-gate 		two_drop(env);
560*0Sstevel@tonic-gate 	}
561*0Sstevel@tonic-gate 	local_ether_addr(env);
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate /*
565*0Sstevel@tonic-gate  * Allow for the programmatic setting of diagnostic-mode?
566*0Sstevel@tonic-gate  */
567*0Sstevel@tonic-gate static int diag_mode_is_valid = 0;
568*0Sstevel@tonic-gate static int diag_mode = 0;
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate void
571*0Sstevel@tonic-gate set_diagnostic_mode(fcode_env_t *env)
572*0Sstevel@tonic-gate {
573*0Sstevel@tonic-gate 	fstack_t d = POP(DS);
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	diag_mode = d;
576*0Sstevel@tonic-gate 	diag_mode_is_valid = 1;
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate void
580*0Sstevel@tonic-gate push_diagnostic_mode(fcode_env_t *env)
581*0Sstevel@tonic-gate {
582*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)diag_mode);
583*0Sstevel@tonic-gate }
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate /*
586*0Sstevel@tonic-gate  * 'diagnostic-mode?' - diagnostic-mode? is equivalent to NVRAM 'diag-switch?'
587*0Sstevel@tonic-gate  */
588*0Sstevel@tonic-gate static void
589*0Sstevel@tonic-gate diagnostic_mode(fcode_env_t *env)
590*0Sstevel@tonic-gate {
591*0Sstevel@tonic-gate 	char *diag_str;
592*0Sstevel@tonic-gate 	int diag_value;
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if (!diag_mode_is_valid) {
595*0Sstevel@tonic-gate 		diag_str = get_eeprom_value(env, "diag-switch?");
596*0Sstevel@tonic-gate 		if (diag_str != NULL && strcmp(diag_str, "false") == 0)
597*0Sstevel@tonic-gate 			diag_value = FALSE;
598*0Sstevel@tonic-gate 		else
599*0Sstevel@tonic-gate 			diag_value = TRUE;
600*0Sstevel@tonic-gate 		PUSH(DS, diag_value);
601*0Sstevel@tonic-gate 		set_diagnostic_mode(env);
602*0Sstevel@tonic-gate 	}
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	push_diagnostic_mode(env);
605*0Sstevel@tonic-gate }
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate /*
608*0Sstevel@tonic-gate  * May need to implement other memory-access Fcodes here (depending upon
609*0Sstevel@tonic-gate  * abuse), like fill, comp, +!, etc., etc.
610*0Sstevel@tonic-gate  */
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate #pragma init(_init)
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate static void
615*0Sstevel@tonic-gate _init(void)
616*0Sstevel@tonic-gate {
617*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	mac_addr = MALLOC(MAC_ADDR_SIZE);
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	ASSERT(env);
622*0Sstevel@tonic-gate 	NOTICE;
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	ANSI(0x06e, 0,		"l@",			fcd_lfetch);
625*0Sstevel@tonic-gate 	ANSI(0x06f, 0,		"w@",			fcd_wfetch);
626*0Sstevel@tonic-gate 	ANSI(0x071, 0,		"c@",			fcd_cfetch);
627*0Sstevel@tonic-gate 	ANSI(0x073, 0,		"l!",			fcd_lstore);
628*0Sstevel@tonic-gate 	ANSI(0x074, 0,		"w!",			fcd_wstore);
629*0Sstevel@tonic-gate 	ANSI(0x075, 0,		"c!",			fcd_cstore);
630*0Sstevel@tonic-gate 	ANSI(0x078, 0,		"move",			fcd_move);
631*0Sstevel@tonic-gate 	ANSI(0x07a, 0,		"comp",			fcd_comp);
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	ANSI(0x120, 0,		"diagnostic-mode?",	diagnostic_mode);
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	ANSI(0x1a4, 0,		"mac-address",		mac_address);
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	P1275(0x220, 0,		"cpeek",		cpeek);
638*0Sstevel@tonic-gate 	P1275(0x221, 0,		"wpeek",		wpeek);
639*0Sstevel@tonic-gate 	P1275(0x222, 0,		"lpeek",		lpeek);
640*0Sstevel@tonic-gate 	P1275(0x223, 0,		"cpoke",		cpoke);
641*0Sstevel@tonic-gate 	P1275(0x224, 0,		"wpoke",		wpoke);
642*0Sstevel@tonic-gate 	P1275(0x225, 0,		"lpoke",		lpoke);
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	P1275(0x230, 0,		"rb@",			rbfetch);
645*0Sstevel@tonic-gate 	P1275(0x231, 0,		"rb!",			rbstore);
646*0Sstevel@tonic-gate 	P1275(0x232, 0,		"rw@",			rwfetch);
647*0Sstevel@tonic-gate 	P1275(0x233, 0,		"rw!",			rwstore);
648*0Sstevel@tonic-gate 	P1275(0x234, 0,		"rl@",			rlfetch);
649*0Sstevel@tonic-gate 	P1275(0x235, 0,		"rl!",			rlstore);
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	P1275(0x246,	0,	"x@",			fcd_xfetch);
652*0Sstevel@tonic-gate 	P1275(0x247,	0,	"x!",			fcd_xstore);
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	P1275(0x22e,	0,	"rx@",			rxfetch);
655*0Sstevel@tonic-gate 	P1275(0x22f,	0,	"rx!",			rxstore);
656*0Sstevel@tonic-gate 	FORTH(0,		"set-diagnostic-mode",	set_diagnostic_mode);
657*0Sstevel@tonic-gate 	FORTH(0,		"local-mac-address?",	local_mac_address);
658*0Sstevel@tonic-gate 	FORTH(0,		"local-ether-addr",	local_ether_addr);
659*0Sstevel@tonic-gate }
660