forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s390x.c
2237 lines (1990 loc) · 60 KB
/
s390x.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
/* s390.c - core analysis suite
*
* Copyright (C) 2001, 2002 Mission Critical Linux, Inc.
* Copyright (C) 2002-2006, 2009-2014 David Anderson
* Copyright (C) 2002-2006, 2009-2014 Red Hat, Inc. All rights reserved.
* Copyright (C) 2005, 2006, 2010-2013 Michael Holzheu, IBM Corporation
*
* 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 S390X
#include <elf.h>
#include "defs.h"
#include "netdump.h"
#define S390X_WORD_SIZE 8
#define S390X_PAGE_BASE_MASK (~((1ULL<<12)-1))
/* Flags used in entries of page dirs and page tables.
*/
#define S390X_PTE_FLAG_BITS 0xfffULL /* Page table entry flag bits */
#define S390X_PAGE_PRESENT 0x001ULL /* set: loaded in physical memory
* clear: not loaded in physical mem */
#define S390X_PAGE_RO 0x200ULL /* HW read-only */
#define S390X_PAGE_INVALID 0x400ULL /* HW invalid */
#define S390X_PAGE_INVALID_MASK 0x601ULL /* for linux 2.6 */
#define S390X_PAGE_INVALID_NONE 0x401ULL /* for linux 2.6 */
/* bits 52, 55 must contain zeroes in a pte */
#define S390X_PTE_INVALID_MASK 0x900ULL
#define S390X_PTE_INVALID(x) ((x) & S390X_PTE_INVALID_MASK)
#define INT_STACK_SIZE STACKSIZE() // can be 8192 or 16384
#define KERNEL_STACK_SIZE STACKSIZE() // can be 8192 or 16384
#define LOWCORE_SIZE 8192
#define VX_SA_SIZE (32 * 16)
#define S390X_PSW_MASK_PSTATE 0x0001000000000000UL
#define S390X_LC_VMCORE_INFO 0xe0c
#define S390X_LC_OS_INFO 0xe18
/*
* Flags for Region and Segment table entries.
*/
#define S390X_RTE_FLAG_BITS_FC0 0xfffULL
#define S390X_RTE_FLAG_BITS_FC1 0x7fffffffULL
#define S390X_RTE_TL 0x3ULL
#define S390X_RTE_TL_10 0x2ULL
#define S390X_RTE_TL_01 0x1ULL
#define S390X_RTE_TT 0xcULL
#define S390X_RTE_TT_10 0x8ULL
#define S390X_RTE_TT_01 0x4ULL
#define S390X_RTE_CR 0x10ULL
#define S390X_RTE_I 0x20ULL
#define S390X_RTE_TF 0xc0ULL
#define S390X_RTE_TF_10 0x80ULL
#define S390X_RTE_TF_01 0x40ULL
#define S390X_RTE_P 0x200ULL
#define S390X_RTE_FC 0x400ULL
#define S390X_RTE_F 0x800ULL
#define S390X_RTE_ACC 0xf000ULL
#define S390X_RTE_ACC_1000 0x8000ULL
#define S390X_RTE_ACC_0100 0x4000ULL
#define S390X_RTE_ACC_0010 0x2000ULL
#define S390X_RTE_ACC_0001 0x1000ULL
#define S390X_RTE_AV 0x10000ULL
#define S390X_STE_FLAG_BITS_FC0 0x7ffULL
#define S390X_STE_FLAG_BITS_FC1 0xfffffULL
#define S390X_STE_TT 0xcULL
#define S390X_STE_TT_10 0x8ULL
#define S390X_STE_TT_01 0x4ULL
#define S390X_STE_CS 0x10ULL
#define S390X_STE_I 0x20ULL
#define S390X_STE_P 0x200ULL
#define S390X_STE_FC 0x400ULL
#define S390X_STE_F 0x800ULL
#define S390X_STE_ACC 0xf000ULL
#define S390X_STE_ACC_1000 0x8000ULL
#define S390X_STE_ACC_0100 0x4000ULL
#define S390X_STE_ACC_0010 0x2000ULL
#define S390X_STE_ACC_0001 0x1000ULL
#define S390X_STE_AV 0x10000ULL
/*
* S390x prstatus ELF Note
*/
struct s390x_nt_prstatus {
uint8_t pad1[32];
uint32_t pr_pid;
uint8_t pad2[76];
uint64_t psw[2];
uint64_t gprs[16];
uint32_t acrs[16];
uint64_t orig_gpr2;
uint32_t pr_fpvalid;
uint8_t pad3[4];
} __attribute__ ((packed));
/*
* S390x floating point register ELF Note
*/
#ifndef NT_FPREGSET
#define NT_FPREGSET 0x2
#endif
struct s390x_nt_fpregset {
uint32_t fpc;
uint32_t pad;
uint64_t fprs[16];
} __attribute__ ((packed));
struct s390x_vxrs {
uint64_t low;
uint64_t high;
} __attribute__ ((packed));
/*
* s390x CPU info
*/
struct s390x_cpu
{
uint64_t gprs[16];
uint64_t ctrs[16];
uint32_t acrs[16];
uint64_t fprs[16];
uint32_t fpc;
uint64_t psw[2];
uint32_t prefix;
uint64_t timer;
uint64_t todcmp;
uint32_t todpreg;
uint64_t vxrs_low[16];
struct s390x_vxrs vxrs_high[16];
};
/*
* declarations of static functions
*/
static void s390x_print_lowcore(char*, struct bt_info*,int);
static int s390x_kvtop(struct task_context *, ulong, physaddr_t *, int);
static int s390x_uvtop(struct task_context *, ulong, physaddr_t *, int);
static int s390x_vtop(unsigned long, ulong, physaddr_t*, int);
static ulong s390x_vmalloc_start(void);
static int s390x_is_task_addr(ulong);
static int s390x_verify_symbol(const char *, ulong, char type);
static ulong s390x_get_task_pgd(ulong);
static int s390x_translate_pte(ulong, void *, ulonglong);
static ulong s390x_processor_speed(void);
static int s390x_eframe_search(struct bt_info *);
static void s390x_back_trace_cmd(struct bt_info *);
static void s390x_get_stack_frame(struct bt_info *, ulong *, ulong *);
static int s390x_dis_filter(ulong, char *, unsigned int);
static void s390x_cmd_mach(void);
static int s390x_get_smp_cpus(void);
static void s390x_display_machine_stats(void);
static void s390x_dump_line_number(ulong);
static struct line_number_hook s390x_line_number_hooks[];
static int s390x_is_uvaddr(ulong, struct task_context *);
static int s390x_get_kvaddr_ranges(struct vaddr_range *);
static int set_s390x_max_physmem_bits(void);
static ulong s390x_generic_VTOP(ulong vaddr);
static ulong s390x_generic_PTOV(ulong paddr);
static int s390x_generic_IS_VMALLOC_ADDR(ulong vaddr);
static ulong s390x_vr_VTOP(ulong vaddr);
static ulong s390x_vr_PTOV(ulong paddr);
static int s390x_vr_IS_VMALLOC_ADDR(ulong vaddr);
static int s390x_vr_is_kvaddr(ulong);
struct machine_specific s390x_machine_specific = {
.virt_to_phys = s390x_generic_VTOP,
.phys_to_virt = s390x_generic_PTOV,
.is_vmalloc_addr = s390x_generic_IS_VMALLOC_ADDR,
};
/*
* struct lowcore name (old: "_lowcore", new: "lowcore")
*/
static char *lc_struct;
/*
* Read a unsigned long value from address
*/
static unsigned long readmem_ul(unsigned long addr)
{
unsigned long rc;
readmem(addr, KVADDR, &rc, sizeof(rc), "readmem_ul", FAULT_ON_ERROR);
return rc;
}
/*
* Print hex data
*/
static void print_hex_buf(void *buf, int len, int cols, char *tag)
{
int j, first = 1;
for (j = 0; j < len; j += 8) {
if (j % (cols * 8) == 0) {
if (first)
first = 0;
else
fprintf(fp, "\n");
fprintf(fp, "%s", tag);
}
fprintf(fp, "%#018lx ", *((unsigned long *)(buf + j)));
}
if (len)
fprintf(fp, "\n");
}
/*
* Initialize member offsets
*/
static void s390x_offsets_init(void)
{
if (STRUCT_EXISTS("lowcore"))
lc_struct = "lowcore";
else
lc_struct = "_lowcore";
if (MEMBER_EXISTS(lc_struct, "st_status_fixed_logout"))
MEMBER_OFFSET_INIT(s390_lowcore_psw_save_area, lc_struct,
"st_status_fixed_logout");
else
MEMBER_OFFSET_INIT(s390_lowcore_psw_save_area, lc_struct,
"psw_save_area");
if (!STRUCT_EXISTS("stack_frame")) {
ASSIGN_OFFSET(s390_stack_frame_back_chain) = 0;
ASSIGN_OFFSET(s390_stack_frame_r14) = 112;
ASSIGN_SIZE(s390_stack_frame) = 160;
} else {
ASSIGN_OFFSET(s390_stack_frame_back_chain) =
MEMBER_OFFSET("stack_frame", "back_chain");
ASSIGN_OFFSET(s390_stack_frame_r14) =
MEMBER_OFFSET("stack_frame", "gprs") + 8 * 8;
ASSIGN_SIZE(s390_stack_frame) = STRUCT_SIZE("stack_frame");
}
}
/*
* MAX_PHYSMEM_BITS is 42 on older kernels, and 46 on newer kernels.
*/
static int
set_s390x_max_physmem_bits(void)
{
int array_len, dimension;
char *string;
if ((string = pc->read_vmcoreinfo("NUMBER(MAX_PHYSMEM_BITS)"))) {
machdep->max_physmem_bits = atol(string);
free(string);
return TRUE;
}
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_OLD;
if (!kernel_symbol_exists("mem_section"))
return TRUE;
/*
* The mem_section was changed to be a pointer in 4.15, so it's
* guaranteed to be a newer kernel.
*/
if (get_symbol_type("mem_section", NULL, NULL) == TYPE_CODE_PTR) {
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_NEW;
return TRUE;
}
if (!(array_len = get_array_length("mem_section", &dimension, 0)))
return FALSE;
/*
* !CONFIG_SPARSEMEM_EXTREME
*/
if (dimension) {
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_OLD;
if (array_len == (NR_MEM_SECTIONS() / _SECTIONS_PER_ROOT()))
return TRUE;
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_NEW;
if (array_len == (NR_MEM_SECTIONS() / _SECTIONS_PER_ROOT()))
return TRUE;
return FALSE;
}
/*
* CONFIG_SPARSEMEM_EXTREME
*/
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_OLD;
if (array_len == (NR_MEM_SECTIONS() / _SECTIONS_PER_ROOT_EXTREME()))
return TRUE;
machdep->max_physmem_bits = _MAX_PHYSMEM_BITS_NEW;
if (array_len == (NR_MEM_SECTIONS() / _SECTIONS_PER_ROOT_EXTREME()))
return TRUE;
return FALSE;
}
static struct s390x_cpu *s390x_cpu_vec;
static int s390x_cpu_cnt;
/*
* Return s390x CPU data for backtrace
*/
static struct s390x_cpu *s390x_cpu_get(struct bt_info *bt)
{
unsigned int cpu = bt->tc->processor;
unsigned long lowcore_ptr, prefix;
unsigned int i;
lowcore_ptr = symbol_value("lowcore_ptr");
readmem(lowcore_ptr + cpu * sizeof(long), KVADDR,
&prefix, sizeof(long), "lowcore_ptr", FAULT_ON_ERROR);
for (i = 0; i < s390x_cpu_cnt; i++) {
if (s390x_cpu_vec[i].prefix == VTOP(prefix))
return &s390x_cpu_vec[i];
}
error(FATAL, "cannot determine CPU for task: %lx\n", bt->task);
return NULL;
}
/*
* ELF core dump fuctions for storing CPU data
*/
static void s390x_elf_nt_prstatus_add(struct s390x_cpu *cpu,
struct s390x_nt_prstatus *prstatus)
{
memcpy(&cpu->psw, &prstatus->psw, sizeof(cpu->psw));
memcpy(&cpu->gprs, &prstatus->gprs, sizeof(cpu->gprs));
memcpy(&cpu->acrs, &prstatus->acrs, sizeof(cpu->acrs));
}
static void s390x_elf_nt_fpregset_add(struct s390x_cpu *cpu,
struct s390x_nt_fpregset *fpregset)
{
memcpy(&cpu->fpc, &fpregset->fpc, sizeof(cpu->fpc));
memcpy(&cpu->fprs, &fpregset->fprs, sizeof(cpu->fprs));
}
static void s390x_elf_nt_timer_add(struct s390x_cpu *cpu, void *desc)
{
memcpy(&cpu->timer, desc, sizeof(cpu->timer));
}
static void s390x_elf_nt_todcmp_add(struct s390x_cpu *cpu, void *desc)
{
memcpy(&cpu->todcmp, desc, sizeof(cpu->todcmp));
}
static void s390x_elf_nt_todpreg_add(struct s390x_cpu *cpu, void *desc)
{
memcpy(&cpu->todpreg, desc, sizeof(cpu->todpreg));
}
static void s390x_elf_nt_ctrs_add(struct s390x_cpu *cpu, void *desc)
{
memcpy(&cpu->ctrs, desc, sizeof(cpu->ctrs));
}
static void s390x_elf_nt_prefix_add(struct s390x_cpu *cpu, void *desc)
{
memcpy(&cpu->prefix, desc, sizeof(cpu->prefix));
}
static void s390x_elf_nt_vxrs_low_add(struct s390x_cpu *cpu, void *desc)
{
memcpy(&cpu->vxrs_low, desc, sizeof(cpu->vxrs_low));
}
static void s390x_elf_nt_vxrs_high_add(struct s390x_cpu *cpu, void *desc)
{
memcpy(&cpu->vxrs_high, desc, sizeof(cpu->vxrs_high));
}
static void *get_elf_note_desc(Elf64_Nhdr *note)
{
void *ptr = note;
return ptr + roundup(sizeof(*note) + note->n_namesz, 4);
}
static void s390x_elf_note_add(int elf_cpu_nr, void *note_ptr)
{
Elf64_Nhdr *note = note_ptr;
struct s390x_cpu *cpu;
void *desc;
desc = get_elf_note_desc(note);
if (elf_cpu_nr != s390x_cpu_cnt) {
s390x_cpu_cnt++;
s390x_cpu_vec = realloc(s390x_cpu_vec,
s390x_cpu_cnt * sizeof(*s390x_cpu_vec));
if (!s390x_cpu_vec)
error(FATAL, "cannot malloc cpu space.");
}
cpu = &s390x_cpu_vec[s390x_cpu_cnt - 1];
switch (note->n_type) {
case NT_PRSTATUS:
s390x_elf_nt_prstatus_add(cpu, desc);
break;
case NT_FPREGSET:
s390x_elf_nt_fpregset_add(cpu, desc);
break;
case NT_S390_TIMER:
s390x_elf_nt_timer_add(cpu, desc);
break;
case NT_S390_TODCMP:
s390x_elf_nt_todcmp_add(cpu, desc);
break;
case NT_S390_TODPREG:
s390x_elf_nt_todpreg_add(cpu, desc);
break;
case NT_S390_CTRS:
s390x_elf_nt_ctrs_add(cpu, desc);
break;
case NT_S390_PREFIX:
s390x_elf_nt_prefix_add(cpu, desc);
break;
case NT_S390_VXRS_LOW:
s390x_elf_nt_vxrs_low_add(cpu, desc);
break;
case NT_S390_VXRS_HIGH:
s390x_elf_nt_vxrs_high_add(cpu, desc);
break;
}
}
static void s390x_process_elf_notes(void *note_ptr, unsigned long size_note)
{
Elf64_Nhdr *note = NULL;
size_t tot, len;
static int num_prstatus_notes = 0;
for (tot = 0; tot < size_note; tot += len) {
note = note_ptr + tot;
if (note->n_type == NT_PRSTATUS)
num_prstatus_notes++;
machdep->dumpfile_init(num_prstatus_notes, note);
len = sizeof(Elf64_Nhdr);
len = roundup(len + note->n_namesz, 4);
len = roundup(len + note->n_descsz, 4);
}
}
static void s390x_check_live(void)
{
unsigned long long live_magic;
readmem(0, KVADDR, &live_magic, sizeof(live_magic), "live_magic",
RETURN_ON_ERROR | QUIET);
if (live_magic == 0x4c49564544554d50ULL)
pc->flags2 |= LIVE_DUMP;
}
static char *
vmcoreinfo_read_string_s390x(const char *vmcoreinfo, const char *key)
{
char *value_string = NULL;
size_t value_length;
char keybuf[128];
char *p1, *p2;
sprintf(keybuf, "%s=", key);
if ((p1 = strstr(vmcoreinfo, keybuf))) {
p2 = p1 + strlen(keybuf);
p1 = strstr(p2, "\n");
value_length = p1-p2;
value_string = calloc(value_length + 1, sizeof(char));
strncpy(value_string, p2, value_length);
value_string[value_length] = NULLCHAR;
}
return value_string;
}
/*
* Check the value in well-known lowcore location and process it as either
* an explicit KASLR offset (early dump case) or as vmcoreinfo pointer to
* read the relocated _stext symbol value (important for s390 and lkcd dump
* formats).
*/
static void s390x_check_kaslr(void)
{
char *_stext_string, *vmcoreinfo;
Elf64_Nhdr note;
char str[128];
ulong addr;
/* Read the value from well-known lowcore location*/
if (!readmem(S390X_LC_VMCORE_INFO, PHYSADDR, &addr,
sizeof(addr), "s390x vmcoreinfo ptr",
QUIET|RETURN_ON_ERROR))
return;
if (addr == 0)
return;
/* Check for explicit kaslr offset flag */
if (addr & 0x1UL) {
/* Drop the last bit to get an offset value */
addr &= ~(0x1UL);
/* Make sure the offset is aligned by 0x1000 */
if (addr && !(addr & 0xfff)) {
kt->relocate = addr * (-1);
kt->flags |= RELOC_SET;
kt->flags2 |= KASLR;
}
return;
}
/* Use the addr value as vmcoreinfo pointer */
if (!readmem(addr, PHYSADDR, ¬e,
sizeof(note), "Elf64_Nhdr vmcoreinfo",
QUIET|RETURN_ON_ERROR))
return;
memset(str, 0, sizeof(str));
if (!readmem(addr + sizeof(note), PHYSADDR, str,
note.n_namesz, "VMCOREINFO",
QUIET|RETURN_ON_ERROR))
return;
if (memcmp(str, "VMCOREINFO", sizeof("VMCOREINFO")) != 0)
return;
if ((vmcoreinfo = malloc(note.n_descsz + 1)) == NULL) {
error(INFO, "s390x_check_kaslr: cannot malloc vmcoreinfo buffer\n");
return;
}
addr = addr + sizeof(note) + note.n_namesz + 1;
if (!readmem(addr, PHYSADDR, vmcoreinfo,
note.n_descsz, "s390x vmcoreinfo",
QUIET|RETURN_ON_ERROR)) {
free(vmcoreinfo);
return;
}
vmcoreinfo[note.n_descsz] = NULLCHAR;
/*
* Read relocated _stext symbol value and store it in the kernel_table
* for further processing within derive_kaslr_offset().
*/
if ((_stext_string = vmcoreinfo_read_string_s390x(vmcoreinfo,
"SYMBOL(_stext)"))) {
kt->vmcoreinfo._stext_SYMBOL = htol(_stext_string,
RETURN_ON_ERROR, NULL);
free(_stext_string);
}
free(vmcoreinfo);
}
#define OS_INFO_VERSION_MAJOR 1
#define OS_INFO_VERSION_MINOR 1
#define OS_INFO_VMCOREINFO 0
#define OS_INFO_REIPL_BLOCK 1
#define OS_INFO_FLAGS_ENTRY 2
#define OS_INFO_RESERVED 3
#define OS_INFO_IDENTITY_BASE 4
#define OS_INFO_KASLR_OFFSET 5
#define OS_INFO_KASLR_OFF_PHYS 6
#define OS_INFO_VMEMMAP 7
#define OS_INFO_AMODE31_START 8
#define OS_INFO_AMODE31_END 9
struct os_info_entry {
union {
__u64 addr;
__u64 val;
};
__u64 size;
__u32 csum;
} __attribute__((packed));
struct os_info {
__u64 magic;
__u32 csum;
__u16 version_major;
__u16 version_minor;
__u64 crashkernel_addr;
__u64 crashkernel_size;
struct os_info_entry entry[10];
__u8 reserved[3864];
} __attribute__((packed));
struct vm_info {
__u64 __identity_base;
__u64 __kaslr_offset;
__u64 __kaslr_offset_phys;
__u64 amode31_start;
__u64 amode31_end;
};
static bool
vmcoreinfo_read_u64(const char *key, __u64 *val)
{
char *string;
string = pc->read_vmcoreinfo(key);
if (string) {
*val = strtoul(string, NULL, 16);
free(string);
return true;
}
return false;
}
static bool vmcoreinfo_read_vm_info(struct vm_info *_vm_info)
{
struct vm_info vm_info;
if (!vmcoreinfo_read_u64("IDENTITYBASE", &vm_info.__identity_base) ||
!vmcoreinfo_read_u64("KERNELOFFSET", &vm_info.__kaslr_offset) ||
!vmcoreinfo_read_u64("KERNELOFFPHYS", &vm_info.__kaslr_offset_phys) ||
!vmcoreinfo_read_u64("SAMODE31", &vm_info.amode31_start) ||
!vmcoreinfo_read_u64("EAMODE31", &vm_info.amode31_end))
return false;
*_vm_info = vm_info;
return true;
}
static bool os_info_read_vm_info(struct vm_info *vm_info)
{
struct os_info os_info;
ulong addr;
if (!readmem(S390X_LC_OS_INFO, PHYSADDR, &addr,
sizeof(addr), "s390x os_info ptr",
QUIET|RETURN_ON_ERROR))
return false;
if (addr == 0)
return true;
if (!readmem(addr, PHYSADDR, &os_info,
offsetof(struct os_info, reserved), "s390x os_info header",
QUIET|RETURN_ON_ERROR))
return false;
vm_info->__identity_base = os_info.entry[OS_INFO_IDENTITY_BASE].val;
vm_info->__kaslr_offset = os_info.entry[OS_INFO_KASLR_OFFSET].val;
vm_info->__kaslr_offset_phys = os_info.entry[OS_INFO_KASLR_OFF_PHYS].val;
vm_info->amode31_start = os_info.entry[OS_INFO_AMODE31_START].val;
vm_info->amode31_end = os_info.entry[OS_INFO_AMODE31_END].val;
return true;
}
static bool vm_info_empty(struct vm_info *vm_info)
{
return !vm_info->__kaslr_offset;
}
static bool s390x_init_vm(void)
{
struct vm_info vm_info;
if (pc->flags & PROC_KCORE) {
if (!vmcoreinfo_read_vm_info(&vm_info))
return true;
} else {
if (!os_info_read_vm_info(&vm_info))
return false;
}
if (vm_info_empty(&vm_info))
return true;
machdep->identity_map_base = vm_info.__identity_base;
machdep->kvbase = vm_info.__kaslr_offset;
machdep->machspec->__kaslr_offset_phys = vm_info.__kaslr_offset_phys;
machdep->machspec->amode31_start = vm_info.amode31_start;
machdep->machspec->amode31_end = vm_info.amode31_end;
machdep->is_kvaddr = s390x_vr_is_kvaddr;
machdep->machspec->virt_to_phys = s390x_vr_VTOP;
machdep->machspec->phys_to_virt = s390x_vr_PTOV;
machdep->machspec->is_vmalloc_addr = s390x_vr_IS_VMALLOC_ADDR;
return true;
}
static ulong s390x_generic_VTOP(ulong vaddr)
{
return vaddr - machdep->kvbase;
}
static ulong s390x_generic_PTOV(ulong paddr)
{
return paddr + machdep->kvbase;
}
static int s390x_generic_IS_VMALLOC_ADDR(ulong vaddr)
{
return vt->vmalloc_start && vaddr >= vt->vmalloc_start;
}
static ulong s390x_vr_VTOP(ulong vaddr)
{
if (vaddr < LOWCORE_SIZE)
return vaddr;
if ((vaddr < machdep->machspec->amode31_end) &&
(vaddr >= machdep->machspec->amode31_start))
return vaddr;
if (vaddr < machdep->kvbase)
return vaddr - machdep->identity_map_base;
return vaddr - machdep->kvbase + machdep->machspec->__kaslr_offset_phys;
}
static ulong s390x_vr_PTOV(ulong paddr)
{
return paddr + machdep->identity_map_base;
}
static int s390x_vr_IS_VMALLOC_ADDR(ulong vaddr)
{
return (vaddr >= vt->vmalloc_start && vaddr < machdep->kvbase);
}
ulong s390x_VTOP(ulong vaddr)
{
return machdep->machspec->virt_to_phys(vaddr);
}
ulong s390x_PTOV(ulong paddr)
{
return machdep->machspec->phys_to_virt(paddr);
}
int s390x_IS_VMALLOC_ADDR(ulong vaddr)
{
return machdep->machspec->is_vmalloc_addr(vaddr);
}
/*
* Do all necessary machine-specific setup here. This is called several
* times during initialization.
*/
void
s390x_init(int when)
{
switch (when)
{
case SETUP_ENV:
machdep->dumpfile_init = s390x_elf_note_add;
machdep->process_elf_notes = s390x_process_elf_notes;
break;
case PRE_SYMTAB:
machdep->machspec = &s390x_machine_specific;
machdep->verify_symbol = s390x_verify_symbol;
if (pc->flags & KERNEL_DEBUG_QUERY)
return;
machdep->pagesize = memory_page_size();
machdep->pageshift = ffs(machdep->pagesize) - 1;
machdep->pageoffset = machdep->pagesize - 1;
machdep->pagemask = ~((ulonglong)machdep->pageoffset);
// machdep->stacksize = KERNEL_STACK_SIZE;
if ((machdep->pgd = (char *)malloc(SEGMENT_TABLE_SIZE)) == NULL)
error(FATAL, "cannot malloc pgd space.");
machdep->pmd = machdep->pgd;
if ((machdep->ptbl = (char *)malloc(PAGESIZE())) == NULL)
error(FATAL, "cannot malloc ptbl space.");
machdep->last_pgd_read = 0;
machdep->last_pmd_read = 0;
machdep->last_ptbl_read = 0;
machdep->verify_paddr = generic_verify_paddr;
machdep->get_kvaddr_ranges = s390x_get_kvaddr_ranges;
machdep->ptrs_per_pgd = PTRS_PER_PGD;
if (DUMPFILE() && !(kt->flags & RELOC_SET))
s390x_check_kaslr();
break;
case PRE_GDB:
machdep->kvbase = 0;
machdep->identity_map_base = 0;
machdep->is_kvaddr = generic_is_kvaddr;
if (!s390x_init_vm())
error(FATAL, "cannot initialize VM parameters.");
machdep->is_uvaddr = s390x_is_uvaddr;
machdep->eframe_search = s390x_eframe_search;
machdep->back_trace = s390x_back_trace_cmd;
machdep->processor_speed = s390x_processor_speed;
machdep->uvtop = s390x_uvtop;
machdep->kvtop = s390x_kvtop;
machdep->get_task_pgd = s390x_get_task_pgd;
machdep->get_stack_frame = s390x_get_stack_frame;
machdep->get_stackbase = generic_get_stackbase;
machdep->get_stacktop = generic_get_stacktop;
machdep->translate_pte = s390x_translate_pte;
machdep->memory_size = generic_memory_size;
machdep->is_task_addr = s390x_is_task_addr;
machdep->dis_filter = s390x_dis_filter;
machdep->cmd_mach = s390x_cmd_mach;
machdep->get_smp_cpus = s390x_get_smp_cpus;
machdep->line_number_hooks = s390x_line_number_hooks;
machdep->value_to_symbol = generic_machdep_value_to_symbol;
machdep->init_kernel_pgd = NULL;
vt->flags |= COMMON_VADDR;
s390x_check_live();
break;
case POST_GDB:
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);
else
machdep->nr_irqs = 0;
machdep->vmalloc_start = s390x_vmalloc_start;
machdep->dump_irq = generic_dump_irq;
if (!machdep->hz)
machdep->hz = HZ;
machdep->section_size_bits = _SECTION_SIZE_BITS;
if (!set_s390x_max_physmem_bits())
error(WARNING, "cannot determine MAX_PHYSMEM_BITS\n");
s390x_offsets_init();
break;
case POST_INIT:
break;
}
}
/*
* Dump machine dependent information
*/
void
s390x_dump_machdep_table(ulong arg)
{
int others;
others = 0;
fprintf(fp, " flags: %lx (", machdep->flags);
if (machdep->flags & KSYMS_START)
fprintf(fp, "%sKSYMS_START", 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: %llx\n", 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",
(unsigned long long)machdep->memsize,
(unsigned long long)machdep->memsize);
fprintf(fp, " bits: %d\n", machdep->bits);
fprintf(fp, " nr_irqs: %d\n", machdep->nr_irqs);
fprintf(fp, " eframe_search: s390x_eframe_search()\n");
fprintf(fp, " back_trace: s390x_back_trace_cmd()\n");
fprintf(fp, " processor_speed: s390x_processor_speed()\n");
fprintf(fp, " uvtop: s390x_uvtop()\n");
fprintf(fp, " kvtop: s390x_kvtop()\n");
fprintf(fp, " get_task_pgd: s390x_get_task_pgd()\n");
fprintf(fp, " dump_irq: generic_dump_irq()\n");
fprintf(fp, " get_stack_frame: s390x_get_stack_frame()\n");
fprintf(fp, " get_stackbase: generic_get_stackbase()\n");
fprintf(fp, " get_stacktop: generic_get_stacktop()\n");
fprintf(fp, " translate_pte: s390x_translate_pte()\n");
fprintf(fp, " memory_size: generic_memory_size()\n");
fprintf(fp, " vmalloc_start: s390x_vmalloc_start()\n");
fprintf(fp, " is_task_addr: s390x_is_task_addr()\n");
fprintf(fp, " verify_symbol: s390x_verify_symbol()\n");
fprintf(fp, " dis_filter: s390x_dis_filter()\n");
fprintf(fp, " cmd_mach: s390x_cmd_mach()\n");
fprintf(fp, " get_smp_cpus: s390x_get_smp_cpus()\n");
fprintf(fp, " is_kvaddr: %s()\n", machdep->is_kvaddr == s390x_vr_is_kvaddr ?
"s390x_vr_is_kvaddr" :
"generic_is_kvaddr");
fprintf(fp, " is_uvaddr: s390x_is_uvaddr()\n");
fprintf(fp, " verify_paddr: generic_verify_paddr()\n");
fprintf(fp, " get_kvaddr_ranges: s390x_get_kvaddr_ranges()\n");
fprintf(fp, " init_kernel_pgd: NULL\n");
fprintf(fp, " value_to_symbol: generic_machdep_value_to_symbol()\n");
fprintf(fp, " dumpfile_init: s390x_elf_note_add()\n");
fprintf(fp, " process_elf_notes: s390x_process_elf_notes()\n");
fprintf(fp, " line_number_hooks: s390x_line_number_hooks\n");
fprintf(fp, " last_pgd_read: %lx\n", machdep->last_pgd_read);
fprintf(fp, " last_pmd_read: %lx\n", machdep->last_pmd_read);
fprintf(fp, " last_ptbl_read: %lx\n", machdep->last_ptbl_read);
fprintf(fp, " pgd: %lx\n", (ulong)machdep->pgd);
fprintf(fp, " pmd: %lx\n", (ulong)machdep->pmd);
fprintf(fp, " ptbl: %lx\n", (ulong)machdep->ptbl);
fprintf(fp, " ptrs_per_pgd: %d\n", machdep->ptrs_per_pgd);
fprintf(fp, " max_physmem_bits: %ld\n", machdep->max_physmem_bits);
fprintf(fp, " section_size_bits: %ld\n", machdep->section_size_bits);
fprintf(fp, " machspec: %lx\n", (ulong)machdep->machspec);
}
static int
s390x_vr_is_kvaddr(ulong vaddr)
{
return (vaddr < LOWCORE_SIZE) || (vaddr >= machdep->identity_map_base);
}
/*
* Check if address is in context's address space
*/
static int
s390x_is_uvaddr(ulong vaddr, struct task_context *tc)
{
return IN_TASK_VMA(tc->task, vaddr);
}
/*
* Translates a user virtual address to its physical address
*/
static int
s390x_uvtop(struct task_context *tc, ulong vaddr, physaddr_t *paddr, int verbose)
{
unsigned long pgd_base;
readmem(tc->mm_struct + OFFSET(mm_struct_pgd), KVADDR,
&pgd_base,sizeof(long), "pgd_base",FAULT_ON_ERROR);
return s390x_vtop(pgd_base, vaddr, paddr, verbose);
}
/*
* Translates a kernel virtual address to its physical address
*/
static int
s390x_kvtop(struct task_context *tc, ulong vaddr, physaddr_t *paddr, int verbose)
{
unsigned long pgd_base;
if (!IS_KVADDR(vaddr)){
*paddr = 0;
return FALSE;
}
if (!vt->vmalloc_start) {
*paddr = VTOP(vaddr);
return TRUE;
}
if (!IS_VMALLOC_ADDR(vaddr)) {
*paddr = VTOP(vaddr);
return TRUE;
}
pgd_base = (unsigned long)vt->kernel_pgd[0];
return s390x_vtop(pgd_base, vaddr, paddr, verbose);
}
/*
* Check if page is mapped
*/
static inline int s390x_pte_present(unsigned long x){
if(THIS_KERNEL_VERSION >= LINUX(2,6,0)){
return !((x) & S390X_PAGE_INVALID) ||
((x) & S390X_PAGE_INVALID_MASK) == S390X_PAGE_INVALID_NONE;
} else {
return ((x) & S390X_PAGE_PRESENT);
}
}
/*
* page table traversal functions
*/
/* Print flags of Segment-Table entry with format control = 1 */
static void print_segment_entry_fc1(ulong val)
{
fprintf(fp, "AV=%u; ACC=%u%u%u%u; F=%u; FC=%u; P=%u; I=%u; CS=%u; TT=%u%u\n",
!!(val & S390X_STE_AV),
!!(val & S390X_STE_ACC_1000),
!!(val & S390X_STE_ACC_0100),
!!(val & S390X_STE_ACC_0010),
!!(val & S390X_STE_ACC_0001),
!!(val & S390X_STE_F),
!!(val & S390X_STE_FC),
!!(val & S390X_STE_P),
!!(val & S390X_STE_I),
!!(val & S390X_STE_CS),
!!(val & S390X_STE_TT_10),
!!(val & S390X_STE_TT_01));
}
/* Print flags of Segment-Table entry with format control = 0 */
static void print_segment_entry_fc0(ulong val)
{
fprintf(fp, "FC=%u; P=%u; I=%u; CS=%u; TT=%u%u\n",