-
Notifications
You must be signed in to change notification settings - Fork 0
/
rom.c
1761 lines (1471 loc) · 37.2 KB
/
rom.c
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This either loads iquik.b (BE) or veneer.exe (LE) and
* emulates just enough OF CIF. Because I am lazy, this uses
* libfdt and a DTB blob. Given these circumstances,
* ihandles and phandles are equivalent.
*/
#define LOG_PFX ROM
#include "guest.h"
#include "rom.h"
#include "ranges.h"
#include "mmu_ranges.h"
#include "pmem.h"
#include "libfdt.h"
#include "term.h"
#include "list.h"
#include "mon.h"
#include "disk.h"
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#define PHANDLE_MUNGE 0x10000000
#define ROOT_PHANDLE rom_get_phandle(0)
#define CELL(x, i) (x + i * sizeof(cell_t))
#define CIA_SERVICE CELL(cia, 0)
#define CIA_IN CELL(cia, 1)
#define CIA_OUT CELL(cia, 2)
#define CIA_ARG(x) ({ \
BUG_ON(x >= cia_in, "arg %u out of bounds", x); \
CELL(cia, (3 + (x))); \
})
#define CIA_RET(x) ({ \
BUG_ON(x >= cia_out, "ret %u out of bounds", x); \
CELL(cia, (3 + cia_in + (x))); \
})
typedef uint32_t cell_t;
typedef cell_t phandle_t;
typedef cell_t ihandle_t;
static void *fdt;
static int memory_node;
static struct list_head known_ihandles;
static ihandle_t mmu_ihandle;
static ihandle_t memory_ihandle;
static ranges_t guest_mem_avail_ranges;
static ranges_t guest_mem_reg_ranges;
static mmu_ranges_t rom_mmu_ranges;
static gra_t cif_trampoline;
static gra_t claim_arena_start;
static gra_t claim_arena_ptr;
static gra_t claim_arena_end;
static gra_t stack_start;
static gra_t stack_end;
static uint8_t xfer_buf[PAGE_SIZE];
struct ihandle_methods;
typedef count_t (*ihandle_write_t)(struct ihandle_methods *im,
const uint8_t *b, count_t len);
typedef count_t (*ihandle_read_t)(struct ihandle_methods *im,
uint8_t *b, count_t len);
typedef err_t (*ihandle_seek_t)(struct ihandle_methods *im,
offset_t offset);
typedef void (*ihandle_close_t)(struct ihandle_methods *im);
typedef struct ihandle_methods {
ihandle_write_t write;
ihandle_read_t read;
ihandle_seek_t seek;
ihandle_close_t close;
} ihandle_methods_t;
typedef enum ihandle_type {
IHANDLE_BOGUS,
IHANDLE_WRAPPED,
IHANDLE_FILE,
IHANDLE_DISK,
} ihandle_type_t;
typedef struct ihandle_header {
struct list_head link;
ihandle_type_t type;
ihandle_t value;
ihandle_methods_t methods;
} ihandle_header_t;
typedef struct ihandle_file {
ihandle_header_t header;
int fd;
char *path;
} ihandle_file_t;
typedef struct ihandle_disk {
ihandle_header_t header;
disk_t *disk;
disk_part_t part;
offset_t current_off;
char *path;
} ihandle_disk_t;
typedef struct {
err_t (*handler)(gea_t cia, count_t cia_in,
count_t cia_out);
const char *name;
} cif_handler_t;
static phandle_t
rom_get_phandle(int node)
{
return node | PHANDLE_MUNGE;
}
static ihandle_t
rom_get_ihandle(int node)
{
return fdt_get_phandle(fdt, node);
}
static ihandle_t
rom_path_to_ihandle(const char *path)
{
int node = fdt_path_offset(fdt, path);
if (node == -1) {
return -1;
}
return rom_get_ihandle(node);
}
static int
rom_node_offset_by_phandle(phandle_t phandle)
{
return phandle &= ~PHANDLE_MUNGE;
}
static int
rom_node_offset_by_ihandle(ihandle_t ihandle)
{
return fdt_node_offset_by_phandle(fdt, ihandle);
}
static void
rom_mon_dump_known_ihandles(void)
{
ihandle_header_t *header;
list_for_each_entry(header, &known_ihandles, link) {
switch (header->type) {
case IHANDLE_WRAPPED: {
int node = rom_node_offset_by_ihandle(header->value);
const char *p = "<UNKNOWN>";
if (node > 0 &&
fdt_get_path(fdt, node, (char *) xfer_buf,
sizeof(xfer_buf)) >= 0) {
p = (const char *) xfer_buf;
}
mon_printf("0x%08x: wrapped (path '%s')\n",
header->value, p);
break;
}
case IHANDLE_FILE: {
ihandle_file_t *f = container_of(header, ihandle_file_t, header);
mon_printf("0x%08x: file (fd %u, path = '%s')\n",
header->value, f->fd, f->path);
break;
}
case IHANDLE_DISK: {
ihandle_disk_t *d = container_of(header, ihandle_disk_t, header);
mon_printf("0x%08x: disk (path = '%s')\n",
header->value, d->path);
break;
}
case IHANDLE_BOGUS:
default:
mon_printf("0x%08x: <corrupt ihandle entry>\n",
header->value);
}
}
}
static ihandle_methods_t *
rom_methods_by_ihandle(ihandle_t ihandle)
{
ihandle_header_t *header;
list_for_each_entry(header, &known_ihandles, link) {
if (header->value == ihandle) {
return &header->methods;
}
}
return NULL;
}
static err_t
rom_wrap_ihandle_with_methods(ihandle_t t,
ihandle_write_t write,
ihandle_read_t read,
ihandle_seek_t seek,
ihandle_close_t close)
{
ihandle_header_t *h;
h = malloc(sizeof(ihandle_header_t));
if (h == NULL) {
return ERR_NO_MEM;
}
h->value = t;
h->type = IHANDLE_WRAPPED;
h->methods.write = write;
h->methods.read = read;
h->methods.seek = seek;
h->methods.close = close;
list_add_tail(&h->link, &known_ihandles);
return ERR_NONE;
}
static err_t
rom_file_seek(ihandle_methods_t *im,
offset_t offset)
{
int ret;
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_file_t *f = container_of(h, ihandle_file_t, header);
ret = lseek(f->fd, offset, SEEK_SET);
if (ret < 0) {
return ERR_POSIX;
}
return ERR_NONE;
}
static count_t
rom_file_read(ihandle_methods_t *im,
uint8_t *s,
count_t len)
{
int ret;
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_file_t *f = container_of(h, ihandle_file_t, header);
ret = read(f->fd, s, len);
if (ret < 0) {
ret = 0;
}
return ret;
}
static count_t
rom_file_write(ihandle_methods_t *im,
const uint8_t *s,
count_t len)
{
int ret;
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_file_t *f = container_of(h, ihandle_file_t, header);
ret = write(f->fd, s, len);
if (ret < 0) {
ret = 0;
}
return ret;
}
static void
rom_file_close(struct ihandle_methods *im)
{
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_file_t *f = container_of(h, ihandle_file_t, header);
close(f->fd);
list_del(&h->link);
free(f->path);
free(f);
}
static err_t
rom_disk_seek(ihandle_methods_t *im,
offset_t offset)
{
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_disk_t *d = container_of(h, ihandle_disk_t, header);
if (offset > d->part.length) {
return ERR_OUT_OF_BOUNDS;
}
d->current_off = offset;
return ERR_NONE;
}
static count_t
rom_disk_read(ihandle_methods_t *im,
uint8_t *s,
count_t len)
{
length_t c;
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_disk_t *d = container_of(h, ihandle_disk_t, header);
c = disk_seek(d->disk, d->part.off + d->current_off);
if (c != ERR_NONE) {
return 0;
}
len = min(len, d->part.length - d->current_off);
c = disk_in(d->disk, s, len);
d->current_off += c;
return c;
}
static count_t
rom_disk_write(ihandle_methods_t *im,
const uint8_t *s,
count_t len)
{
length_t c;
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_disk_t *d = container_of(h, ihandle_disk_t, header);
c = disk_seek(d->disk, d->part.off + d->current_off);
if (c != ERR_NONE) {
return 0;
}
len = min(len, d->part.length - d->current_off);
c = disk_out(d->disk, s, len);
d->current_off += c;
return c;
}
static void
rom_disk_close(struct ihandle_methods *im)
{
ihandle_header_t *h = container_of(im, ihandle_header_t, methods);
ihandle_disk_t *d = container_of(h, ihandle_disk_t, header);
disk_close(d->disk);
list_del(&h->link);
free(d->path);
free(d);
}
static err_t
rom_ihandle_from_file(const char *path,
const char *full_path,
ihandle_t *ihandle)
{
int fd;
ihandle_file_t *f;
char *dup_path;
fd = open(path, O_RDWR);
if (fd < 0) {
return ERR_POSIX;
}
f = malloc(sizeof(ihandle_file_t));
if (f == NULL) {
return ERR_NO_MEM;
}
dup_path = strdup(full_path);
if (dup_path == NULL) {
free(f);
return ERR_NO_MEM;
}
f->header.type = IHANDLE_FILE;
f->header.value = (ihandle_t) f;
f->header.methods.write = rom_file_write;
f->header.methods.read = rom_file_read;
f->header.methods.seek = rom_file_seek;
f->header.methods.close = rom_file_close;
f->fd = fd;
f->path = dup_path;
list_add_tail(&f->header.link, &known_ihandles);
*ihandle = f->header.value;
return ERR_NONE;
}
static err_t
rom_ihandle_from_disk(disk_t *disk,
disk_part_t *part,
const char *full_path,
ihandle_t *ihandle)
{
ihandle_disk_t *d;
char *dup_path;
d = malloc(sizeof(ihandle_disk_t));
if (d == NULL) {
return ERR_NO_MEM;
}
dup_path = strdup(full_path);
if (dup_path == NULL) {
free(d);
return ERR_NO_MEM;
}
d->header.type = IHANDLE_DISK;
d->header.value = (ihandle_t) d;
d->header.methods.write = rom_disk_write;
d->header.methods.read = rom_disk_read;
d->header.methods.seek = rom_disk_seek;
d->header.methods.close = rom_disk_close;
d->path = dup_path;
d->disk = disk;
d->part = *part;
list_add_tail(&d->header.link, &known_ihandles);
*ihandle = d->header.value;
return ERR_NONE;
}
static uint32_t
rom_claim_ex(uint32_t addr,
uint32_t size,
uint32_t align)
{
uint32_t out;
/*
* This implementation doesn't comply with
* IEEE-1275. Notably, it doesn't fail if
* a range is already claimed.
*/
if (align != 0) {
claim_arena_ptr =
ALIGN_UP(claim_arena_ptr, align);
if (claim_arena_ptr >=
claim_arena_end) {
WARN("claim arena overflow");
return -1;
}
out = (uint32_t) claim_arena_ptr;
claim_arena_ptr += size;
if (claim_arena_ptr >
claim_arena_end) {
WARN("claim arena overflow");
return -1;
}
} else {
if (addr + size > pmem_size()) {
WARN("claimed [0x%x, 0x%lx) is outside pmem",
addr, addr + size);
return -1;
}
out = addr;
}
/*
* Internally we keep track of all allocations at page granularity,
* due to some buggy handling of non-page aligned ranges in veneer.
*/
range_remove(&guest_mem_avail_ranges,
ALIGN(out, PAGE_SIZE),
ALIGN_UP(out + size, PAGE_SIZE) - 1);
return out;
}
static err_t
rom_milliseconds(gea_t cia,
count_t cia_in,
count_t cia_out)
{
err_t err;
cell_t ms;
struct timeval te;
gettimeofday(&te, NULL);
ms = te.tv_sec * 1000 + te.tv_usec / 1000;
err = guest_to_x(CIA_RET(0), &ms);
ON_ERROR("out", err, done);
done:
return err;
}
static err_t
rom_claim(gea_t cia,
count_t cia_in,
count_t cia_out)
{
err_t err;
cell_t addr;
cell_t size;
cell_t align;
cell_t out;
err = guest_from_x(&addr, CIA_ARG(0));
ON_ERROR("addr", err, done);
err = guest_from_x(&size, CIA_ARG(1));
ON_ERROR("size", err, done);
err = guest_from_x(&align, CIA_ARG(2));
ON_ERROR("align", err, done);
out = rom_claim_ex(addr, size, align);
err = guest_to_x(CIA_RET(0), &out);
ON_ERROR("out", err, done);
done:
return err;
}
static err_t
rom_mem_call(gea_t cia,
const char *call,
count_t cia_in,
count_t cia_out)
{
err_t err;
cell_t align;
cell_t size;
gra_t addr;
cell_t result;
if (strcmp("claim", call)) {
return ERR_UNSUPPORTED;
}
err = guest_from_x(&align, CIA_ARG(2));
ON_ERROR("mclaim align", err, done);
err = guest_from_x(&size, CIA_ARG(3));
ON_ERROR("mclaim size", err, done);
err = guest_from_x(&addr, CIA_ARG(4));
ON_ERROR("mclaim addr", err, done);
result = rom_claim_ex(addr, size, align);
err = guest_to_x(CIA_RET(1), &result);
ON_ERROR("mclaim result", err, done);
done:
return err;
}
static err_t
rom_mmu_call(gea_t cia,
const char *call,
count_t cia_in,
count_t cia_out)
{
err_t err;
gra_t phys;
gea_t virt;
cell_t size;
cell_t mode;
if (strcmp("map", call)) {
return ERR_UNSUPPORTED;
}
err = guest_from_x(&mode, CIA_ARG(2));
ON_ERROR("mmap mode", err, done);
err = guest_from_x(&size, CIA_ARG(3));
ON_ERROR("mmap size", err, done);
err = guest_from_x(&virt, CIA_ARG(4));
ON_ERROR("mmap virt", err, done);
err = guest_from_x(&phys, CIA_ARG(5));
ON_ERROR("mmap phys", err, done);
if (mode == -2) {
/*
* Seen with veneer.exe. Weird.
*/
WARN("treating non-standard mode %d as -1", (int) mode);
mode = -1;
}
/*
* For default mode (-1), the values for WIMGxPP
* for memory shall be W=0, I=0, M=0, G=1, PP=10.
* For I/O, these shall be W=1, I=1, M=0, G=1, PP=10.
*/
BUG_ON(mode != -1, "non-default mode 0x%x not supported",
mode);
if (phys == virt) {
return ERR_NONE;
}
if (!pmem_gra_valid(phys)) {
return ERR_BAD_ACCESS;
}
BUG_ON((phys & PAGE_MASK) != 0, "bad alignment");
BUG_ON((virt & PAGE_MASK) != 0, "bad alignment");
size = ALIGN(size, PAGE_SIZE);
mmu_range_add(&rom_mmu_ranges, virt, virt + size - 1,
phys, 0);
done:
return err;
}
static err_t
rom_callmethod(gea_t cia,
count_t cia_in,
count_t cia_out)
{
err_t err;
gea_t method_ea;
ihandle_t ihandle;
char *call;
cell_t result;
err = guest_from_x(&method_ea, CIA_ARG(0));
ON_ERROR("method ea", err, done);
err = guest_from_x(&ihandle, CIA_ARG(1));
ON_ERROR("ihandle", err, done);
call = (char *) xfer_buf;
call[guest_from_ex(call, method_ea, sizeof(xfer_buf) - 1, 1, true)] = '\0';
if (ihandle == memory_ihandle) {
err = rom_mem_call(cia, call, cia_in, cia_out);
} else if (ihandle == mmu_ihandle) {
err = rom_mmu_call(cia, call, cia_in, cia_out);
} else {
err = ERR_UNSUPPORTED;
}
/*
* outer (call-method) result.
*/
result = err == ERR_NONE ? 0 : -1;
err = guest_to_x(CIA_RET(0), &result);
done:
return err;
}
static count_t
rom_console_read(ihandle_methods_t *im,
uint8_t *s,
count_t len)
{
return term_in((char *) s, len);
}
static count_t
rom_console_write(ihandle_methods_t *im,
const uint8_t *s,
count_t len)
{
while (len--) {
if (*s == 0x9b) {
term_out("\33[", 2);
} else if (*s == 0xcd) {
term_out("=", 1);
} else if (*s == 0xba) {
term_out("|", 1);
} else if (*s == 0xbb ||
*s == 0xc8) {
term_out("\\", 1);
} else if (*s == 0xbc ||
*s == 0xc9) {
term_out("/", 1);
} else {
term_out((const char *) s, 1);
}
s++;
}
return len;
}
err_t
rom_init(const char *fdt_path)
{
int fd;
int ret;
gea_t loader_base;
gea_t loader_entry;
length_t loader_length;
length_t loaded_length;
struct stat st;
const char *loader;
void *loader_data;
gea_t stack_base;
ihandle_t console_ihandle;
err_t err = ERR_NONE;
BUG_ON(pmem_size() <= MB(16), "guest RAM too small");
INIT_LIST_HEAD(&known_ihandles);
mmu_range_init(&rom_mmu_ranges);
/*
* Map everything 1:1.
*/
mmu_range_add(&rom_mmu_ranges, 0, pmem_size() - 1, 0, 0);
range_init(&guest_mem_avail_ranges);
range_init(&guest_mem_reg_ranges);
range_add(&guest_mem_avail_ranges, 0, pmem_size() - 1);
range_add(&guest_mem_reg_ranges, 0, pmem_size() - 1);
claim_arena_start = pmem_size() - MB(16);
claim_arena_ptr = claim_arena_start;
claim_arena_end = pmem_size();
if (guest_is_little()) {
loader = "veneer.exe";
/*
* Skip COFF header.
*/
loader_entry = 0x50000;
loader_base = loader_entry - 0x200;
} else {
loader = "iquik.b";
loader_entry = loader_base = 0x3e0000;
}
stack_base = 2 * vm_page_size;
uint32_t hvcall = 0x44000022; /* sc 1 */
fd = open(loader, O_RDONLY);
if (fd < 0) {
POSIX_ERROR(errno, "could not open loader '%s'", loader);
goto posix_err;
}
ret = fstat(fd, &st);
ON_POSIX_ERROR("stat", ret, posix_err);
loader_length = st.st_size;
loader_data = malloc(loader_length);
if (loader_data == NULL) {
POSIX_ERROR(errno, "could not alloc temp buffer for loader '%s'", loader);
goto posix_err;
}
ret = read(fd, loader_data, loader_length);
ON_POSIX_ERROR("read", ret, posix_err);
close(fd);
loaded_length = pmem_to(loader_base, loader_data, loader_length, 1);
free(loader_data);
if (loader_length != loaded_length) {
err = ERR_BAD_ACCESS;
ERROR(err, "could not copy in the loader");
goto done;
}
rom_claim_ex(loader_base, loader_length, 0);
fd = open(fdt_path, O_RDONLY);
if (fd < 0) {
POSIX_ERROR(errno, "could not open device tree '%s'", fdt_path);
goto posix_err;
}
ret = fstat(fd, &st);
ON_POSIX_ERROR("fdt stat", ret, posix_err);
fdt = malloc(st.st_size);
if (fdt == NULL) {
ERROR(errno, "couldn't alloc %u bytes to read fdt", st.st_size);
goto posix_err;
}
ret = read(fd, fdt, st.st_size);
ON_POSIX_ERROR("fdt read", ret, posix_err);
close(fd);
memory_node = fdt_path_offset(fdt, "mem");
BUG_ON(memory_node == -1, "memory node missing from DT template");
memory_ihandle = rom_get_ihandle(memory_node);
mmu_ihandle = rom_path_to_ihandle("mmu");
BUG_ON(mmu_ihandle == -1, "mmu node missing from DT template");
console_ihandle = rom_path_to_ihandle("con");
BUG_ON(console_ihandle == -1, "console node missing from DT template");
err = rom_wrap_ihandle_with_methods(console_ihandle,
rom_console_write,
rom_console_read,
NULL, NULL);
ON_ERROR("rom_wrap_ihandle_with_methods", err, done);
/*
* CIF entry.
*/
cif_trampoline = 0x4;
pmem_to(cif_trampoline, &hvcall, sizeof(hvcall), 4);
rom_claim_ex(cif_trampoline, sizeof(hvcall), 0);
/*
* Stack.
*/
stack_end = claim_arena_start;
stack_start = stack_end - MB(1);
rom_claim_ex(stack_start, stack_end - stack_start, 0);
/*
* Prep CPU state.
*/
guest->regs->ppcPC = loader_entry;
guest->regs->ppcGPRs[1] = stack_end - C_RED_ZONE;
guest->regs->ppcGPRs[5] = cif_trampoline;
done:
return err;
posix_err:
return ERR_POSIX;
}
static err_t
rom_getprop_ex(int node,
const char *name,
cell_t len_in,
gea_t *data_ea,
cell_t *len_out)
{
int len;
const void *data;
err_t err = ERR_NONE;
if (!strcmp(name, "name")) {
if (node == 0) {
data = "/";
len = 1;
} else {
data = fdt_get_name(fdt, node, &len);
}
} else if (node == memory_node &&
!strcmp(name, "reg")) {
len = range_count(&guest_mem_reg_ranges) *
sizeof(cell_t) * 2;
// XXX: is this working around a veneer bug?
if (len_in == 0) {
*len_out = len;
} else {
*len_out = min(len, len_in);
}
if (data_ea != NULL) {
range_t *r;
gea_t ea = *data_ea;
list_for_each_entry(r, &guest_mem_reg_ranges, link) {
length_t l;
if (len < sizeof(cell_t)) {
break;
}
err = guest_to(ea, &r->base, sizeof(cell_t), 1);
ON_ERROR("base", err, done);
len -= sizeof(cell_t);
ea += sizeof(cell_t);
if (len < sizeof(cell_t)) {
break;
}
l = r->limit - r->base + 1;
err = guest_to(ea, &l, sizeof(cell_t), 1);
ON_ERROR("size", err, done);
len -= sizeof(cell_t);
ea += sizeof(cell_t);
}
}
return ERR_NONE;
} else if (node == memory_node &&
!strcmp(name, "available")) {
len = range_count(&guest_mem_avail_ranges) *
sizeof(cell_t) * 2;
// XXX: is this working around a veneer bug?
if (len_in == 0) {
*len_out = len;
} else {
*len_out = min(len, len_in);
}
if (data_ea != NULL) {
range_t *r;
gea_t ea = *data_ea;
list_for_each_entry(r, &guest_mem_avail_ranges, link) {
/*
* Somehow veneer really doesn't like non-page aligned
* quantities in the "available" property.
*/
cell_t b = r->base;
cell_t bs = r->limit - r->base + 1;
BUG_ON(b % PAGE_SIZE != 0, "unexpected avail range 0x%x-0x%x",
r->base, r->limit);
BUG_ON(bs % PAGE_SIZE != 0, "unexpected avail range 0x%x-0x%x",
r->base, r->limit);
if (len < sizeof(cell_t)) {
break;
}
err = guest_to(ea, &b, sizeof(cell_t), 1);
ON_ERROR("base", err, done);
len -= sizeof(cell_t);
ea += sizeof(cell_t);
if (len < sizeof(cell_t)) {
break;
}
err = guest_to(ea, &bs, sizeof(cell_t), 1);
ON_ERROR("size", err, done);
len -= sizeof(cell_t);
ea += sizeof(cell_t);
}
}
return ERR_NONE;
} else {
data = fdt_getprop(fdt, node, name, &len);
}
if (data == NULL) {
WARN("property '%s' not found in node %u (%s)", name,
node, fdt_get_name(fdt, node, NULL));
*len_out = -1;
} else {
*len_out = len;
if (data_ea != NULL) {
err = guest_to(*data_ea, data, min(len, len_in), 1);
ON_ERROR("data", err, done);
}
}
done:
return err;
}
static err_t
rom_getprop(gea_t cia,
count_t cia_in,
count_t cia_out)
{
int node;
err_t err;
phandle_t phandle;
gea_t prop_ea;
gea_t data_ea;
cell_t len_in;
cell_t len_out;
char *p = (char *) xfer_buf;
err = guest_from_x(&phandle, CIA_ARG(0));
ON_ERROR("phandle", err, done);
err = guest_from_x(&prop_ea, CIA_ARG(1));
ON_ERROR("prop ea", err, done);
err = guest_from_x(&data_ea, CIA_ARG(2));
ON_ERROR("data ea", err, done);
err = guest_from_x(&len_in, CIA_ARG(3));
ON_ERROR("len in", err, done);
p[guest_from_ex(p, prop_ea, sizeof(xfer_buf), 1, true)] = '\0';
node = rom_node_offset_by_phandle(phandle);
if (node < 0) {
WARN("looking up '%s' in unknown phandle 0x%x",
p, phandle);
len_out = -1;
} else {
err = rom_getprop_ex(node, p, len_in,
&data_ea, &len_out);
if (err != ERR_NONE) {
goto done;
}
}