-
Notifications
You must be signed in to change notification settings - Fork 0
/
arm64.c
4869 lines (4227 loc) · 140 KB
/
arm64.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
/*
* arm64.c - core analysis suite
*
* Copyright (C) 2012-2020 David Anderson
* Copyright (C) 2012-2020 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifdef ARM64
#include "defs.h"
#include <elf.h>
#include <endian.h>
#include <math.h>
#include <sys/ioctl.h>
#define NOT_IMPLEMENTED(X) error((X), "%s: function not implemented\n", __func__)
static struct machine_specific arm64_machine_specific = { 0 };
static int arm64_verify_symbol(const char *, ulong, char);
static void arm64_parse_cmdline_args(void);
static int arm64_search_for_kimage_voffset(ulong);
static int verify_kimage_voffset(void);
static void arm64_calc_kimage_voffset(void);
static void arm64_calc_phys_offset(void);
static void arm64_calc_physvirt_offset(void);
static void arm64_calc_virtual_memory_ranges(void);
static void arm64_get_section_size_bits(void);
static int arm64_kdump_phys_base(ulong *);
static ulong arm64_processor_speed(void);
static void arm64_init_kernel_pgd(void);
static int arm64_kvtop(struct task_context *, ulong, physaddr_t *, int);
static int arm64_uvtop(struct task_context *, ulong, physaddr_t *, int);
static int arm64_vtop_2level_64k(ulong, ulong, physaddr_t *, int);
static int arm64_vtop_3level_64k(ulong, ulong, physaddr_t *, int);
static int arm64_vtop_3level_4k(ulong, ulong, physaddr_t *, int);
static int arm64_vtop_4level_4k(ulong, ulong, physaddr_t *, int);
static ulong arm64_get_task_pgd(ulong);
static void arm64_irq_stack_init(void);
static void arm64_overflow_stack_init(void);
static void arm64_stackframe_init(void);
static int arm64_eframe_search(struct bt_info *);
static int arm64_is_kernel_exception_frame(struct bt_info *, ulong);
static int arm64_in_exception_text(ulong);
static int arm64_in_exp_entry(ulong);
static void arm64_back_trace_cmd(struct bt_info *);
static void arm64_back_trace_cmd_v2(struct bt_info *);
static void arm64_print_text_symbols(struct bt_info *, struct arm64_stackframe *, FILE *);
static int arm64_print_stackframe_entry(struct bt_info *, int, struct arm64_stackframe *, FILE *);
static int arm64_print_stackframe_entry_v2(struct bt_info *, int, struct arm64_stackframe *, FILE *);
static void arm64_display_full_frame(struct bt_info *, ulong);
static void arm64_display_full_frame_v2(struct bt_info *, struct arm64_stackframe *, struct arm64_stackframe *);
static int arm64_unwind_frame(struct bt_info *, struct arm64_stackframe *);
static int arm64_unwind_frame_v2(struct bt_info *, struct arm64_stackframe *, FILE *);
static int arm64_get_dumpfile_stackframe(struct bt_info *, struct arm64_stackframe *);
static int arm64_in_kdump_text(struct bt_info *, struct arm64_stackframe *);
static int arm64_in_kdump_text_on_irq_stack(struct bt_info *);
static int arm64_switch_stack(struct bt_info *, struct arm64_stackframe *, FILE *);
static int arm64_switch_stack_from_overflow(struct bt_info *, struct arm64_stackframe *, FILE *);
static int arm64_get_stackframe(struct bt_info *, struct arm64_stackframe *);
static void arm64_get_stack_frame(struct bt_info *, ulong *, ulong *);
static void arm64_gen_hidden_frame(struct bt_info *bt, ulong, struct arm64_stackframe *);
static void arm64_print_exception_frame(struct bt_info *, ulong, int, FILE *);
static void arm64_do_bt_reference_check(struct bt_info *, ulong, char *);
static int arm64_translate_pte(ulong, void *, ulonglong);
static ulong arm64_vmalloc_start(void);
static int arm64_is_task_addr(ulong);
static int arm64_dis_filter(ulong, char *, unsigned int);
static void arm64_cmd_mach(void);
static void arm64_display_machine_stats(void);
static int arm64_get_smp_cpus(void);
static void arm64_clear_machdep_cache(void);
static int arm64_on_process_stack(struct bt_info *, ulong);
static int arm64_in_alternate_stack(int, ulong);
static int arm64_in_alternate_stackv(int cpu, ulong stkptr, ulong *stacks, ulong stack_size);
static int arm64_on_irq_stack(int, ulong);
static int arm64_on_overflow_stack(int, ulong);
static void arm64_set_irq_stack(struct bt_info *);
static void arm64_set_overflow_stack(struct bt_info *);
static void arm64_set_process_stack(struct bt_info *);
static int arm64_get_kvaddr_ranges(struct vaddr_range *);
static void arm64_get_crash_notes(void);
static void arm64_calc_VA_BITS(void);
static int arm64_is_uvaddr(ulong, struct task_context *);
static void arm64_calc_KERNELPACMASK(void);
struct kernel_range {
unsigned long modules_vaddr, modules_end;
unsigned long vmalloc_start_addr, vmalloc_end;
unsigned long vmemmap_vaddr, vmemmap_end;
};
static struct kernel_range *arm64_get_va_range(struct machine_specific *ms);
static void arm64_get_struct_page_size(struct machine_specific *ms);
static void arm64_calc_kernel_start(void)
{
struct machine_specific *ms = machdep->machspec;
struct syment *sp;
if (THIS_KERNEL_VERSION >= LINUX(5,11,0))
sp = kernel_symbol_search("_stext");
else
sp = kernel_symbol_search("_text");
ms->kimage_text = (sp ? sp->value : 0);
sp = kernel_symbol_search("_end");
ms->kimage_end = (sp ? sp->value : 0);
}
/*
* Do all necessary machine-specific setup here. This is called several times
* during initialization.
*/
void
arm64_init(int when)
{
ulong value;
char *string;
struct machine_specific *ms;
#if defined(__x86_64__)
if (ACTIVE())
error(FATAL, "compiled for the ARM64 architecture\n");
#endif
switch (when) {
case SETUP_ENV:
machdep->process_elf_notes = process_elf64_notes;
break;
case PRE_SYMTAB:
machdep->machspec = &arm64_machine_specific;
machdep->verify_symbol = arm64_verify_symbol;
if (pc->flags & KERNEL_DEBUG_QUERY)
return;
machdep->verify_paddr = generic_verify_paddr;
if (machdep->cmdline_args[0])
arm64_parse_cmdline_args();
machdep->flags |= MACHDEP_BT_TEXT;
ms = machdep->machspec;
/*
* The st->_stext_vmlinux is needed in arm64_init(PRE_GDB) when a
* dumpfile does not have vmcoreinfo and we use -m vabits_actual
* option, e.g. a raw RAM dumpfile.
*/
if (ms->VA_BITS_ACTUAL)
st->_stext_vmlinux = UNINITIALIZED;
if (!ms->kimage_voffset && STREQ(pc->live_memsrc, "/dev/crash"))
ioctl(pc->mfd, DEV_CRASH_ARCH_DATA, &ms->kimage_voffset);
if (!ms->kimage_voffset &&
(string = pc->read_vmcoreinfo("NUMBER(kimage_voffset)"))) {
ms->kimage_voffset = htol(string, QUIET, NULL);
free(string);
}
if (ms->kimage_voffset ||
(ACTIVE() && (symbol_value_from_proc_kallsyms("kimage_voffset") != BADVAL))) {
machdep->flags |= NEW_VMEMMAP;
/*
* Even if CONFIG_RANDOMIZE_BASE is not configured,
* derive_kaslr_offset() should work and set
* kt->relocate to 0
*/
if (!kt->relocate && !(kt->flags2 & (RELOC_AUTO|KASLR)))
kt->flags2 |= (RELOC_AUTO|KASLR);
}
break;
case PRE_GDB:
if (kernel_symbol_exists("kimage_voffset"))
machdep->flags |= NEW_VMEMMAP;
if (!machdep->pagesize &&
(string = pc->read_vmcoreinfo("PAGESIZE"))) {
machdep->pagesize = atoi(string);
free(string);
}
if (!machdep->pagesize) {
/*
* Kerneldoc Documentation/arm64/booting.txt describes
* the kernel image header flags field.
*/
value = machdep->machspec->kernel_flags;
value = (value >> 1) & 3;
switch(value)
{
case 0:
break;
case 1:
machdep->pagesize = 4096;
break;
case 2:
/* TODO: machdep->pagesize = 16384; */
error(FATAL, "16K pages not supported.");
break;
case 3:
machdep->pagesize = 65536;
break;
}
}
/*
* This code section will only be executed if the kernel is
* earlier than Linux 4.4 (if there is no vmcoreinfo)
*/
if (!machdep->pagesize &&
kernel_symbol_exists("swapper_pg_dir") &&
kernel_symbol_exists("idmap_pg_dir")) {
value = symbol_value("swapper_pg_dir") -
symbol_value("idmap_pg_dir");
/*
* idmap_pg_dir is 2 pages prior to 4.1,
* and 3 pages thereafter. Only 4K and 64K
* page sizes are supported.
*/
switch (value)
{
case (4096 * 2):
case (4096 * 3):
machdep->pagesize = 4096;
break;
case (65536 * 2):
case (65536 * 3):
machdep->pagesize = 65536;
break;
}
} else if (ACTIVE())
machdep->pagesize = memory_page_size(); /* host */
machdep->pageshift = ffs(machdep->pagesize) - 1;
machdep->pageoffset = machdep->pagesize - 1;
machdep->pagemask = ~((ulonglong)machdep->pageoffset);
ms = machdep->machspec;
arm64_get_struct_page_size(ms);
arm64_calc_VA_BITS();
arm64_calc_KERNELPACMASK();
/* vabits_actual introduced after mm flip, so it should be flipped layout */
if (ms->VA_BITS_ACTUAL) {
ms->page_offset = ARM64_FLIP_PAGE_OFFSET;
/* useless on arm64 */
machdep->identity_map_base = ARM64_FLIP_PAGE_OFFSET;
machdep->kvbase = ARM64_FLIP_PAGE_OFFSET;
ms->userspace_top = ARM64_USERSPACE_TOP_ACTUAL;
} else {
ms->page_offset = ARM64_PAGE_OFFSET;
machdep->identity_map_base = ARM64_PAGE_OFFSET;
machdep->kvbase = ARM64_VA_START;
ms->userspace_top = ARM64_USERSPACE_TOP;
}
machdep->is_kvaddr = generic_is_kvaddr;
machdep->kvtop = arm64_kvtop;
/* The defaults */
ms->vmalloc_end = ARM64_VMALLOC_END;
ms->vmemmap_vaddr = ARM64_VMEMMAP_VADDR;
ms->vmemmap_end = ARM64_VMEMMAP_END;
if (machdep->flags & NEW_VMEMMAP) {
struct syment *sp;
struct kernel_range *r;
/* It is finally decided in arm64_calc_kernel_start() */
sp = kernel_symbol_search("_text");
ms->kimage_text = (sp ? sp->value : 0);
sp = kernel_symbol_search("_end");
ms->kimage_end = (sp ? sp->value : 0);
if (ms->struct_page_size && (r = arm64_get_va_range(ms))) {
/* We can get all the MODULES/VMALLOC/VMEMMAP ranges now.*/
ms->modules_vaddr = r->modules_vaddr;
ms->modules_end = r->modules_end - 1;
ms->vmalloc_start_addr = r->vmalloc_start_addr;
ms->vmalloc_end = r->vmalloc_end - 1;
ms->vmemmap_vaddr = r->vmemmap_vaddr;
ms->vmemmap_end = r->vmemmap_end - 1;
} else if (ms->VA_BITS_ACTUAL) {
ms->modules_vaddr = (st->_stext_vmlinux & TEXT_OFFSET_MASK) - ARM64_MODULES_VSIZE;
ms->modules_end = ms->modules_vaddr + ARM64_MODULES_VSIZE -1;
ms->vmalloc_start_addr = ms->modules_end + 1;
} else {
ms->modules_vaddr = ARM64_VA_START;
if (kernel_symbol_exists("kasan_init"))
ms->modules_vaddr += ARM64_KASAN_SHADOW_SIZE;
ms->modules_end = ms->modules_vaddr + ARM64_MODULES_VSIZE -1;
ms->vmalloc_start_addr = ms->modules_end + 1;
}
arm64_calc_kimage_voffset();
} else {
ms->modules_vaddr = ARM64_PAGE_OFFSET - MEGABYTES(64);
ms->modules_end = ARM64_PAGE_OFFSET - 1;
ms->vmalloc_start_addr = ARM64_VA_START;
}
switch (machdep->pagesize)
{
case 4096:
machdep->ptrs_per_pgd = PTRS_PER_PGD_L3_4K;
if ((machdep->pgd =
(char *)malloc(PTRS_PER_PGD_L3_4K * 8)) == NULL)
error(FATAL, "cannot malloc pgd space.");
if (machdep->machspec->VA_BITS > PGDIR_SHIFT_L4_4K) {
machdep->flags |= VM_L4_4K;
if ((machdep->pud =
(char *)malloc(PTRS_PER_PUD_L4_4K * 8))
== NULL)
error(FATAL, "cannot malloc pud space.");
} else {
machdep->flags |= VM_L3_4K;
machdep->pud = NULL; /* not used */
}
if ((machdep->pmd =
(char *)malloc(PTRS_PER_PMD_L3_4K * 8)) == NULL)
error(FATAL, "cannot malloc pmd space.");
if ((machdep->ptbl =
(char *)malloc(PTRS_PER_PTE_L3_4K * 8)) == NULL)
error(FATAL, "cannot malloc ptbl space.");
break;
case 65536:
if (kernel_symbol_exists("idmap_ptrs_per_pgd") &&
readmem(symbol_value("idmap_ptrs_per_pgd"), KVADDR,
&value, sizeof(ulong), "idmap_ptrs_per_pgd", QUIET|RETURN_ON_ERROR))
machdep->ptrs_per_pgd = value;
if (machdep->machspec->VA_BITS > PGDIR_SHIFT_L3_64K) {
machdep->flags |= VM_L3_64K;
if (!machdep->ptrs_per_pgd)
machdep->ptrs_per_pgd = PTRS_PER_PGD_L3_64K;
if ((machdep->pgd =
(char *)malloc(machdep->ptrs_per_pgd * 8)) == NULL)
error(FATAL, "cannot malloc pgd space.");
if ((machdep->pmd =
(char *)malloc(PTRS_PER_PMD_L3_64K * 8)) == NULL)
error(FATAL, "cannot malloc pmd space.");
if ((machdep->ptbl =
(char *)malloc(PTRS_PER_PTE_L3_64K * 8)) == NULL)
error(FATAL, "cannot malloc ptbl space.");
} else {
machdep->flags |= VM_L2_64K;
if (!machdep->ptrs_per_pgd)
machdep->ptrs_per_pgd = PTRS_PER_PGD_L2_64K;
if ((machdep->pgd =
(char *)malloc(machdep->ptrs_per_pgd * 8)) == NULL)
error(FATAL, "cannot malloc pgd space.");
if ((machdep->ptbl =
(char *)malloc(PTRS_PER_PTE_L2_64K * 8)) == NULL)
error(FATAL, "cannot malloc ptbl space.");
machdep->pmd = NULL; /* not used */
}
machdep->pud = NULL; /* not used */
break;
default:
if (machdep->pagesize)
error(FATAL, "invalid/unsupported page size: %d\n",
machdep->pagesize);
else
error(FATAL, "cannot determine page size\n");
}
machdep->last_pgd_read = 0;
machdep->last_pud_read = 0;
machdep->last_pmd_read = 0;
machdep->last_ptbl_read = 0;
machdep->clear_machdep_cache = arm64_clear_machdep_cache;
machdep->stacksize = ARM64_STACK_SIZE;
machdep->flags |= VMEMMAP;
machdep->uvtop = arm64_uvtop;
machdep->is_uvaddr = arm64_is_uvaddr;
machdep->eframe_search = arm64_eframe_search;
machdep->back_trace = arm64_back_trace_cmd;
machdep->in_alternate_stack = arm64_in_alternate_stack;
machdep->processor_speed = arm64_processor_speed;
machdep->get_task_pgd = arm64_get_task_pgd;
machdep->get_stack_frame = arm64_get_stack_frame;
machdep->get_stackbase = generic_get_stackbase;
machdep->get_stacktop = generic_get_stacktop;
machdep->translate_pte = arm64_translate_pte;
machdep->memory_size = generic_memory_size;
machdep->vmalloc_start = arm64_vmalloc_start;
machdep->get_kvaddr_ranges = arm64_get_kvaddr_ranges;
machdep->is_task_addr = arm64_is_task_addr;
machdep->dis_filter = arm64_dis_filter;
machdep->cmd_mach = arm64_cmd_mach;
machdep->get_smp_cpus = arm64_get_smp_cpus;
machdep->line_number_hooks = NULL;
machdep->value_to_symbol = generic_machdep_value_to_symbol;
machdep->dump_irq = generic_dump_irq;
machdep->show_interrupts = generic_show_interrupts;
machdep->get_irq_affinity = generic_get_irq_affinity;
machdep->dumpfile_init = NULL;
machdep->verify_line_number = NULL;
machdep->init_kernel_pgd = arm64_init_kernel_pgd;
/* use machdep parameters */
arm64_calc_phys_offset();
arm64_calc_physvirt_offset();
if (CRASHDEBUG(1)) {
if (machdep->flags & NEW_VMEMMAP)
fprintf(fp, "kimage_voffset: %lx\n",
machdep->machspec->kimage_voffset);
fprintf(fp, "phys_offset: %lx\n",
machdep->machspec->phys_offset);
fprintf(fp, "physvirt_offset: %lx\n", machdep->machspec->physvirt_offset);
}
break;
case POST_GDB:
/* Rely on kernel version to decide the kernel start address */
arm64_calc_kernel_start();
/* Can we get the size of struct page before POST_GDB */
ms = machdep->machspec;
if (!ms->struct_page_size)
arm64_calc_virtual_memory_ranges();
arm64_get_section_size_bits();
if (!machdep->max_physmem_bits) {
if ((string = pc->read_vmcoreinfo("NUMBER(MAX_PHYSMEM_BITS)"))) {
machdep->max_physmem_bits = atol(string);
free(string);
} else if (machdep->machspec->VA_BITS == 52) /* guess */
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_52;
else if (THIS_KERNEL_VERSION >= LINUX(3,17,0))
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_3_17;
else
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS;
}
if (CRASHDEBUG(1)) {
if (ms->VA_BITS_ACTUAL) {
fprintf(fp, "CONFIG_ARM64_VA_BITS: %ld\n", ms->CONFIG_ARM64_VA_BITS);
fprintf(fp, " VA_BITS_ACTUAL: %ld\n", ms->VA_BITS_ACTUAL);
fprintf(fp, "(calculated) VA_BITS: %ld\n", ms->VA_BITS);
fprintf(fp, " PAGE_OFFSET: %lx\n", ARM64_FLIP_PAGE_OFFSET);
fprintf(fp, " VA_START: %lx\n", ms->VA_START);
fprintf(fp, " modules: %lx - %lx\n", ms->modules_vaddr, ms->modules_end);
fprintf(fp, " vmalloc: %lx - %lx\n", ms->vmalloc_start_addr, ms->vmalloc_end);
fprintf(fp, "kernel image: %lx - %lx\n", ms->kimage_text, ms->kimage_end);
fprintf(fp, " vmemmap: %lx - %lx\n\n", ms->vmemmap_vaddr, ms->vmemmap_end);
}
}
if (THIS_KERNEL_VERSION >= LINUX(5,19,0)) {
ms->__SWP_TYPE_BITS = 5;
ms->__SWP_TYPE_SHIFT = 3;
ms->__SWP_TYPE_MASK = ((1UL << ms->__SWP_TYPE_BITS) - 1);
ms->__SWP_OFFSET_SHIFT = (ms->__SWP_TYPE_BITS + ms->__SWP_TYPE_SHIFT);
ms->__SWP_OFFSET_BITS = 50;
ms->__SWP_OFFSET_MASK = ((1UL << ms->__SWP_OFFSET_BITS) - 1);
ms->PTE_PROT_NONE = (1UL << 58);
ms->PTE_FILE = 0; /* unused */
} else if (THIS_KERNEL_VERSION >= LINUX(4,0,0)) {
ms->__SWP_TYPE_BITS = 6;
ms->__SWP_TYPE_SHIFT = 2;
ms->__SWP_TYPE_MASK = ((1UL << ms->__SWP_TYPE_BITS) - 1);
ms->__SWP_OFFSET_SHIFT = (ms->__SWP_TYPE_BITS + ms->__SWP_TYPE_SHIFT);
ms->__SWP_OFFSET_BITS = 50;
ms->__SWP_OFFSET_MASK = ((1UL << ms->__SWP_OFFSET_BITS) - 1);
ms->PTE_PROT_NONE = (1UL << 58);
ms->PTE_FILE = 0; /* unused */
} else if (THIS_KERNEL_VERSION >= LINUX(3,13,0)) {
ms->__SWP_TYPE_BITS = 6;
ms->__SWP_TYPE_SHIFT = 3;
ms->__SWP_TYPE_MASK = ((1UL << ms->__SWP_TYPE_BITS) - 1);
ms->__SWP_OFFSET_SHIFT = (ms->__SWP_TYPE_BITS + ms->__SWP_TYPE_SHIFT);
ms->__SWP_OFFSET_BITS = 49;
ms->__SWP_OFFSET_MASK = ((1UL << ms->__SWP_OFFSET_BITS) - 1);
ms->PTE_PROT_NONE = (1UL << 58);
ms->PTE_FILE = (1UL << 2);
} else if (THIS_KERNEL_VERSION >= LINUX(3,11,0)) {
ms->__SWP_TYPE_BITS = 6;
ms->__SWP_TYPE_SHIFT = 4;
ms->__SWP_TYPE_MASK = ((1UL << ms->__SWP_TYPE_BITS) - 1);
ms->__SWP_OFFSET_SHIFT = (ms->__SWP_TYPE_BITS + ms->__SWP_TYPE_SHIFT);
ms->__SWP_OFFSET_BITS = 0; /* unused */
ms->__SWP_OFFSET_MASK = 0; /* unused */
ms->PTE_PROT_NONE = (1UL << 2);
ms->PTE_FILE = (1UL << 3);
} else {
ms->__SWP_TYPE_BITS = 6;
ms->__SWP_TYPE_SHIFT = 3;
ms->__SWP_TYPE_MASK = ((1UL << ms->__SWP_TYPE_BITS) - 1);
ms->__SWP_OFFSET_SHIFT = (ms->__SWP_TYPE_BITS + ms->__SWP_TYPE_SHIFT);
ms->__SWP_OFFSET_BITS = 0; /* unused */
ms->__SWP_OFFSET_MASK = 0; /* unused */
ms->PTE_PROT_NONE = (1UL << 1);
ms->PTE_FILE = (1UL << 2);
}
if (symbol_exists("irq_desc"))
ARRAY_LENGTH_INIT(machdep->nr_irqs, irq_desc,
"irq_desc", NULL, 0);
else if (kernel_symbol_exists("nr_irqs"))
get_symbol_data("nr_irqs", sizeof(unsigned int),
&machdep->nr_irqs);
if (!machdep->hz)
machdep->hz = 100;
arm64_irq_stack_init();
arm64_overflow_stack_init();
arm64_stackframe_init();
break;
case POST_INIT:
/*
* crash_notes contains machine specific information about the
* crash. In particular, it contains CPU registers at the time
* of the crash. We need this information to extract correct
* backtraces from the panic task.
*/
if (!LIVE())
arm64_get_crash_notes();
break;
case LOG_ONLY:
machdep->machspec = &arm64_machine_specific;
arm64_calc_VA_BITS();
arm64_calc_KERNELPACMASK();
arm64_calc_phys_offset();
machdep->machspec->page_offset = ARM64_PAGE_OFFSET;
arm64_calc_physvirt_offset();
break;
}
}
struct kernel_va_range_handler {
unsigned long kernel_versions_start; /* include */
unsigned long kernel_versions_end; /* exclude */
struct kernel_range *(*get_range)(struct machine_specific *);
};
static struct kernel_range tmp_range;
#define _PAGE_END(va) (-(1UL << ((va) - 1)))
#define SZ_64K 0x00010000
#define SZ_2M 0x00200000
/*
* Get the max shift of the size of struct page.
* Most of the time, it is 64 bytes, but not sure.
*/
static int arm64_get_struct_page_max_shift(struct machine_specific *ms)
{
return (int)ceil(log2(ms->struct_page_size));
}
/* Return TRUE if we succeed, return FALSE on failure. */
static int arm64_get_vmcoreinfo_ul(unsigned long *vaddr, const char* label)
{
char *string = pc->read_vmcoreinfo(label);
if (!string)
return FALSE;
*vaddr = strtoul(string, NULL, 0);
free(string);
return TRUE;
}
/*
* The change is caused by the kernel patch since v5.18-rc1:
* "arm64: crash_core: Export MODULES, VMALLOC, and VMEMMAP ranges"
*/
static struct kernel_range *arm64_get_range_v5_18(struct machine_specific *ms)
{
struct kernel_range *r = &tmp_range;
/* Get the MODULES_VADDR ~ MODULES_END */
if (!arm64_get_vmcoreinfo_ul(&r->modules_vaddr, "NUMBER(MODULES_VADDR)"))
return NULL;
if (!arm64_get_vmcoreinfo_ul(&r->modules_end, "NUMBER(MODULES_END)"))
return NULL;
/* Get the VMEMMAP_START ~ VMEMMAP_END */
if (!arm64_get_vmcoreinfo_ul(&r->vmemmap_vaddr, "NUMBER(VMEMMAP_START)"))
return NULL;
if (!arm64_get_vmcoreinfo_ul(&r->vmemmap_end, "NUMBER(VMEMMAP_END)"))
return NULL;
/* Get the VMALLOC_START ~ VMALLOC_END */
if (!arm64_get_vmcoreinfo_ul(&r->vmalloc_start_addr, "NUMBER(VMALLOC_START)"))
return NULL;
if (!arm64_get_vmcoreinfo_ul(&r->vmalloc_end, "NUMBER(VMALLOC_END)"))
return NULL;
return r;
}
/*
* The change is caused by the kernel patch since v5.17-rc1:
* "b89ddf4cca43 arm64/bpf: Remove 128MB limit for BPF JIT programs"
*/
static struct kernel_range *arm64_get_range_v5_17(struct machine_specific *ms)
{
struct kernel_range *r = &tmp_range;
unsigned long v = ms->CONFIG_ARM64_VA_BITS;
unsigned long vmem_shift, vmemmap_size;
/* Not initialized yet */
if (v == 0)
return NULL;
if (v > 48)
v = 48;
/* Get the MODULES_VADDR ~ MODULES_END */
r->modules_vaddr = _PAGE_END(v);
r->modules_end = r->modules_vaddr + MEGABYTES(128);
/* Get the VMEMMAP_START ~ VMEMMAP_END */
vmem_shift = machdep->pageshift - arm64_get_struct_page_max_shift(ms);
vmemmap_size = (_PAGE_END(v) - PAGE_OFFSET) >> vmem_shift;
r->vmemmap_vaddr = (-(1UL << (ms->CONFIG_ARM64_VA_BITS - vmem_shift)));
r->vmemmap_end = r->vmemmap_vaddr + vmemmap_size;
/* Get the VMALLOC_START ~ VMALLOC_END */
r->vmalloc_start_addr = r->modules_end;
r->vmalloc_end = r->vmemmap_vaddr - MEGABYTES(256);
return r;
}
/*
* The change is caused by the kernel patch since v5.11:
* "9ad7c6d5e75b arm64: mm: tidy up top of kernel VA space"
*/
static struct kernel_range *arm64_get_range_v5_11(struct machine_specific *ms)
{
struct kernel_range *r = &tmp_range;
unsigned long v = ms->CONFIG_ARM64_VA_BITS;
unsigned long vmem_shift, vmemmap_size, bpf_jit_size = MEGABYTES(128);
/* Not initialized yet */
if (v == 0)
return NULL;
if (v > 48)
v = 48;
/* Get the MODULES_VADDR ~ MODULES_END */
r->modules_vaddr = _PAGE_END(v) + bpf_jit_size;
r->modules_end = r->modules_vaddr + MEGABYTES(128);
/* Get the VMEMMAP_START ~ VMEMMAP_END */
vmem_shift = machdep->pageshift - arm64_get_struct_page_max_shift(ms);
vmemmap_size = (_PAGE_END(v) - PAGE_OFFSET) >> vmem_shift;
r->vmemmap_vaddr = (-(1UL << (ms->CONFIG_ARM64_VA_BITS - vmem_shift)));
r->vmemmap_end = r->vmemmap_vaddr + vmemmap_size;
/* Get the VMALLOC_START ~ VMALLOC_END */
r->vmalloc_start_addr = r->modules_end;
r->vmalloc_end = r->vmemmap_vaddr - MEGABYTES(256);
return r;
}
static unsigned long arm64_get_pud_size(void)
{
unsigned long PUD_SIZE = 0;
switch (machdep->pagesize) {
case 4096:
if (machdep->machspec->VA_BITS > PGDIR_SHIFT_L4_4K) {
PUD_SIZE = PUD_SIZE_L4_4K;
} else {
PUD_SIZE = PGDIR_SIZE_L3_4K;
}
break;
case 65536:
PUD_SIZE = PGDIR_SIZE_L2_64K;
default:
break;
}
return PUD_SIZE;
}
/*
* The change is caused by the kernel patches since v5.4, such as:
* "ce3aaed87344 arm64: mm: Modify calculation of VMEMMAP_SIZE"
* "14c127c957c1 arm64: mm: Flip kernel VA space"
*/
static struct kernel_range *arm64_get_range_v5_4(struct machine_specific *ms)
{
struct kernel_range *r = &tmp_range;
unsigned long v = ms->CONFIG_ARM64_VA_BITS;
unsigned long kasan_shadow_shift, kasan_shadow_offset, PUD_SIZE;
unsigned long vmem_shift, vmemmap_size, bpf_jit_size = MEGABYTES(128);
char *string;
int ret;
/* Not initialized yet */
if (v == 0)
return NULL;
if (v > 48)
v = 48;
/* Get the MODULES_VADDR ~ MODULES_END */
if (kernel_symbol_exists("kasan_init")) {
/* See the arch/arm64/Makefile */
ret = get_kernel_config("CONFIG_KASAN_SW_TAGS", NULL);
if (ret == IKCONFIG_N)
return NULL;
kasan_shadow_shift = (ret == IKCONFIG_Y) ? 4: 3;
/* See the arch/arm64/Kconfig*/
ret = get_kernel_config("CONFIG_KASAN_SHADOW_OFFSET", &string);
if (ret != IKCONFIG_STR)
return NULL;
kasan_shadow_offset = atol(string);
r->modules_vaddr = (1UL << (64 - kasan_shadow_shift)) + kasan_shadow_offset
+ bpf_jit_size;
} else {
r->modules_vaddr = _PAGE_END(v) + bpf_jit_size;
}
r->modules_end = r->modules_vaddr + MEGABYTES(128);
/* Get the VMEMMAP_START ~ VMEMMAP_END */
vmem_shift = machdep->pageshift - arm64_get_struct_page_max_shift(ms);
vmemmap_size = (_PAGE_END(v) - PAGE_OFFSET) >> vmem_shift;
r->vmemmap_vaddr = (-vmemmap_size - SZ_2M);
/*
* In the v5.7, the patch: "bbd6ec605c arm64/mm: Enable memory hot remove"
* adds the VMEMMAP_END.
*
* But before the VMEMMAP_END was added to kernel, we can also see
* the following in arch/arm64/mm/dump.c:
* { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" },
*/
r->vmemmap_end = r->vmemmap_vaddr + vmemmap_size;
/* Get the VMALLOC_START ~ VMALLOC_END */
PUD_SIZE = arm64_get_pud_size();
r->vmalloc_start_addr = r->modules_end;
r->vmalloc_end = (-PUD_SIZE - vmemmap_size - SZ_64K);
return r;
}
/*
* The change is caused by the kernel patches since v5.0, such as:
* "91fc957c9b1d arm64/bpf: don't allocate BPF JIT programs in module memory"
*/
static struct kernel_range *arm64_get_range_v5_0(struct machine_specific *ms)
{
struct kernel_range *r = &tmp_range;
unsigned long v = ms->CONFIG_ARM64_VA_BITS;
unsigned long kasan_shadow_shift, PUD_SIZE;
unsigned long vmemmap_size, bpf_jit_size = MEGABYTES(128);
unsigned long va_start, page_offset;
int ret;
/* Not initialized yet */
if (v == 0)
return NULL;
va_start = (0xffffffffffffffffUL - (1UL << v) + 1);
page_offset = (0xffffffffffffffffUL - (1UL << (v - 1)) + 1);
/* Get the MODULES_VADDR ~ MODULES_END */
if (kernel_symbol_exists("kasan_init")) {
/* See the arch/arm64/Makefile */
ret = get_kernel_config("CONFIG_KASAN_SW_TAGS", NULL);
if (ret == IKCONFIG_N)
return NULL;
kasan_shadow_shift = (ret == IKCONFIG_Y) ? 4: 3;
r->modules_vaddr = va_start + (1UL << (v - kasan_shadow_shift)) + bpf_jit_size;
} else {
r->modules_vaddr = va_start + bpf_jit_size;
}
r->modules_end = r->modules_vaddr + MEGABYTES(128);
/* Get the VMEMMAP_START ~ VMEMMAP_END */
vmemmap_size = (1UL << (v - machdep->pageshift - 1 + arm64_get_struct_page_max_shift(ms)));
r->vmemmap_vaddr = page_offset - vmemmap_size;
r->vmemmap_end = r->vmemmap_vaddr + vmemmap_size; /* See the arch/arm64/mm/dump.c */
/* Get the VMALLOC_START ~ VMALLOC_END */
PUD_SIZE = arm64_get_pud_size();
r->vmalloc_start_addr = r->modules_end;
r->vmalloc_end = page_offset - PUD_SIZE - vmemmap_size - SZ_64K;
return r;
}
static struct kernel_va_range_handler kernel_va_range_handlers[] = {
{
LINUX(5,18,0),
LINUX(999,0,0), /* Just a boundary */
get_range: arm64_get_range_v5_18,
}, {
LINUX(5,17,0), LINUX(5,18,0),
get_range: arm64_get_range_v5_17,
}, {
LINUX(5,11,0), LINUX(5,17,0),
get_range: arm64_get_range_v5_11,
}, {
LINUX(5,4,0), LINUX(5,11,0),
get_range: arm64_get_range_v5_4,
}, {
LINUX(5,0,0), LINUX(5,4,0),
get_range: arm64_get_range_v5_0,
},
};
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
static unsigned long arm64_get_kernel_version(void)
{
char *string;
if (THIS_KERNEL_VERSION)
return THIS_KERNEL_VERSION;
if ((string = pc->read_vmcoreinfo("OSRELEASE"))) {
parse_kernel_version(string);
free(string);
}
return THIS_KERNEL_VERSION;
}
/* Return NULL if we fail. */
static struct kernel_range *arm64_get_va_range(struct machine_specific *ms)
{
struct kernel_va_range_handler *h;
unsigned long kernel_version = arm64_get_kernel_version();
struct kernel_range *r = NULL;
int i;
if (!kernel_version)
goto range_failed;
for (i = 0; i < ARRAY_SIZE(kernel_va_range_handlers); i++) {
h = kernel_va_range_handlers + i;
/* Get the right hook for this kernel version */
if (h->kernel_versions_start <= kernel_version &&
kernel_version < h->kernel_versions_end) {
/* Get the correct virtual address ranges */
r = h->get_range(ms);
if (!r)
goto range_failed;
return r;
}
}
range_failed:
/* Reset ms->struct_page_size to 0 for arm64_calc_virtual_memory_ranges() */
ms->struct_page_size = 0;
return NULL;
}
/* Get the size of struct page {} */
static void arm64_get_struct_page_size(struct machine_specific *ms)
{
char *string;
string = pc->read_vmcoreinfo("SIZE(page)");
if (string)
ms->struct_page_size = atol(string);
free(string);
}
/*
* Accept or reject a symbol from the kernel namelist.
*/
static int
arm64_verify_symbol(const char *name, ulong value, char type)
{
if (!name || !strlen(name))
return FALSE;
if ((type == 'A') && STREQ(name, "_kernel_flags_le"))
machdep->machspec->kernel_flags = le64toh(value);
if ((type == 'A') && STREQ(name, "_kernel_flags_le_hi32"))
machdep->machspec->kernel_flags |= ((ulong)le32toh(value) << 32);
if ((type == 'A') && STREQ(name, "_kernel_flags_le_lo32"))
machdep->machspec->kernel_flags |= le32toh(value);
if (((type == 'A') || (type == 'a')) && (highest_bit_long(value) != 63))
return FALSE;
if ((value == 0) &&
((type == 'a') || (type == 'n') || (type == 'N') || (type == 'U')))
return FALSE;
if (STREQ(name, "$d") || STRNEQ(name, "$d.") ||
STREQ(name, "$x") || STRNEQ(name, "$x.") ||
STREQ(name, "$c") || STRNEQ(name, "$c."))
return FALSE;
if ((type == 'A') && STRNEQ(name, "__crc_"))
return FALSE;
if ((type == 'N') && strstr(name, "$d"))
return FALSE;
if (!(machdep->flags & KSYMS_START) && STREQ(name, "idmap_pg_dir"))
machdep->flags |= KSYMS_START;
return TRUE;
}
void
arm64_dump_machdep_table(ulong arg)
{
const struct machine_specific *ms = machdep->machspec;
int others, i;
others = 0;
fprintf(fp, " flags: %lx (", machdep->flags);
if (machdep->flags & KSYMS_START)
fprintf(fp, "%sKSYMS_START", others++ ? "|" : "");
if (machdep->flags & PHYS_OFFSET)
fprintf(fp, "%sPHYS_OFFSET", others++ ? "|" : "");
if (machdep->flags & VM_L2_64K)
fprintf(fp, "%sVM_L2_64K", others++ ? "|" : "");
if (machdep->flags & VM_L3_64K)
fprintf(fp, "%sVM_L3_64K", others++ ? "|" : "");
if (machdep->flags & VM_L3_4K)
fprintf(fp, "%sVM_L3_4K", others++ ? "|" : "");
if (machdep->flags & VM_L4_4K)
fprintf(fp, "%sVM_L4_4K", others++ ? "|" : "");
if (machdep->flags & VMEMMAP)
fprintf(fp, "%sVMEMMAP", others++ ? "|" : "");
if (machdep->flags & KDUMP_ENABLED)
fprintf(fp, "%sKDUMP_ENABLED", others++ ? "|" : "");
if (machdep->flags & IRQ_STACKS)
fprintf(fp, "%sIRQ_STACKS", others++ ? "|" : "");
if (machdep->flags & UNW_4_14)
fprintf(fp, "%sUNW_4_14", others++ ? "|" : "");
if (machdep->flags & MACHDEP_BT_TEXT)
fprintf(fp, "%sMACHDEP_BT_TEXT", others++ ? "|" : "");
if (machdep->flags & NEW_VMEMMAP)
fprintf(fp, "%sNEW_VMEMMAP", others++ ? "|" : "");
if (machdep->flags & FLIPPED_VM)
fprintf(fp, "%sFLIPPED_VM", others++ ? "|" : "");
if (machdep->flags & HAS_PHYSVIRT_OFFSET)
fprintf(fp, "%sHAS_PHYSVIRT_OFFSET", others++ ? "|" : "");
fprintf(fp, ")\n");
fprintf(fp, " kvbase: %lx\n", machdep->kvbase);
fprintf(fp, " identity_map_base: %lx\n", machdep->identity_map_base);
fprintf(fp, " pagesize: %d\n", machdep->pagesize);
fprintf(fp, " pageshift: %d\n", machdep->pageshift);
fprintf(fp, " pagemask: %lx\n", (ulong)machdep->pagemask);
fprintf(fp, " pageoffset: %lx\n", machdep->pageoffset);
fprintf(fp, " stacksize: %ld\n", machdep->stacksize);
fprintf(fp, " hz: %d\n", machdep->hz);
fprintf(fp, " mhz: %ld\n", machdep->mhz);
fprintf(fp, " memsize: %lld (0x%llx)\n",
(ulonglong)machdep->memsize, (ulonglong)machdep->memsize);
fprintf(fp, " bits: %d\n", machdep->bits);
fprintf(fp, " nr_irqs: %d\n", machdep->nr_irqs);
fprintf(fp, " eframe_search: arm64_eframe_search()\n");
fprintf(fp, " back_trace: arm64_back_trace_cmd() (default: %s method)\n",
kt->flags & USE_OPT_BT ? "optional" : "original");
fprintf(fp, " in_alternate_stack: arm64_in_alternate_stack()\n");
fprintf(fp, " processor_speed: arm64_processor_speed()\n");
fprintf(fp, " uvtop: arm64_uvtop()->%s()\n",
machdep->flags & VM_L3_4K ?
"arm64_vtop_3level_4k" :