-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvm_flat.c
2060 lines (1757 loc) · 63.1 KB
/
kvm_flat.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
/* Sample code for /dev/kvm API
*
* Copyright (c) 2015 Intel Corporation
* Author: Josh Triplett <[email protected]>
*
* Converted into a DPMI test harness by: Hamish Coleman <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <err.h>
#include <fcntl.h>
#include <linux/kvm.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#ifndef KVM_INTERNAL_ERROR_DELIVERY_EV
#define KVM_INTERNAL_ERROR_DELIVERY_EV 3
#endif
int debug_level = 2;
int debug_printf(unsigned char level, const char *fmt, ...)
{
va_list args;
char buf[1025];
int i;
if (level > debug_level)
return 0;
va_start(args, fmt);
i=vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
fprintf(stderr,buf);
return i;
}
#define SEG_TEXT 0x08 /* gdt[1] */
#define SEG_DATA 0x10 /* gdt[2] */
#define SEG_PSP 0x18
#define SEG_ENV 0x20
#define SEG_GO32 0x28
#define SEG_GO32_TEXT 0x30
#define SEG_SYS_MAX SEG_GO32_TEXT
#define SEG_PSP_SIZE 256
#define SEG_PSP_BASE 0xf0030000
#define SEG_ENV_SIZE 256
#define SEG_ENV_BASE (SEG_PSP_BASE+SEG_PSP_SIZE)
#define SEG_GO32_SIZE (0x5000-SEG_PSP_SIZE-SEG_ENV_SIZE)
#define SEG_GO32_BASE (SEG_ENV_BASE+SEG_ENV_SIZE)
#define MEM_REGION_GDT 0
#define MEM_REGION_IDT 1
#define MEM_REGION_TEXT 2
#define MEM_REGION_PSP 3
#define MEM_REGION_ZERO 4
#define MEM_REGION_SYS_MAX 4
#define MEM_REGION_MAX 16
#define REGION_TB_BASE 0x10
#define REGION_TB_SIZE 0x800
#define REGION_DOSMEM_BASE 0x810
#define REGION_DOSMEM_SIZE 0x1000
#define REGION_STACK_SIZE 0x1000
#define REGION_STACK_BASE 0xf0000000
#define REGION_IDT_SIZE 0x1000
#define REGION_IDT_BASE 0xf0010000
#define REGION_GDT_SIZE 0x1000
#define REGION_GDT_BASE 0xf0020000
#define REGION_PSP_SIZE (SEG_PSP_SIZE+SEG_ENV_SIZE+SEG_GO32_SIZE)
#define REGION_PSP_BASE 0xf0030000
#define REGION_BSS_BASE 0x00200000
struct __attribute__ ((__packed__)) smi_buffer00 {
__u32 unk1;
__u32 unk2;
__u64 unk3;
__u64 size;
__u8 uuid[16];
__u8 data[];
};
struct __attribute__ ((__packed__)) smi_buffer05 {
__u32 unk1; /* looks like packet type */
__u32 unk2;
__u32 unk3;
__u32 unk4;
__u64 unk5; /* _FLASH_MAP base ? */
__u8 pad1[0x18];
__u64 unk6;
__u8 pad2[0x10];
__u64 unk7;
__u64 unk8;
__u64 unk9;
__u8 uuid[16];
};
struct __attribute__ ((__packed__)) gdt_entry {
__u16 limit_l;
__u16 base_l;
__u8 base_m;
__u8 type_flags;
__u8 limit_flags;
__u8 base_h;
};
struct __attribute__ ((__packed__)) idt_entry {
__u16 offset_l;
__u16 selector;
__u8 always0;
__u8 type_flags;
__u16 offset_h;
};
struct __attribute__ ((__packed__)) idt {
struct idt_entry entry[256];
uint8_t hlt[256][4];
};
struct __attribute__ ((__packed__)) djgcc_stubinfo {
char magic[16];
__u32 size; /* bytes in structure */
__u32 minstack; /* minimum amount of DPMI stack space */
__u32 memory_handle; /* DPMI memory handle */
__u32 initial_size; /* size of initial segment */
__u16 minkeep; /* size of transfer buffer */
__u16 ds_selector; /* selector used for transfer buffer */
__u16 ds_segment; /* segment address of transfer buffer */
__u16 psp_selector; /* PSP selector (PSP is at offset 0) */
__u16 cs_selector; /* to be freed */
__u16 env_size; /* number of bytes of environment */
char basename[8]; /* base name of executable to load (asciiz if < 8) */
char argv0[16]; /* used ONLY by the application (asciiz if < 16) */
char dpmi_server[16]; /* used by stub to load DPMI server if no DPMI already present */
};
struct __attribute__ ((__packed__)) region_psp {
char psp[128];
char cmdline_len;
char cmdline[127];
char env[256];
struct djgcc_stubinfo stubinfo; /* offset 0 in go32 */
char padding[172];
char buffer[16384]; /* offset 0x100 in go32 */
char padding2[3328];
};
struct irq_handler_entry; /* forward definition */
struct emu {
unsigned long entry; /* entry point for the binary - from config */
int kvm;
int vmfd;
int vcpufd;
struct kvm_run *run;
struct kvm_userspace_memory_region mem[MEM_REGION_MAX];
unsigned int region_stack; /* which slot contains the stack */
unsigned int region_brk; /* start of available region slots */
unsigned int bss_brk; /* start of available bss */
unsigned int bss_size; /* how much space to allocate for the BSS */
unsigned int gdt_brk; /* start of available descriptors */
struct irq_handler_entry *irq;
__u32 mmio_next; /* if mmio matches this, avoid verbosity */
int mmio_count; /* just how many mmio_next matches have we seen */
int trace; /* enable instruction tracing */
__u32 debug_addr;
int debug_count;
int smi_count; /* number of SMI io ops we have seen */
__u32 smi_Buffer_Ptr_Address; /* Could set this by reading ACPI tables */
int dos_alloc; /* have we given out the low memory region? */
char *cmdline;
} emu_global;
struct irq_subhandler_entry {
int subcode;
char *name;
int (*handler)(void *, struct emu *, struct kvm_regs *);
void *data;
};
struct irq_handler_entry {
char *name;
int (*handler)(void *, struct emu *, struct kvm_regs *);
void *data;
};
#define WANT_NONE 0
#define WANT_SET_REGS 1
#define WANT_NEWLINE 2
int handle_irqno(struct irq_handler_entry *, unsigned char, struct emu *, struct kvm_regs *); /* forward definition */
void *mem_guest2host(struct emu *emu, __u64 guestaddr) {
for (int i=0; i<MEM_REGION_MAX; i++) {
if (guestaddr >= emu->mem[i].guest_phys_addr && guestaddr <= emu->mem[i].guest_phys_addr + emu->mem[i].memory_size) {
return (uint8_t *)emu->mem[i].userspace_addr + (guestaddr - emu->mem[i].guest_phys_addr);
}
}
return NULL;
}
/*
* Do the same as mem_guest2host, but take some extra steps
* if it looks like we are running a strange stack
*/
void *mem_getstack(struct emu *emu, __u64 rsp) {
if (rsp>0 && rsp<0x1000) {
/* Stack is in the zero-page area, possibly we are using the
* GO32 segment for the stack
*/
struct kvm_sregs sregs;
int ret = ioctl(emu->vcpufd, KVM_GET_SREGS, &sregs);
if (ret == -1)
err(1, "KVM_GET_SREGS");
if (sregs.ss.selector == SEG_GO32) {
return mem_guest2host(emu, SEG_GO32_BASE + rsp);
}
}
return mem_guest2host(emu, rsp);
}
__u32 get_retaddr(struct emu *emu, struct kvm_regs *regs) {
__u32 *stack = mem_getstack(emu,regs->rsp);
if (stack) {
return *stack;
}
return 0;
}
void gdt_setlimit(struct gdt_entry *gdt, __u32 limit) {
if (limit > 0xfffff) {
gdt->limit_flags |= 0x80; /* set G bit */
limit = limit>>12;
} else {
gdt->limit_flags &= ~0x80; /* clear G bit */
}
gdt->limit_l = limit & 0xffff;
gdt->limit_flags &= 0xf0;
gdt->limit_flags |= (limit & 0xf0000) >> 16;
}
void gdt_setbase(struct gdt_entry *gdt, __u32 base) {
gdt->base_l = base & 0xffff;
gdt->base_m = (base & 0xff0000) >> 16;
gdt->base_h = (base & 0xff000000) >> 24;
}
__u32 gdt_getbase(struct gdt_entry *gdt) {
return gdt->base_l | (gdt->base_m <<16) | (gdt->base_h <<24);
}
void dump_backtrace(struct emu *emu, struct kvm_regs *called_regs) {
struct kvm_regs regs;
regs.rsp = called_regs->rsp;
regs.rbp = called_regs->rbp;
debug_printf(2,"Backtrace:");
__u32 *stack = mem_getstack(emu,regs.rsp);
if (!stack) {
return;
}
debug_printf(2," (0x%08llx 0x%08x)", called_regs->rip, *stack);
int maxdepth = 18;
int depth = 0;
while (depth<maxdepth) {
if (depth%7==0) {
debug_printf(2,"\n ");
}
regs.rsp = regs.rbp;
stack = mem_getstack(emu,regs.rsp);
if (!stack) {
debug_printf(2,"!stack");
break;
}
regs.rbp = stack[0];
debug_printf(2,"0x%08x ",stack[1]);
if (stack[1] == 0) {
break;
}
depth++;
}
debug_printf(2,"\n");
}
void dump_kvm_run(struct kvm_run *run) {
const char * kvm_exit_str[] = {
"KVM_EXIT_UNKNOWN", "KVM_EXIT_EXCEPTION", "KVM_EXIT_IO",
"KVM_EXIT_HYPERCALL", "KVM_EXIT_DEBUG", "KVM_EXIT_HLT",
"KVM_EXIT_MMIO", "KVM_EXIT_IRQ_WINDOW_OPEN", "KVM_EXIT_SHUTDOWN",
"KVM_EXIT_FAIL_ENTRY", "KVM_EXIT_INTR", "KVM_EXIT_SET_TPR",
"KVM_EXIT_TPR_ACCESS", "KVM_EXIT_S390_SIEIC", "KVM_EXIT_S390_RESET",
"KVM_EXIT_DCR", "KVM_EXIT_NMI", "KVM_EXIT_INTERNAL_ERROR",
"KVM_EXIT_OSI", "KVM_EXIT_PAPR_HCALL",
};
debug_printf(1,"%s(%i)\n",kvm_exit_str[run->exit_reason],run->exit_reason);
switch (run->exit_reason) {
case KVM_EXIT_INTERNAL_ERROR:
debug_printf(1,"\tsuberror: 0x%x\n",run->internal.suberror);
debug_printf(1,"\textra data (%i):\n\t",run->internal.ndata);
for (int i=0; i<run->internal.ndata; i++) {
debug_printf(1,"0x%x ",run->internal.data[i]);
}
debug_printf(1,"\n");
break;
case KVM_EXIT_MMIO:
debug_printf(1,"\tphys_addr: 0x%llx\n",run->mmio.phys_addr);
break;
case KVM_EXIT_IO:
debug_printf(1,"\tport[0x%llx]",run->io.port);
if (run->io.direction == KVM_EXIT_IO_OUT) {
__u64 val = 0;
/* FIXME - what if size is > 8 ? */
memcpy(&val,((char *)run)+run->io.data_offset,run->io.size);
debug_printf(1,"=0x%llx(%i)\n",val,run->io.size);
} else {
debug_printf(1,"\n");
}
break;
}
}
void dump_hex(__u8 *data, int size) {
if (!data) {
return;
}
int addr_line = 0;
int addr;
while(addr_line<size) {
addr = 0;
debug_printf(1," %04x ",addr_line);
while (addr<16 && addr_line+addr<size) {
debug_printf(1,"%02x ",data[addr_line+addr++]);
if (addr==8) {
debug_printf(1," ");
}
}
while (addr<16) {
debug_printf(1," ");
addr++;
}
addr = 0;
debug_printf(1," |");
while (addr<16 && addr_line+addr<size) {
debug_printf(1,"%c",
isprint(data[addr_line+addr])?data[addr_line+addr]:'.');
addr++;
}
debug_printf(1,"|\n");
addr_line+=16;
}
}
void dump_uuid(__u8 *data) {
debug_printf(1,"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
data[0],data[1],data[2],data[3],
data[4],data[5],
data[6],data[7],
data[8],data[9],
data[10],data[11],data[12],data[13],data[14],data[15]);
}
void dump_smi(struct emu *emu) {
if (!emu->smi_Buffer_Ptr_Address) {
return;
}
__u8 *smi = mem_guest2host(emu, emu->smi_Buffer_Ptr_Address);
if (!smi) {
return;
}
if (*smi == 0x5) {
struct smi_buffer05 *smi5 = (struct smi_buffer05 *)smi;
debug_printf(1,"buf: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
smi5->unk1, smi5->unk2, smi5->unk3, smi5->unk4,
smi5->unk5, smi5->unk6, smi5->unk7, smi5->unk8,
smi5->unk9
);
debug_printf(1,"\tuuid=");
dump_uuid(smi5->uuid);
debug_printf(1,"\n");
__u8 *dosbuf = mem_guest2host(emu, 0x810);
dump_hex(dosbuf, 0x1000);
} else {
struct smi_buffer00 *smi0 = (struct smi_buffer00 *)smi;
debug_printf(1,"buf: 0x%x 0x%x 0x%x size=0x%x uuid=",
smi0->unk1, smi0->unk2, smi0->unk3, smi0->size
);
dump_uuid(smi0->uuid);
debug_printf(1,"\n");
dump_hex(smi0->data, smi0->size - sizeof(*smi0));
}
}
void dump_dwords(void *data, int words) {
__u32 *p = data;
if (!data) {
return;
}
int i = 0;
/* FIXME - can run off the end of the segment easily */
while(i<words) {
if (i%7 == 0) {
debug_printf(1,"\n ");
}
debug_printf(1,"0x%08x ",*p++);
i++;
}
debug_printf(1,"\n");
}
void dump_kvm_regs(struct kvm_regs *regs) {
debug_printf(1,"ax=0x%08llx bx=0x%08llx cx=0x%08llx dx=0x%08llx flags=0x%08llx\n",
regs->rax,regs->rbx,regs->rcx,regs->rdx,regs->rflags);
#if 0
debug_printf(1,"8=0x%08x 9=0x%08x 10=0x%08x 11=0x%08x 12=0x%08x\n",
regs->r8,regs->r9,regs->r10,regs->r11,regs->r12);
#endif
debug_printf(1,"si=0x%08llx di=0x%08llx sp=0x%08llx bp=0x%08llx ip=0x%08llx ",
regs->rsi,regs->rdi,regs->rsp,regs->rbp,regs->rip);
debug_printf(1,"(%07x)\n",get_retaddr(&emu_global,regs));
}
void dump_kvm_segment(struct kvm_segment *seg, char *name) {
unsigned int limit;
if (seg->g) {
limit = (seg->limit <<12) + 0xfff;
} else {
limit = seg->limit;
}
debug_printf(1,"%s:%02x %08llx(%08x) type=%x dpl=%i %s%s%s%s%s\n",
name,seg->selector,seg->base,limit,
seg->type, seg->dpl,
seg->present?"P":"_",
seg->db?"B":"_",
seg->s?"U":"S",
seg->l?"L":"_",
seg->g?"G":"_"
);
}
void dump_descriptor(struct gdt_entry *gdt, __u16 selector) {
struct kvm_segment seg;
seg.base = gdt->base_l | gdt->base_m<<16 | gdt->base_h<<24;
seg.limit = gdt->limit_l | (gdt->limit_flags & 0x0f)<<16;
seg.selector = selector;
seg.type = gdt->type_flags & 0xf;
seg.present = (gdt->type_flags & 0x80)>>7;
seg.dpl = (gdt->type_flags & 0x60)>>5;
seg.db = (gdt->limit_flags & 0x40)>>6;
seg.s = (gdt->type_flags & 0x10)>>4;
seg.l = (gdt->limit_flags & 0x20)>>5;
seg.g = (gdt->limit_flags & 0x80)>>7;
dump_kvm_segment(&seg,"_dt");
}
void dump_kvm_dtable(struct kvm_dtable *seg, char *name) {
debug_printf(1,"%s: %08llx(%08x)\n",
name,seg->base,seg->limit);
}
void dump_kvm_sregs(struct emu *emu) {
struct kvm_sregs sregs;
int ret = ioctl(emu->vcpufd, KVM_GET_SREGS, &sregs);
if (ret == -1)
err(1, "KVM_GET_SREGS");
debug_printf(1,"cr0=0x%08llx\n",
sregs.cr0);
dump_kvm_segment(&sregs.cs,"cs");
dump_kvm_segment(&sregs.ds,"ds");
dump_kvm_segment(&sregs.es,"es");
dump_kvm_segment(&sregs.es,"fs");
dump_kvm_segment(&sregs.es,"gs");
dump_kvm_segment(&sregs.ss,"ss");
// dump_kvm_segment(&sregs.tr,"tr");
// dump_kvm_segment(&sregs.ldt,"ldt");
dump_kvm_dtable(&sregs.gdt,"gdt");
dump_kvm_dtable(&sregs.idt,"idt");
struct gdt_entry *gdt = (struct gdt_entry *)emu->mem[MEM_REGION_GDT].userspace_addr;
for (int i=1; i<emu->gdt_brk; i++) {
dump_descriptor(&gdt[i],i<<3);
}
#if 0
debug_printf(1,"irq:");
for (int i = 0; i < (KVM_NR_INTERRUPTS + 63) / 64; i++) {
debug_printf(1,"%016llx",sregs.interrupt_bitmap[i]);
}
#endif
debug_printf(1,"\n");
}
void dump_kvm_memmap_one(struct kvm_userspace_memory_region *p) {
debug_printf(1,"%i: 0x%08llx(0x%08llx) = 0x%08llx (flags=%x)\n",
p->slot, p->guest_phys_addr, p->memory_size,
p->userspace_addr, p->flags
);
}
void dump_kvm_memmap(struct emu *emu) {
struct kvm_userspace_memory_region *p = &emu->mem[0];
debug_printf(1,"Memmap:\n");
for (int i=0; i<MEM_REGION_MAX; i++,p++) {
dump_kvm_memmap_one(p);
}
}
void dump_kvm_exit(struct emu *emu) {
struct kvm_regs regs;
__u32 *stack;
int ret = ioctl(emu->vcpufd, KVM_GET_REGS, ®s);
if (ret == -1)
err(1, "KVM_GET_REGS");
debug_printf(1,"\n");
dump_kvm_run(emu->run);
dump_kvm_regs(®s);
switch(emu->run->exit_reason) {
case KVM_EXIT_INTERNAL_ERROR:
case KVM_EXIT_SHUTDOWN:
case KVM_EXIT_MMIO:
case KVM_EXIT_IO:
stack = mem_getstack(emu, regs.rsp);
if (stack) {
debug_printf(0,"Stack:");
dump_dwords(stack,16);
}
dump_backtrace(emu,®s);
dump_kvm_sregs(emu);
dump_kvm_memmap(emu);
break;
}
}
#define MEMR_REGISTER 1
#define MEMR_ANONYMOUS 2
#define MEMR_RDWR 4
int load_memory(struct emu *emu, __u32 phys_addr, __u32 size, char *filename, __u32 file_offset, __u32 flags) {
int fd;
int mmap_prot;
int mmap_flags;
if (!(flags & MEMR_ANONYMOUS)) {
int oflag;
if ((flags & MEMR_RDWR)) {
oflag = O_RDWR;
} else {
oflag = O_RDONLY;
}
fd = open(filename, oflag);
if (fd == -1)
errx(1, "opening file %s",filename);
mmap_prot = PROT_READ;
mmap_flags = MAP_SHARED;
} else {
file_offset = 0;
fd = -1;
mmap_prot = PROT_READ;
mmap_flags = MAP_SHARED | MAP_ANONYMOUS;
}
if ((flags & MEMR_RDWR)) {
mmap_prot |= PROT_WRITE;
}
void *region = mmap(NULL, size, mmap_prot, mmap_flags, fd, file_offset);
if (!region)
errx(1, "mmap memory from %s",filename);
int slot = emu->region_brk++;
if (slot > MEM_REGION_MAX) {
errx(1, "too many memory regions loading %s",filename);
}
emu->mem[slot].slot = slot;
emu->mem[slot].guest_phys_addr = phys_addr;
emu->mem[slot].memory_size = size;
emu->mem[slot].userspace_addr = (uint64_t)region;
if (flags & MEMR_REGISTER) {
/*
* by adding the region to the mem table, but not registering it, it becomes
* straightforward to trace accesses as MMIO exits
*/
int ret = ioctl(emu->vmfd, KVM_SET_USER_MEMORY_REGION, &emu->mem[slot]);
if (ret == -1)
errx(1, "KVM_SET_USER_MEMORY_REGION %i: %s",slot,filename);
}
return slot;
}
void setup_gdt(struct kvm_sregs *sregs, struct emu *emu) {
struct gdt_entry *gdt = mmap(NULL, REGION_GDT_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (!gdt)
err(1, "allocating gdt memory");
memset(gdt,0,REGION_GDT_SIZE);
/* code segment */
gdt[1].type_flags = 0x9e;
gdt[1].limit_flags = 0x40; /* Set the 32bit segment flag */
gdt_setlimit(&gdt[1],0xffffffff);
gdt_setbase(&gdt[1],0);
/* data segment */
gdt[2].type_flags = 0x92;
gdt[2].limit_flags = 0x40;
gdt_setlimit(&gdt[2],0xffffffff);
gdt_setbase(&gdt[2],0);
/* PSP segment */
gdt[3].type_flags = 0x92;
gdt[3].limit_flags = 0x40;
gdt_setlimit(&gdt[3],SEG_PSP_SIZE);
gdt_setbase(&gdt[3],SEG_PSP_BASE);
/* ENV segment */
gdt[4].type_flags = 0x92;
gdt[4].limit_flags = 0x40;
gdt_setlimit(&gdt[4],SEG_ENV_SIZE);
gdt_setbase(&gdt[4],SEG_ENV_BASE);
/* GO32 segment */
gdt[5].type_flags = 0x92;
gdt[5].limit_flags = 0x40;
gdt_setlimit(&gdt[5],SEG_GO32_SIZE);
gdt_setbase(&gdt[5],SEG_GO32_BASE);
/* Segment used for running the exit code */
gdt[6].type_flags = 0x9e;
gdt[6].limit_flags = 0x40;
gdt_setlimit(&gdt[6],SEG_GO32_SIZE);
gdt_setbase(&gdt[6],SEG_GO32_BASE);
emu->mem[MEM_REGION_GDT].slot = MEM_REGION_GDT;
emu->mem[MEM_REGION_GDT].guest_phys_addr = REGION_GDT_BASE;
emu->mem[MEM_REGION_GDT].memory_size = REGION_GDT_SIZE;
emu->mem[MEM_REGION_GDT].userspace_addr = (uint64_t)gdt;
int ret = ioctl(emu->vmfd, KVM_SET_USER_MEMORY_REGION, &emu->mem[MEM_REGION_GDT]);
if (ret == -1)
errx(1, "KVM_SET_USER_MEMORY_REGION %i",__LINE__);
emu->gdt_brk = (SEG_SYS_MAX>>3) +1;
sregs->gdt.base = REGION_GDT_BASE;
sregs->gdt.limit = REGION_GDT_SIZE;
}
void setup_idt(struct kvm_sregs *sregs,struct emu *emu) {
struct idt *idt = mmap(NULL, REGION_IDT_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (!idt)
err(1, "allocating idt memory");
for (int i=0; i<256; i++) {
idt->entry[i].offset_l = sizeof(idt->entry) + i*sizeof(idt->hlt[i]);
idt->entry[i].selector = SEG_TEXT;
idt->entry[i].always0 = 0;
idt->entry[i].type_flags = 0xee; /* dpl=3, present, 32-bit interrupt */
idt->entry[i].offset_h = REGION_IDT_BASE>>16;
idt->hlt[i][0] = 0xe7; /* out imm8,ax */
idt->hlt[i][1] = 0x7f; /* imm8 = 0x7f */
idt->hlt[i][2] = 0xcf; /* iret */
idt->hlt[i][3] = 0xf4; /* hlt */
}
emu->mem[MEM_REGION_IDT].slot = MEM_REGION_IDT;
emu->mem[MEM_REGION_IDT].guest_phys_addr = REGION_IDT_BASE;
emu->mem[MEM_REGION_IDT].memory_size = REGION_IDT_SIZE;
emu->mem[MEM_REGION_IDT].userspace_addr = (uint64_t)idt;
int ret = ioctl(emu->vmfd, KVM_SET_USER_MEMORY_REGION, &emu->mem[MEM_REGION_IDT]);
if (ret == -1)
errx(1, "KVM_SET_USER_MEMORY_REGION %i",__LINE__);
sregs->idt.base = REGION_IDT_BASE;
sregs->idt.limit = sizeof(idt->entry);
}
void setup_flat_segments(struct kvm_sregs *sregs) {
sregs->cs.base = 0;
sregs->cs.limit = 0xfffff;
sregs->cs.selector = SEG_TEXT; /* gdt[1] */
sregs->cs.db = 1;
sregs->cs.g = 1;
sregs->cs.type = 0xe; /* x=1, c=1, r=1, a=0 */
memcpy(&sregs->ds,&sregs->cs,sizeof(sregs->cs));
sregs->ds.selector = SEG_DATA; /* gdt[2] */
sregs->ds.type = 2; /* x=0, e=0, w=1, a=0 */
/* FIXME - set ds.type correctly sregs->ds.type = 2 ? */
memcpy(&sregs->es,&sregs->ds,sizeof(sregs->ds));
memcpy(&sregs->fs,&sregs->ds,sizeof(sregs->ds));
sregs->fs.g = 0;
sregs->fs.selector = SEG_GO32;
sregs->fs.base = SEG_GO32_BASE;
sregs->fs.limit = SEG_GO32_SIZE;
memcpy(&sregs->gs,&sregs->ds,sizeof(sregs->ds));
memcpy(&sregs->ss,&sregs->ds,sizeof(sregs->ds));
sregs->ss.type = 6; /* x=0,e=1,w=1,a=0 */
/* FIXME
* - descriptor table too
*/
}
int kvm_init(struct emu *emu) {
struct kvm_sregs sregs;
emu->kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
if (emu->kvm == -1)
err(1, "/dev/kvm");
/* Make sure we have the stable version of the API */
int ret = ioctl(emu->kvm, KVM_GET_API_VERSION, NULL);
if (ret == -1)
err(1, "KVM_GET_API_VERSION");
if (ret != 12)
errx(1, "KVM_GET_API_VERSION %d, expected 12", ret);
emu->vmfd = ioctl(emu->kvm, KVM_CREATE_VM, (unsigned long)0);
if (emu->vmfd == -1)
err(1, "KVM_CREATE_VM");
emu->vcpufd = ioctl(emu->vmfd, KVM_CREATE_VCPU, (unsigned long)0);
if (emu->vcpufd == -1)
err(1, "KVM_CREATE_VCPU");
/* Map the shared kvm_run structure and following data. */
ret = ioctl(emu->kvm, KVM_GET_VCPU_MMAP_SIZE, NULL);
if (ret == -1)
err(1, "KVM_GET_VCPU_MMAP_SIZE");
size_t mmap_size = ret;
if (mmap_size < sizeof(*emu->run))
errx(1, "KVM_GET_VCPU_MMAP_SIZE unexpectedly small");
emu->run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, emu->vcpufd, 0);
if (!emu->run)
err(1, "mmap vcpu");
/* Initialize CS to point at 0, via a read-modify-write of sregs. */
ret = ioctl(emu->vcpufd, KVM_GET_SREGS, &sregs);
if (ret == -1)
err(1, "KVM_GET_SREGS");
sregs.cr0 = 0x1; /* protected mode enable */
setup_flat_segments(&sregs);
setup_idt(&sregs,emu);
setup_gdt(&sregs,emu);
ret = ioctl(emu->vcpufd, KVM_SET_SREGS, &sregs);
if (ret == -1)
err(1, "KVM_SET_SREGS");
debug_printf(1,"Map stack\n");
ret = load_memory(emu,REGION_STACK_BASE,REGION_STACK_SIZE,"stack",0,MEMR_REGISTER|MEMR_ANONYMOUS|MEMR_RDWR);
if (ret == -1)
err(1, "load_memory stack");
emu->region_stack = ret;
/* Stack canary - this should point to unmapped memory */
uint8_t *stack = mem_guest2host(emu, REGION_STACK_BASE+REGION_STACK_SIZE-4);
*((__u32*)stack) = 0xbad0add0;
debug_printf(1,"Map bss\n");
ret = load_memory(emu,REGION_BSS_BASE,emu->bss_size,"bss",0,MEMR_REGISTER|MEMR_ANONYMOUS|MEMR_RDWR);
if (ret == -1)
err(1, "load_memory bss");
/* initialise the starting alloc brk */
emu->bss_brk = 0;
return 0;
}
int load_image(struct emu *emu, char *filename) {
int ret;
const uint8_t code[] = {
0xba, 0xf8, 0x03, 0,0, /* mov $0x3f8, %dx */
0x00, 0xd8, /* add %bl, %al */
0x04, '0', /* add $'0', %al */
0xee, /* out %al, (%dx) */
0xb0, '\n', /* mov $'\n', %al */
0xee, /* out %al, (%dx) */
0xe7, /* out imm8,ax */
0xb2, /* imm8 */
0xf4, /* hlt */
};
__u64 text_size;
uint8_t *text;
if (*filename == '-') {
text_size = 0x2000;
/* Allocate one aligned page of guest memory to hold the code. */
text = mmap(NULL, text_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (!text)
err(1, "allocating guest memory");
memcpy(text+0x1000, code, sizeof(code));
} else {
int fd = open(filename, O_RDONLY);
if (fd == -1)
err(1, "opening file");
struct stat s;
ret = fstat(fd,&s);
if (ret == -1)
err(1, "statting file");
text_size = ((s.st_size >>12) +1 )<<12;
text = mmap(NULL, text_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (!text)
err(1, "allocating guest memory");
uint8_t *p = text;
while(s.st_size) {
ret = read(fd,p,4096);
if (ret == -1)
err(1, "reading file");
s.st_size -= ret;
p += ret;
}
}
emu->mem[MEM_REGION_ZERO].slot = MEM_REGION_ZERO;
emu->mem[MEM_REGION_ZERO].guest_phys_addr = 0;
emu->mem[MEM_REGION_ZERO].memory_size = 0x1000;
emu->mem[MEM_REGION_ZERO].userspace_addr = (uint64_t)text;
emu->mem[MEM_REGION_TEXT].slot = MEM_REGION_TEXT;
emu->mem[MEM_REGION_TEXT].guest_phys_addr = 0x1000;
emu->mem[MEM_REGION_TEXT].memory_size = text_size;
emu->mem[MEM_REGION_TEXT].userspace_addr = (uint64_t)(text+0x1000);
ret = ioctl(emu->vmfd, KVM_SET_USER_MEMORY_REGION, &emu->mem[MEM_REGION_TEXT]);
if (ret == -1)
errx(1, "KVM_SET_USER_MEMORY_REGION %i",__LINE__);
uint8_t *psp = mmap(NULL, REGION_PSP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (!psp)
err(1, "allocating PSP area");
emu->mem[MEM_REGION_PSP].slot = MEM_REGION_PSP;
emu->mem[MEM_REGION_PSP].guest_phys_addr = REGION_PSP_BASE;
emu->mem[MEM_REGION_PSP].memory_size = REGION_PSP_SIZE;
emu->mem[MEM_REGION_PSP].userspace_addr = (uint64_t)psp;
ret = ioctl(emu->vmfd, KVM_SET_USER_MEMORY_REGION, &emu->mem[MEM_REGION_PSP]);
if (ret == -1)
errx(1, "KVM_SET_USER_MEMORY_REGION %i",__LINE__);
/* This segment is going to need more data, so fill it with a canary so
* that it will be simpler to see
*/
memset(psp,0xf5,REGION_PSP_SIZE);
struct region_psp *region_psp = (struct region_psp *)psp;
/* FIXME - define the PSP structure */
*(__u16*)((__u8*)psp+0x2c) = SEG_ENV; /* environment segment */
memset(region_psp->env,0,sizeof(region_psp->env));
region_psp->cmdline_len=0;
region_psp->cmdline[0]=0;
if (emu->cmdline) {
strncat(region_psp->cmdline,emu->cmdline,sizeof(region_psp->cmdline));
strncat(region_psp->cmdline,"\r",sizeof(region_psp->cmdline));
region_psp->cmdline_len = strlen(region_psp->cmdline);
}
memcpy(®ion_psp->stubinfo.magic,"go32stub, v 2.HC",16);
region_psp->stubinfo.size = sizeof(struct djgcc_stubinfo);
region_psp->stubinfo.minstack = 0x80000; /* 512k */
region_psp->stubinfo.memory_handle = 0xfeedbad0;
region_psp->stubinfo.initial_size = text_size;
region_psp->stubinfo.minkeep = REGION_TB_SIZE;
region_psp->stubinfo.ds_selector = SEG_GO32;
region_psp->stubinfo.ds_segment = REGION_TB_BASE >>4; /* gets shl 4 and xref 0x0003de8d */
region_psp->stubinfo.psp_selector = SEG_PSP;
region_psp->stubinfo.cs_selector = SEG_GO32_TEXT;
region_psp->stubinfo.env_size = 1;
memset(®ion_psp->stubinfo.basename,0,8);
memset(®ion_psp->stubinfo.argv0,0,16);
memset(®ion_psp->stubinfo.dpmi_server,0,16);
/* Details from djgcc djlsr205.zip/src/stub/stub.asm
*
* Interface to 32-bit executable:
*
* cs:eip according to COFF header
* ds 32-bit data segment for COFF program
* fs selector for our data segment (fs:0 is stubinfo)
* ss:sp our stack (ss to be freed)
* <others> All unspecified registers have unspecified values in them.
*/
struct kvm_regs regs = {
.rip = emu->entry,
.rax = 0,
.rbx = 0,
.rflags = 0x2,
.rsp = REGION_STACK_BASE + REGION_STACK_SIZE - 0x10,
};
ret = ioctl(emu->vcpufd, KVM_SET_REGS, ®s);
if (ret == -1)
err(1, "KVM_SET_REGS");
*text = 0xf4; /* hlt - hack to allow the code to stop */
return 0;
}
void load_patch_file(struct emu *emu, char *filename) {
FILE *file = fopen(filename, "r");
if (!file)
err(1, "opening patch file");
debug_printf(0,"Patching memory image\n");
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, file)) != -1) {
__u64 addr;
int patch = 0;
uint8_t buf[32]; /* we only expect lines of 16 bytes.. but .. */
char *p = line;
if (*p == '+') {
patch = 1;
} else if (*p != '-') {
continue;
}
/* it is either a plus or a minus */
p++;
char *p2;
addr = strtoul(p,&p2,16);
if (p2==p) {
debug_printf(0,"unexpected patch data\n");
exit(1);
}
p=p2;
uint8_t *data = mem_guest2host(emu, addr);
if (!data) {
debug_printf(0,"Could not locate address 0x%08llx\n",addr);
exit(1);
}
int size=0;
while(*p) {
/* FIXME - can overflow buf */
buf[size] = strtoul(p,&p2,16);
if (p2==p) {
/* nothing matched, this is a line end */
break;