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