-
Notifications
You must be signed in to change notification settings - Fork 5
/
vmm.c
1026 lines (930 loc) · 31.5 KB
/
vmm.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
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <parlib/arch/arch.h>
#include <parlib/ros_debug.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <ros/syscall.h>
#include <sys/mman.h>
#include <coreboot_tables.h>
#include <vmm.h>
#include <acpi/acpi.h>
#include <ros/arch/mmu.h>
#include <ros/vmm.h>
#include <parlib/uthread.h>
#include <bootparam.h>
#include <virtio.h>
#include <virtio_mmio.h>
#include <virtio_ids.h>
#include <virtio_config.h>
int msrio(struct vmctl *vcpu, uint32_t opcode);
struct vmctl vmctl;
/* Kind of sad what a total clusterf the pc world is. By 1999, you could just scan the hardware
* and work it out. But 2005, that was no longer possible. How sad.
* so we have to fake acpi to make it all work. !@#$!@#$#.
* This will be copied to memory at 0xe0000, so the kernel can find it.
*/
/* assume they're all 256 bytes long just to make it easy. Just have pointers that point to aligned things. */
struct acpi_table_rsdp rsdp = {
.signature = "RSD PTR ",
.oem_id = "AKAROS",
.revision = 2,
.length = 36,
};
struct acpi_table_xsdt xsdt = {
.header = {
.signature= "XSDT",
// This is so stupid. Incredibly stupid.
.revision = 0,
.oem_id = "AKAROS",
.oem_table_id = "ALPHABET",
.oem_revision = 0,
.asl_compiler_id = "RON ",
.asl_compiler_revision = 0,
},
};
struct acpi_table_fadt fadt = {
.header = {
.signature= "FADT",
// This is so stupid. Incredibly stupid.
.revision = 0,
.oem_id = "AKAROS",
.oem_table_id = "ALPHABET",
.oem_revision = 0,
.asl_compiler_id = "RON ",
.asl_compiler_revision = 0,
},
};
/* This has to be dropped into memory, then the other crap just follows it.
*/
struct acpi_table_madt madt = {
.header = {
.signature = "APIC",
.revision = 0,
.oem_id = "AKAROS",
.oem_table_id = "ALPHABET",
.oem_revision = 0,
.asl_compiler_id = "RON ",
.asl_compiler_revision = 0,
},
.address = 0xfee00000ULL,
};
struct acpi_table_hpet hpet = {
.header = {
.signature= "HPET",
// This is so stupid. Incredibly stupid.
.revision = 0,
.oem_id = "AKAROS",
.oem_table_id = "ALPHABET",
.oem_revision = 0,
.asl_compiler_id = "RON ",
.asl_compiler_revision = 0,
},
.id = 0x8086af01,
.address = {.address = 0xfed00000},
.sequence = 0,
.minimum_tick=0x37ee, // TODO: fill in from the real table. Painful!
.flags = ACPI_HPET_PAGE_PROTECT4,
};
struct acpi_madt_local_apic Apic0 = {.header = {.type = ACPI_MADT_TYPE_LOCAL_APIC, .length = sizeof(struct acpi_madt_local_apic)},
.processor_id = 0, .id = 0};
struct acpi_madt_io_apic Apic1 = {.header = {.type = ACPI_MADT_TYPE_IO_APIC, .length = sizeof(struct acpi_madt_io_apic)},
.id = 1, .address = 0xfec00000, .global_irq_base = 0};
struct acpi_madt_interrupt_override isor[] = {
/* I have no idea if it should be source irq 2, global 0, or global 2, source 0. Shit. */
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 2, .global_irq = 0, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 1, .global_irq = 1, .inti_flags = 0},
//{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
//.bus = 0, .source_irq = 2, .global_irq = 2, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 3, .global_irq = 3, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 4, .global_irq = 4, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 5, .global_irq = 5, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 6, .global_irq = 6, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 7, .global_irq = 7, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 8, .global_irq = 8, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 9, .global_irq = 9, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 10, .global_irq = 10, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 11, .global_irq = 11, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 12, .global_irq = 12, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 13, .global_irq = 13, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 14, .global_irq = 14, .inti_flags = 0},
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 15, .global_irq = 15, .inti_flags = 0},
// VMMCP routes irq 32 to gsi 17
{.header = {.type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE, .length = sizeof(struct acpi_madt_interrupt_override)},
.bus = 0, .source_irq = 32, .global_irq = 17, .inti_flags = 5},
};
/* this test will run the "kernel" in the negative address space. We hope. */
void *low1m;
uint8_t low4k[4096];
unsigned long long stack[1024];
volatile int shared = 0;
volatile int quit = 0;
int mcp = 1;
int virtioirq = 17;
/* total hack. If the vm runs away we want to get control again. */
unsigned int maxresume = (unsigned int) -1;
#define MiB 0x100000u
#define GiB (1u<<30)
#define GKERNBASE (16*MiB)
#define KERNSIZE (128*MiB+GKERNBASE)
uint8_t _kernel[KERNSIZE];
unsigned long long *p512, *p1, *p2m;
void **my_retvals;
int nr_threads = 4;
int debug = 0;
int resumeprompt = 0;
/* unlike Linux, this shared struct is for both host and guest. */
// struct virtqueue *constoguest =
// vring_new_virtqueue(0, 512, 8192, 0, inpages, NULL, NULL, "test");
uint64_t virtio_mmio_base = 0x100000000ULL;
void vapic_status_dump(FILE *f, void *vapic);
static void set_posted_interrupt(int vector);
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
#error "Get a gcc newer than 4.4.0"
#else
#define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
#endif
#define LOCK_PREFIX "lock "
#define ADDR BITOP_ADDR(addr)
static inline int test_and_set_bit(int nr, volatile unsigned long *addr);
static int timer_started;
static pthread_t timerthread_struct;
void *timer_thread(void *arg)
{
int fd = open("#cons/vmctl", O_RDWR), ret;
int initcount;
fprintf(stderr, "TIMER THREAD START 0x%llx\n", vmctl.timer_msr);
// We only begin injection when linux writes a non zero initial count
initcount = vmctl.initial_count;
while (!initcount) {
uthread_usleep(10000);
initcount = vmctl.initial_count;
}
fprintf(stderr, "INJECTING TIMER 0x%llx\n", vmctl.timer_msr);
fflush(stderr);
while (1) {
if (initcount) {
set_posted_interrupt(vmctl.timer_msr & 0xff);
//set_posted_interrupt(0xff);
pwrite(fd, &vmctl, sizeof(vmctl), 1<<12);
}
uthread_usleep(10000);
// check initcount again to make sure linux still wants it
initcount = vmctl.initial_count;
}
close(fd);
}
void *consout(void *arg)
{
char *line, *consline, *outline;
static struct scatterlist out[] = { {NULL, sizeof(outline)}, };
static struct scatterlist in[] = { {NULL, sizeof(line)}, };
static struct scatterlist iov[32];
struct virtio_threadarg *a = arg;
static unsigned int inlen, outlen, conslen;
struct virtqueue *v = a->arg->virtio;
fprintf(stderr, "talk thread ..\n");
uint16_t head, gaveit = 0, gotitback = 0;
uint32_t vv;
int i;
int num;
if (debug) {
fprintf(stderr, "----------------------- TT a %p\n", a);
fprintf(stderr, "talk thread ttargs %x v %x\n", a, v);
}
for(num = 0;;num++) {
//int debug = 1;
/* host: use any buffers we should have been sent. */
head = wait_for_vq_desc(v, iov, &outlen, &inlen);
if (debug)
fprintf(stderr, "CCC: vq desc head %d, gaveit %d gotitback %d\n", head, gaveit, gotitback);
for(i = 0; debug && i < outlen + inlen; i++)
fprintf(stderr, "CCC: v[%d/%d] v %p len %d\n", i, outlen + inlen, iov[i].v, iov[i].length);
/* host: if we got an output buffer, just output it. */
for(i = 0; i < outlen; i++) {
num++;
int j;
if (debug) {
fprintf(stderr, "CCC: IOV length is %d\n", iov[i].length);
}
for (j = 0; j < iov[i].length; j++)
printf("%c", ((char *)iov[i].v)[j]);
}
fflush(stdout);
if (debug)
fprintf(stderr, "CCC: outlen is %d; inlen is %d\n", outlen, inlen);
/* host: fill in the writeable buffers. */
/* why we're getting these I don't know. */
for (i = outlen; i < outlen + inlen; i++) {
if (debug) fprintf(stderr, "CCC: send back empty writeable");
iov[i].length = 0;
}
if (debug) fprintf(stderr, "CCC: call add_used\n");
/* host: now ack that we used them all. */
add_used(v, head, outlen+inlen);
if (debug) fprintf(stderr, "CCC: DONE call add_used\n");
}
fprintf(stderr, "All done\n");
return NULL;
}
// FIXME.
volatile int consdata = 0;
void *consin(void *arg)
{
struct virtio_threadarg *a = arg;
char *line, *outline;
static char consline[128];
static struct scatterlist iov[32];
static struct scatterlist out[] = { {NULL, sizeof(outline)}, };
static struct scatterlist in[] = { {NULL, sizeof(line)}, };
static unsigned int inlen, outlen, conslen;
struct virtqueue *v = a->arg->virtio;
fprintf(stderr, "consin thread ..\n");
uint16_t head, gaveit = 0, gotitback = 0;
uint32_t vv;
int i;
int num;
//char c[1];
int fd = open("#cons/vmctl", O_RDWR), ret;
if (debug) fprintf(stderr, "Spin on console being read, print num queues, halt\n");
for(num = 0;! quit;num++) {
//int debug = 1;
/* host: use any buffers we should have been sent. */
head = wait_for_vq_desc(v, iov, &outlen, &inlen);
if (debug)
fprintf(stderr, "vq desc head %d, gaveit %d gotitback %d\n", head, gaveit, gotitback);
for(i = 0; debug && i < outlen + inlen; i++)
fprintf(stderr, "v[%d/%d] v %p len %d\n", i, outlen + inlen, iov[i].v, iov[i].length);
if (debug)
fprintf(stderr, "outlen is %d; inlen is %d\n", outlen, inlen);
/* host: fill in the writeable buffers. */
for (i = outlen; i < outlen + inlen; i++) {
/* host: read a line. */
memset(consline, 0, 128);
if (read(0, consline, 1) < 0) {
exit(0);
}
if (debug) fprintf(stderr, "CONSIN: GOT A LINE:%s:\n", consline);
if (debug) fprintf(stderr, "CONSIN: OUTLEN:%d:\n", outlen);
if (strlen(consline) < 3 && consline[0] == 'q' ) {
quit = 1;
break;
}
memmove(iov[i].v, consline, strlen(consline)+ 1);
iov[i].length = strlen(consline) + 1;
}
if (debug) fprintf(stderr, "call add_used\n");
/* host: now ack that we used them all. */
add_used(v, head, outlen+inlen);
// consdata = 1;
if (debug) fprintf(stderr, "DONE call add_used\n");
// Send spurious for testing (Gan)
set_posted_interrupt(0xE5);
virtio_mmio_set_vring_irq();
pwrite(fd, &vmctl, sizeof(vmctl), 1<<12);
}
fprintf(stderr, "All done\n");
return NULL;
}
static struct vqdev vqdev= {
name: "console",
dev: VIRTIO_ID_CONSOLE,
device_features: 0, /* Can't do it: linux console device does not support it. VIRTIO_F_VERSION_1*/
numvqs: 2,
vqs: {
{name: "consin", maxqnum: 64, f: consin, arg: (void *)0},
{name: "consout", maxqnum: 64, f: consout, arg: (void *)0},
}
};
void lowmem() {
__asm__ __volatile__ (".section .lowmem, \"aw\"\n\tlow: \n\t.=0x1000\n\t.align 0x100000\n\t.previous\n");
}
static uint8_t acpi_tb_checksum(uint8_t *buffer, uint32_t length)
{
uint8_t sum = 0;
uint8_t *end = buffer + length;
fprintf(stderr, "tbchecksum %p for %d", buffer, length);
while (buffer < end) {
if (end - buffer < 2)
fprintf(stderr, "%02x\n", sum);
sum = (uint8_t)(sum + *(buffer++));
}
fprintf(stderr, " is %02x\n", sum);
return (sum);
}
static void gencsum(uint8_t *target, void *data, int len)
{
uint8_t csum;
// blast target to zero so it does not get counted (it might be in the struct we checksum)
// And, yes, it is, goodness.
fprintf(stderr, "gencsum %p target %p source %d bytes\n", target, data, len);
*target = 0;
csum = acpi_tb_checksum((uint8_t *)data, len);
*target = ~csum + 1;
fprintf(stderr, "Cmoputed is %02x\n", *target);
}
static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
{
int oldbit;
asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
"sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
return oldbit;
}
static void pir_dump()
{
unsigned long *pir_ptr = (unsigned long *)vmctl.pir;
int i;
fprintf(stderr, "-------Begin PIR dump-------\n");
for (i = 0; i < 8; i++){
fprintf(stderr, "Byte %d: 0x%016lx\n", i, pir_ptr[i]);
}
fprintf(stderr, "-------End PIR dump-------\n");
}
static void set_posted_interrupt(int vector)
{
unsigned long *bit_vec;
int bit_offset;
int i, j;
unsigned long *pir = (unsigned long *)vmctl.pir;
// Move to the correct location to set our bit.
bit_vec = pir + vector/(sizeof(unsigned long)*8);
bit_offset = vector%(sizeof(unsigned long)*8);
if(debug) fprintf(stderr, "%s: Pre set PIR dump\n", __func__);
if(debug) pir_dump();
if(debug) vapic_status_dump(stderr, (void *)vmctl.vapic);
if(debug) fprintf(stderr, "%s: Setting pir bit offset %d at 0x%p\n", __func__,
bit_offset, bit_vec);
if(test_and_set_bit(bit_offset, bit_vec)){
// fprintf(stderr, "FAIL\n");
} else {
// fprintf(stderr, "PASS\n");
}
// Set outstanding notification bit
bit_vec = pir + 4;
if(debug) fprintf(stderr, "%s: Setting pir bit offset 0 at 0x%p", __func__,
bit_vec);
test_and_set_bit(0, bit_vec);
if(debug) pir_dump();
}
int main(int argc, char **argv)
{
struct boot_params *bp;
char *cmdline;
uint64_t *p64;
void *a = (void *)0xe0000;
struct acpi_table_rsdp *r;
struct acpi_table_fadt *f;
struct acpi_table_madt *m;
struct acpi_table_xsdt *x;
struct acpi_table_hpet *h;
uint64_t virtiobase = 0x100000000ULL;
// lowmem is a bump allocated pointer to 2M at the "physbase" of memory
void *lowmem = (void *) 0x1000000;
//struct vmctl vmctl;
int amt;
int vmmflags = 0; // Disabled probably forever. VMM_VMCALL_PRINTF;
uint64_t entry = 0x1200000, kerneladdress = 0x1200000;
int nr_gpcs = 1;
int fd = open("#cons/vmctl", O_RDWR), ret;
void * xp;
int kfd = -1;
static char cmd[512];
int i;
uint8_t csum;
void *coreboot_tables = (void *) 0x1165000;
void *a_page;
uint64_t tsc_freq_khz;
fprintf(stderr, "%p %p %p %p\n", PGSIZE, PGSHIFT, PML1_SHIFT, PML1_PTE_REACH);
// mmap is not working for us at present.
if ((uint64_t)_kernel > GKERNBASE) {
fprintf(stderr, "kernel array @%p is above , GKERNBASE@%p sucks\n", _kernel, GKERNBASE);
exit(1);
}
memset(_kernel, 0, sizeof(_kernel));
memset(lowmem, 0xff, 2*1048576);
memset(low4k, 0xff, 4096);
// avoid at all costs, requires too much instruction emulation.
//low4k[0x40e] = 0;
//low4k[0x40f] = 0xe0;
//Place mmap(Gan)
a_page = mmap((void *)0xfee00000, PGSIZE, PROT_READ | PROT_WRITE,
MAP_POPULATE | MAP_ANONYMOUS, -1, 0);
fprintf(stderr, "a_page mmap pointer %p", a_page);
if (a_page == (void *) -1) {
perror("Could not mmap APIC");
exit(1);
}
if (((uint64_t)a_page & 0xfff) != 0) {
perror("APIC page mapping is not page aligned");
exit(1);
}
memset(a_page, 0, 4096);
//((uint32_t *)a_page)[0x30/4] = 0x01060015;
((uint32_t *)a_page)[0x30/4] = 0xDEADBEEF;
if (fd < 0) {
perror("#cons/sysctl");
exit(1);
}
argc--,argv++;
// switches ...
// Sorry, I don't much like the gnu opt parsing code.
while (1) {
if (*argv[0] != '-')
break;
switch(argv[0][1]) {
case 'd':
debug++;
break;
case 'v':
vmmflags |= VMM_VMCALL_PRINTF;
break;
case 'm':
argc--,argv++;
maxresume = strtoull(argv[0], 0, 0);
break;
case 'i':
argc--,argv++;
virtioirq = strtoull(argv[0], 0, 0);
break;
default:
fprintf(stderr, "BMAFR\n");
break;
}
argc--,argv++;
}
if (argc < 1) {
fprintf(stderr, "Usage: %s vmimage [-n (no vmcall printf)] [coreboot_tables [loadaddress [entrypoint]]]\n", argv[0]);
exit(1);
}
if (argc > 1)
coreboot_tables = (void *) strtoull(argv[1], 0, 0);
if (argc > 2)
kerneladdress = strtoull(argv[2], 0, 0);
if (argc > 3)
entry = strtoull(argv[3], 0, 0);
kfd = open(argv[0], O_RDONLY);
if (kfd < 0) {
perror(argv[0]);
exit(1);
}
// read in the kernel.
xp = (void *)kerneladdress;
for(;;) {
amt = read(kfd, xp, 1048576);
if (amt < 0) {
perror("read");
exit(1);
}
if (amt == 0) {
break;
}
xp += amt;
}
fprintf(stderr, "Read in %d bytes\n", xp-kerneladdress);
close(kfd);
// The low 1m so we can fill in bullshit like ACPI. */
// And, sorry, due to the STUPID format of the RSDP for now we need the low 1M.
low1m = mmap((int*)4096, MiB-4096, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS, -1, 0);
if (low1m != (void *)4096) {
perror("Unable to mmap low 1m");
exit(1);
}
memset(low1m, 0xff, MiB-4096);
r = a;
fprintf(stderr, "install rsdp to %p\n", r);
*r = rsdp;
a += sizeof(*r);
memmove(&r->xsdt_physical_address, &a, sizeof(a));
gencsum(&r->checksum, r, ACPI_RSDP_CHECKSUM_LENGTH);
if ((csum = acpi_tb_checksum((uint8_t *) r, ACPI_RSDP_CHECKSUM_LENGTH)) != 0) {
fprintf(stderr, "RSDP has bad checksum; summed to %x\n", csum);
exit(1);
}
/* Check extended checksum if table version >= 2 */
gencsum(&r->extended_checksum, r, ACPI_RSDP_XCHECKSUM_LENGTH);
if ((rsdp.revision >= 2) &&
(acpi_tb_checksum((uint8_t *) r, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)) {
fprintf(stderr, "RSDP has bad checksum v2\n");
exit(1);
}
/* just leave a bunch of space for the xsdt. */
/* we need to zero the area since it has pointers. */
x = a;
a += sizeof(*x) + 8*sizeof(void *);
memset(x, 0, a - (void *)x);
fprintf(stderr, "install xsdt to %p\n", x);
*x = xsdt;
x->table_offset_entry[0] = 0;
x->table_offset_entry[1] = 0;
x->header.length = a - (void *)x;
f = a;
fprintf(stderr, "install fadt to %p\n", f);
*f = fadt;
x->table_offset_entry[2] = (uint64_t) f;
a += sizeof(*f);
f->header.length = a - (void *)f;
gencsum(&f->header.checksum, f, f->header.length);
if (acpi_tb_checksum((uint8_t *)f, f->header.length) != 0) {
fprintf(stderr, "ffadt has bad checksum v2\n");
exit(1);
}
m = a;
*m = madt;
x->table_offset_entry[3] = (uint64_t) m;
a += sizeof(*m);
fprintf(stderr, "install madt to %p\n", m);
memmove(a, &Apic0, sizeof(Apic0));
a += sizeof(Apic0);
memmove(a, &Apic1, sizeof(Apic1));
a += sizeof(Apic1);
memmove(a, &isor, sizeof(isor));
a += sizeof(isor);
m->header.length = a - (void *)m;
gencsum(&m->header.checksum, m, m->header.length);
if (acpi_tb_checksum((uint8_t *) m, m->header.length) != 0) {
fprintf(stderr, "madt has bad checksum v2\n");
exit(1);
}
h = a;
fprintf(stderr, "install hpet to %p\n", h);
*h = hpet;
x->table_offset_entry[4] = (uint64_t) h;
a += sizeof(*h);
h->header.length = a - (void *)h;
gencsum(&h->header.checksum, h, h->header.length);
if (acpi_tb_checksum((uint8_t *)h, h->header.length) != 0) {
fprintf(stderr, "hpet has bad checksum v2\n");
exit(1);
}
fprintf(stderr, "allchecksums ok\n");
gencsum(&x->header.checksum, x, x->header.length);
if ((csum = acpi_tb_checksum((uint8_t *) x, x->header.length)) != 0) {
fprintf(stderr, "XSDT has bad checksum; summed to %x\n", csum);
exit(1);
}
hexdump(stdout, r, a-(void *)r);
a = (void *)(((unsigned long)a + 0xfff) & ~0xfff);
vmctl.pir = (uint64_t) a;
memset(a, 0, 4096);
a += 4096;
vmctl.vapic = (uint64_t) a;
//vmctl.vapic = (uint64_t) a_page;
memset(a, 0, 4096);
((uint32_t *)a)[0x30/4] = 0x01060014;
p64 = a;
// set up apic values? do we need to?
// qemu does this.
//((uint8_t *)a)[4] = 1;
/* Allocate memory for, and zero the bootparams
* page before writing to it, or Linux thinks
* we're talking crazy.
*/
a += 4096;
bp = a;
memset(bp, 0, 4096);
/* Set the kernel command line parameters */
a += 4096;
cmdline = a;
a += 4096;
bp->hdr.cmd_line_ptr = (uintptr_t) cmdline;
tsc_freq_khz = get_tsc_freq()/1000;
sprintf(cmdline, "earlyprintk=vmcall,keep"
" console=hvc0"
" virtio_mmio.device=1M@0x100000000:32"
" nosmp"
" maxcpus=1"
" acpi.debug_layer=0x2"
" acpi.debug_level=0xffffffff"
" apic=debug"
" noexec=off"
" nohlt"
" init=/bin/sh"
" lapic=notscdeadline"
" lapictimerfreq=1000000"
" pit=none"
" tscfreq=%lld", tsc_freq_khz);
/* Put the e820 memory region information in the boot_params */
bp->e820_entries = 3;
int e820i = 0;
bp->e820_map[e820i].addr = 0;
bp->e820_map[e820i].size = 16*1048576;
bp->e820_map[e820i++].type = E820_RESERVED;
bp->e820_map[e820i].addr = 16*1048576;
bp->e820_map[e820i].size = 128*1048576;
bp->e820_map[e820i++].type = E820_RAM;
//bp->e820_map[2].addr = 4096*1048576ULL;
//bp->e820_map[2].size = 2*1048576;
//bp->e820_map[2].type = E820_RAM;
bp->e820_map[e820i].addr = 0xf0000000;
bp->e820_map[e820i].size = 0x10000000;
bp->e820_map[e820i++].type = E820_RESERVED;
if (ros_syscall(SYS_setup_vmm, nr_gpcs, vmmflags, 0, 0, 0, 0) != nr_gpcs) {
perror("Guest pcore setup failed");
exit(1);
}
fprintf(stderr, "Run with %d cores and vmmflags 0x%x\n", nr_gpcs, vmmflags);
mcp = 1;
if (mcp) {
my_retvals = malloc(sizeof(void*) * nr_threads);
if (!my_retvals)
perror("Init threads/malloc");
pthread_can_vcore_request(FALSE); /* 2LS won't manage vcores */
pthread_need_tls(FALSE);
pthread_mcp_init(); /* gives us one vcore */
vcore_request(nr_threads - 1); /* ghetto incremental interface */
for (int i = 0; i < nr_threads; i++) {
xp = __procinfo.vcoremap;
fprintf(stderr, "%p\n", __procinfo.vcoremap);
fprintf(stderr, "Vcore %d mapped to pcore %d\n", i,
__procinfo.vcoremap[i].pcoreid);
}
}
ret = syscall(33, 1);
if (ret < 0) {
perror("vm setup");
exit(1);
}
ret = posix_memalign((void **)&p512, 4096, 3*4096);
fprintf(stderr, "memalign is %p\n", p512);
if (ret) {
perror("ptp alloc");
exit(1);
}
p1 = &p512[512];
p2m = &p512[1024];
uint64_t kernbase = 0; //0xffffffff80000000;
uint64_t highkernbase = 0xffffffff80000000;
p512[PML4(kernbase)] = (unsigned long long)p1 | 7;
p1[PML3(kernbase)] = /*0x87; */(unsigned long long)p2m | 7;
p512[PML4(highkernbase)] = (unsigned long long)p1 | 7;
p1[PML3(highkernbase)] = /*0x87; */(unsigned long long)p2m | 7;
#define _2MiB (0x200000)
for (i = 0; i < 512; i++) {
p2m[PML2(kernbase + i * _2MiB)] = 0x87 | i * _2MiB;
}
kernbase >>= (0+12);
kernbase <<= (0 + 12);
uint8_t *kernel = (void *)GKERNBASE;
//write_coreboot_table(coreboot_tables, ((void *)VIRTIOBASE) /*kernel*/, KERNSIZE + 1048576);
hexdump(stdout, coreboot_tables, 512);
fprintf(stderr, "kernbase for pml4 is 0x%llx and entry is %llx\n", kernbase, entry);
fprintf(stderr, "p512 %p p512[0] is 0x%lx p1 %p p1[0] is 0x%x\n", p512, p512[0], p1, p1[0]);
vmctl.interrupt = 0;
vmctl.command = REG_RSP_RIP_CR3;
vmctl.cr3 = (uint64_t) p512;
vmctl.regs.tf_rip = entry;
vmctl.regs.tf_rsp = (uint64_t) &stack[1024];
vmctl.regs.tf_rsi = (uint64_t) bp;
if (mcp) {
/* set up virtio bits, which depend on threads being enabled. */
register_virtio_mmio(&vqdev, virtio_mmio_base);
}
fprintf(stderr, "threads started\n");
fprintf(stderr, "Writing command :%s:\n", cmd);
// Start up timer thread
if (1 && !timer_started && mcp) {
// Start up timer thread
if (pthread_create(&timerthread_struct, NULL, timer_thread, NULL)) {
fprintf(stderr, "pth_create failed for timer thread.\n");
perror("pth_create");
} else {
fprintf(stderr, "Starting Timer\n");
timer_started = 1;
}
}
if(debug) vapic_status_dump(stderr, (void *)vmctl.vapic);
ret = pwrite(fd, &vmctl, sizeof(vmctl), 0);
if(debug) vapic_status_dump(stderr, (void *)vmctl.vapic);
if (ret != sizeof(vmctl)) {
perror(cmd);
}
while (1) {
void showstatus(FILE *f, struct vmctl *v);
int c;
uint8_t byte;
vmctl.command = REG_RIP;
if (maxresume-- == 0) {
debug = 1;
resumeprompt = 1;
}
if (debug) {
fprintf(stderr, "RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
showstatus(stderr, &vmctl);
}
if (resumeprompt) {
fprintf(stderr, "RESUME?\n");
c = getchar();
if (c == 'q')
break;
}
if (vmctl.shutdown == SHUTDOWN_EPT_VIOLATION) {
uint64_t gpa, *regp, val;
uint8_t regx;
int store, size;
int advance;
if (decode(&vmctl, &gpa, ®x, ®p, &store, &size, &advance)) {
fprintf(stderr, "RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
showstatus(stderr, &vmctl);
quit = 1;
break;
}
if (debug) fprintf(stderr, "%p %p %p %p %p %p\n", gpa, regx, regp, store, size, advance);
if ((gpa & ~0xfffULL) == virtiobase) {
if (debug) fprintf(stderr, "DO SOME VIRTIO\n");
// Lucky for us the various virtio ops are well-defined.
virtio_mmio(&vmctl, gpa, regx, regp, store);
if (debug) fprintf(stderr, "store is %d:\n", store);
if (debug) fprintf(stderr, "REGP IS %16x:\n", *regp);
} else if ((gpa & 0xfee00000) == 0xfee00000) {
// until we fix our include mess, just put the proto here.
//int apic(struct vmctl *v, uint64_t gpa, int destreg, uint64_t *regp, int store);
//apic(&vmctl, gpa, regx, regp, store);
} else if ((gpa & 0xfec00000) == 0xfec00000) {
// until we fix our include mess, just put the proto here.
int do_ioapic(struct vmctl *v, uint64_t gpa, int destreg, uint64_t *regp, int store);
do_ioapic(&vmctl, gpa, regx, regp, store);
} else if (gpa < 4096) {
uint64_t val = 0;
memmove(&val, &low4k[gpa], size);
hexdump(stdout, &low4k[gpa], size);
fprintf(stderr, "Low 1m, code %p read @ %p, size %d, val %p\n", vmctl.regs.tf_rip, gpa, size, val);
memmove(regp, &low4k[gpa], size);
hexdump(stdout, regp, size);
} else {
fprintf(stderr, "EPT violation: can't handle %p\n", gpa);
fprintf(stderr, "RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
fprintf(stderr, "Returning 0xffffffff\n");
showstatus(stderr, &vmctl);
// Just fill the whole register for now.
*regp = (uint64_t) -1;
}
vmctl.regs.tf_rip += advance;
if (debug) fprintf(stderr, "Advance rip by %d bytes to %p\n", advance, vmctl.regs.tf_rip);
vmctl.shutdown = 0;
vmctl.gpa = 0;
vmctl.command = REG_ALL;
} else if (vmctl.shutdown == SHUTDOWN_UNHANDLED_EXIT_REASON) {
switch(vmctl.ret_code){
case EXIT_REASON_VMCALL:
byte = vmctl.regs.tf_rdi;
printf("%c", byte);
if (byte == '\n') printf("%c", '%');
vmctl.regs.tf_rip += 3;
break;
case EXIT_REASON_EXTERNAL_INTERRUPT:
if (debug) pir_dump();
vmctl.command = RESUME;
break;
case EXIT_REASON_IO_INSTRUCTION:
fprintf(stderr, "IO @ %p\n", vmctl.regs.tf_rip);
io(&vmctl);
vmctl.shutdown = 0;
vmctl.gpa = 0;
vmctl.command = REG_ALL;
break;
case EXIT_REASON_INTERRUPT_WINDOW:
if (consdata) {
if (debug) fprintf(stderr, "inject an interrupt\n");
virtio_mmio_set_vring_irq();
vmctl.interrupt = 0x80000000 | virtioirq;
vmctl.command = RESUME;
consdata = 0;
}
break;
case EXIT_REASON_MSR_WRITE:
case EXIT_REASON_MSR_READ:
fprintf(stderr, "Do an msr\n");
if ( msrio(&vmctl, vmctl.ret_code) ) { // uh-oh, msrio failed
// well, hand back a GP fault which is what Intel does
fprintf(stderr, "MSR FAILED: RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
showstatus(stderr, &vmctl);
// Use event injection through vmctl to send
// a general protection fault
// vmctl.interrupt gets written to the VM-Entry
// Interruption-Information Field by vmx
vmctl.command = RESUME;
vmctl.interrupt = (1 << 31) // "Valid" bit
| (0 << 12) // Reserved by Intel
| (1 << 11) // Deliver-error-code bit (set if event pushes error code to stack)
| (3 << 8) // Event type (3 is "hardware exception")
| 13; // Interrupt/exception vector (13 is "general protection fault")
} else {
vmctl.regs.tf_rip += 2;
vmctl.command = REG_ALL;
}
break;
case EXIT_REASON_MWAIT_INSTRUCTION:
fflush(stdout);
if (debug)fprintf(stderr, "\n================== Guest MWAIT. =======================\n");
if (debug)fprintf(stderr, "Wait for cons data\n");
while (!consdata)
;
//debug = 1;
if(debug) vapic_status_dump(stderr, (void *)vmctl.vapic);
if (debug)fprintf(stderr, "Resume with consdata ...\n");
vmctl.regs.tf_rip += 3;
ret = pwrite(fd, &vmctl, sizeof(vmctl), 0);
if (ret != sizeof(vmctl)) {
perror(cmd);
}
//fprintf(stderr, "RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
//showstatus(stderr, &vmctl);
break;
case EXIT_REASON_HLT:
fflush(stdout);
if (debug)fprintf(stderr, "\n================== Guest halted. =======================\n");
if (debug)fprintf(stderr, "Wait for cons data\n");
while (!consdata)
;
//debug = 1;
if (debug)fprintf(stderr, "Resume with consdata ...\n");
vmctl.regs.tf_rip += 1;
ret = pwrite(fd, &vmctl, sizeof(vmctl), 0);
if (ret != sizeof(vmctl)) {
perror(cmd);
}
//fprintf(stderr, "RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
//showstatus(stderr, &vmctl);
break;
case EXIT_REASON_APIC_ACCESS:
if (1 || debug)fprintf(stderr, "APIC READ EXIT\n");
uint64_t gpa, *regp, val;
uint8_t regx;
int store, size;
int advance;
if (decode(&vmctl, &gpa, ®x, ®p, &store, &size, &advance)) {
fprintf(stderr, "RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
showstatus(stderr, &vmctl);
quit = 1;
break;
}
int apic(struct vmctl *v, uint64_t gpa, int destreg, uint64_t *regp, int store);
apic(&vmctl, gpa, regx, regp, store);
vmctl.regs.tf_rip += advance;
if (debug) fprintf(stderr, "Advance rip by %d bytes to %p\n", advance, vmctl.regs.tf_rip);
vmctl.shutdown = 0;
vmctl.gpa = 0;
vmctl.command = REG_ALL;
break;
case EXIT_REASON_APIC_WRITE:
if (1 || debug)fprintf(stderr, "APIC WRITE EXIT\n");
break;
default:
fprintf(stderr, "Don't know how to handle exit %d (%x)\n", vmctl.ret_code, vmctl.ret_code);
fprintf(stderr, "RIP %p, shutdown 0x%x\n", vmctl.regs.tf_rip, vmctl.shutdown);
showstatus(stderr, &vmctl);
quit = 1;
break;
}
}
if (debug) fprintf(stderr, "at bottom of switch, quit is %d\n", quit);
if (quit)
break;
if (consdata) {
if (debug) fprintf(stderr, "inject an interrupt\n");