-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlkmd_support.c
1328 lines (1222 loc) · 32.9 KB
/
lkmd_support.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
/*
* Kernel Debugger Architecture Independent Support Functions
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*
* Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
* 03/02/13 added new 2.5 kallsyms <xavier.bru@bull.net>
*/
#include <stdarg.h>
#include <linux/types.h>
#include <linux/ctype.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/kallsyms.h>
#include <linux/stddef.h>
#include <linux/vmalloc.h>
#include <linux/ptrace.h>
#include <linux/module.h>
#include <linux/highmem.h>
#include <linux/hardirq.h>
#include <linux/delay.h>
#include <linux/version.h>
#include <asm/uaccess.h>
#include "lkmd.h"
#include "lkmd_private.h"
int lkmd_strnicmp(const char *s1, const char *s2, size_t len)
{
/* Yes, Virginia, it had better be unsigned */
unsigned char c1, c2;
c1 = c2 = 0;
if (len) {
do {
c1 = *s1;
c2 = *s2;
s1++;
s2++;
if (!c1)
break;
if (!c2)
break;
if (c1 == c2)
continue;
c1 = tolower(c1);
c2 = tolower(c2);
if (c1 != c2)
break;
} while (--len);
}
return (int)c1 - (int)c2;
}
/*
* Kernel symbol
*/
struct lkmd_kernel_sym {
unsigned long curr_task;
unsigned long irq_enter;
unsigned long irq_exit;
unsigned long kallsyms_lookup;
unsigned long find_extend_vma;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
unsigned long follow_page_mask;
#endif
};
static struct lkmd_kernel_sym kernelsym;
int __init lkmd_kernsym_init(void)
{
if ((kernelsym.curr_task = kallsyms_lookup_name("curr_task")) == 0 ||
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
(kernelsym.follow_page_mask = kallsyms_lookup_name("follow_page_mask")) == 0 ||
#endif
(kernelsym.irq_enter = kallsyms_lookup_name("irq_enter")) == 0 ||
(kernelsym.irq_exit = kallsyms_lookup_name("irq_exit")) == 0 ||
(kernelsym.kallsyms_lookup = kallsyms_lookup_name("kallsyms_lookup")) == 0 ||
(kernelsym.find_extend_vma = kallsyms_lookup_name("find_extend_vma")) == 0)
return -EFAULT;
if ((orig_smp_error_interrupt = (void *)kallsyms_lookup_name("smp_error_interrupt")) == 0 ||
(orig_do_debug = (void *)kallsyms_lookup_name("do_debug")) == 0 ||
(orig_do_int3 = (void *)kallsyms_lookup_name("do_int3")) == 0)
return -EFAULT;
return 0;
}
struct task_struct *lkmd_curr_task(int p)
{
struct task_struct *(*fn)(int) = (void *)kernelsym.curr_task;
return fn(p);
}
void lkmd_irq_enter(void)
{
void (*fn)(void) = (void *)kernelsym.irq_enter;
fn();
}
void lkmd_irq_exit(void)
{
void (*fn)(void) = (void *)kernelsym.irq_exit;
fn();
}
const char *lkmd_kallsyms_lookup(unsigned long addr,
unsigned long *symbolsize,
unsigned long *offset,
char **modname, char *namebuf)
{
const char *(*fn)(unsigned long,
unsigned long *, unsigned long *,
char **, char *) = (void *)kernelsym.kallsyms_lookup;
return fn(addr, symbolsize, offset, modname, namebuf);
}
struct vm_area_struct *lkmd_find_extend_vma(struct mm_struct *mm, unsigned long addr)
{
struct vm_area_struct *(*fn)(struct mm_struct *,
unsigned long) = (void *)kernelsym.find_extend_vma;
return fn(mm, addr);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
struct page *lkmd_follow_page(struct vm_area_struct *vma,
unsigned long address, unsigned int flags)
{
unsigned int unused_page_mask;
struct page *(*fn)(struct vm_area_struct *,unsigned long, unsigned int,
unsigned int *) = (void *)kernelsym.follow_page_mask;
return fn(vma, address, flags, &unused_page_mask);
}
#else
#define lkmd_follow_page(vma, address, flags) follow_page(vma, address, flags)
#endif
int kdb_task_has_cpu(const struct task_struct *p)
{
return (lkmd_curr_task(task_cpu(p)) == p);
}
struct task_struct *lkmd_find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
{
return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
}
// @TODO
// struct kallsym_iter {
// loff_t pos;
// unsigned long value;
// unsigned int nameoff; /* If iterating in core kernel symbols. */
// char name[KSYM_NAME_LEN];
// };
// const char *lkmd_walk_kallsyms(loff_t *pos)
// {
// static struct kallsym_iter kdb_walk_kallsyms_iter;
// if (*pos == 0) {
// memset(&kdb_walk_kallsyms_iter, 0, sizeof(kdb_walk_kallsyms_iter));
// reset_iter(&kdb_walk_kallsyms_iter, 0);
// }
// while (1) {
// if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
// return NULL;
// ++*pos;
// /* Some debugging symbols have no name. Ignore them. */
// if (kdb_walk_kallsyms_iter.name[0])
// return kdb_walk_kallsyms_iter.name;
// }
// }
/*
* Symbol table functions.
*/
/*
* lkmd_get_sym_val
*
* Return the address of the given symbol.
*
* Parameters:
* symname Character string containing symbol name
* symtab Structure to receive results
* Outputs:
* Returns:
* 0 Symbol not found, symtab zero filled
* 1 Symbol mapped to module/symbol/section, data in symtab
* Locking:
* None.
* Remarks:
*/
int lkmd_get_sym_val(const char *symname, kdb_symtab_t *symtab)
{
if (KDB_DEBUG(AR))
lkmd_printf("lkmd_get_sym_val: symname=%s, symtab=%p\n", symname, symtab);
memset(symtab, 0, sizeof(*symtab));
if ((symtab->sym_start = kallsyms_lookup_name(symname))) {
if (KDB_DEBUG(AR))
lkmd_printf("lkmd_get_sym_val: returns 1, symtab->sym_start=0x%lx\n", symtab->sym_start);
return 1;
}
if (KDB_DEBUG(AR))
lkmd_printf("lkmd_get_sym_val: returns 0\n");
return 0;
}
/*
* kdbnearsym
*
* Return the name of the symbol with the nearest address
* less than 'addr'.
*
* Parameters:
* addr Address to check for symbol near
* symtab Structure to receive results
* Outputs:
* Returns:
* 0 No sections contain this address, symtab zero filled
* 1 Address mapped to module/symbol/section, data in symtab
* Locking:
* None.
* Remarks:
* 2.6 kallsyms has a "feature" where it unpacks the name into a string.
* If that string is reused before the caller expects it then the caller
* sees its string change without warning. To avoid cluttering up the
* main kdb code with lots of kdb_strdup, tests and kfree calls, kdbnearsym
* maintains an LRU list of the last few unique strings. The list is sized
* large enough to hold active strings, no kdb caller of kdbnearsym makes
* more than ~20 later calls before using a saved value.
*/
static char *kdb_name_table[100]; /* arbitrary size */
int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
{
int ret = 0;
unsigned long symbolsize;
unsigned long offset;
#define knt1_size 128 /* must be >= kallsyms table size */
char *knt1 = NULL;
if (KDB_DEBUG(AR))
lkmd_printf("kdbnearsym: addr=0x%lx, symtab=%p\n", addr, symtab);
memset(symtab, 0, sizeof(*symtab));
if (addr < 4096)
goto out;
knt1 = debug_kmalloc(knt1_size, GFP_ATOMIC);
if (!knt1) {
lkmd_printf("kdbnearsym: addr=0x%lx cannot kmalloc knt1\n", addr);
goto out;
}
symtab->sym_name = lkmd_kallsyms_lookup(addr, &symbolsize , &offset, (char **)(&symtab->mod_name), knt1);
if (offset > 8*1024*1024) {
symtab->sym_name = NULL;
addr = offset = symbolsize = 0;
}
symtab->sym_start = addr - offset;
symtab->sym_end = symtab->sym_start + symbolsize;
ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';
if (ret) {
int i;
/* Another 2.6 kallsyms "feature". Sometimes the sym_name is
* set but the buffer passed into kallsyms_lookup is not used,
* so it contains garbage. The caller has to work out which
* buffer needs to be saved.
*
* What was Rusty smoking when he wrote that code?
*/
if (symtab->sym_name != knt1) {
strncpy(knt1, symtab->sym_name, knt1_size);
knt1[knt1_size-1] = '\0';
}
for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
if (kdb_name_table[i] && strcmp(kdb_name_table[i], knt1) == 0)
break;
}
if (i >= ARRAY_SIZE(kdb_name_table)) {
debug_kfree(kdb_name_table[0]);
memcpy(kdb_name_table, kdb_name_table+1,
sizeof(kdb_name_table[0])*(ARRAY_SIZE(kdb_name_table)-1));
} else {
debug_kfree(knt1);
knt1 = kdb_name_table[i];
memcpy(kdb_name_table+i, kdb_name_table+i+1,
sizeof(kdb_name_table[0])*(ARRAY_SIZE(kdb_name_table)-i-1));
}
i = ARRAY_SIZE(kdb_name_table) - 1;
kdb_name_table[i] = knt1;
symtab->sym_name = kdb_name_table[i];
knt1 = NULL;
}
if (symtab->mod_name == NULL)
symtab->mod_name = "kernel";
if (KDB_DEBUG(AR))
lkmd_printf("kdbnearsym: returns %d symtab->sym_start=0x%lx, symtab->mod_name=%p, symtab->sym_name=%p (%s)\n", ret, symtab->sym_start, symtab->mod_name, symtab->sym_name, symtab->sym_name);
out:
debug_kfree(knt1);
return ret;
}
void
kdbnearsym_cleanup(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
if (kdb_name_table[i]) {
debug_kfree(kdb_name_table[i]);
kdb_name_table[i] = NULL;
}
}
}
/*
* kallsyms_symbol_complete
*
* Parameters:
* prefix_name prefix of a symbol name to lookup
* max_len maximum length that can be returned
* Returns:
* Number of symbols which match the given prefix.
* Notes:
* prefix_name is changed to contain the longest unique prefix that
* starts with this prefix (tab completion).
*/
// @TODO
// static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];
//
// int kallsyms_symbol_complete(char *prefix_name, int max_len)
// {
// loff_t pos = 0;
// int prefix_len = strlen(prefix_name), prev_len = 0;
// int i, number = 0;
// const char *name;
// while ((name = lkmd_walk_kallsyms(&pos))) {
// if (strncmp(name, prefix_name, prefix_len) == 0) {
// strcpy(ks_namebuf, name);
// /* Work out the longest name that matches the prefix */
// if (++number == 1) {
// prev_len = min_t(int, max_len-1, strlen(ks_namebuf));
// memcpy(ks_namebuf_prev, ks_namebuf, prev_len);
// ks_namebuf_prev[prev_len] = '\0';
// } else for (i = 0; i < prev_len; ++i) {
// if (ks_namebuf[i] != ks_namebuf_prev[i]) {
// prev_len = i;
// ks_namebuf_prev[i] = '\0';
// break;
// }
// }
// }
// }
// if (prev_len > prefix_len)
// memcpy(prefix_name, ks_namebuf_prev, prev_len+1);
// return number;
// }
/*
* kallsyms_symbol_next
*
* Parameters:
* prefix_name prefix of a symbol name to lookup
* flag 0 means search from the head, 1 means continue search.
* Returns:
* 1 if a symbol matches the given prefix.
* 0 if no string found
*/
// @TODO
// int kallsyms_symbol_next(char *prefix_name, int flag)
// {
// int prefix_len = strlen(prefix_name);
// static loff_t pos;
// const char *name;
// if (!flag)
// pos = 0;
// while ((name = lkmd_walk_kallsyms(&pos))) {
// if (strncmp(name, prefix_name, prefix_len) == 0) {
// strncpy(prefix_name, name, strlen(name)+1);
// return 1;
// }
// }
// return 0;
// }
#if defined(CONFIG_SMP)
/*
* kdb_ipi
*
* This function is called from the non-maskable interrupt
* handler to handle a kdb IPI instruction.
*
* Inputs:
* regs = Exception frame pointer
* Outputs:
* None.
* Returns:
* 0 - Did not handle NMI
* 1 - Handled NMI
* Locking:
* None.
* Remarks:
* Initially one processor is invoked in the kdb() code. That
* processor sends an ipi which drives this routine on the other
* processors. All this does is call kdb() with reason SWITCH.
* This puts all processors into the kdb() routine and all the
* code for breakpoints etc. is in one place.
* One problem with the way the kdb NMI is sent, the NMI has no
* identification that says it came from kdb. If the cpu's kdb state is
* marked as "waiting for kdb_ipi" then the NMI is treated as coming from
* kdb, otherwise it is assumed to be for another reason and is ignored.
*/
int kdb_ipi(struct pt_regs *regs, void (*ack_interrupt)(void))
{
/* Do not print before checking and clearing WAIT_IPI, IPIs are
* going all the time.
*/
if (KDB_STATE(WAIT_IPI)) {
/*
* Stopping other processors via smp_kdb_stop().
*/
if (ack_interrupt)
(*ack_interrupt)(); /* Acknowledge the interrupt */
KDB_STATE_CLEAR(WAIT_IPI);
KDB_DEBUG_STATE("kdb_ipi 1", 0);
kdb(KDB_REASON_SWITCH, 0, regs); /* Spin in kdb() */
KDB_DEBUG_STATE("kdb_ipi 2", 0);
return 1;
}
return 0;
}
#endif /* CONFIG_SMP */
/*
* kdb_symbol_print
*
* Standard method for printing a symbol name and offset.
* Inputs:
* addr Address to be printed.
* symtab Address of symbol data, if NULL this routine does its
* own lookup.
* punc Punctuation for string, bit field.
* Outputs:
* None.
* Returns:
* Always 0.
* Locking:
* none.
* Remarks:
* The string and its punctuation is only printed if the address
* is inside the kernel, except that the value is always printed
* when requested.
*/
void kdb_symbol_print(kdb_machreg_t addr, const kdb_symtab_t *symtab_p, unsigned int punc)
{
kdb_symtab_t symtab, *symtab_p2;
if (symtab_p) {
symtab_p2 = (kdb_symtab_t *)symtab_p;
}
else {
symtab_p2 = &symtab;
kdbnearsym(addr, symtab_p2);
}
if (symtab_p2->sym_name || (punc & KDB_SP_VALUE)) {
; /* drop through */
}
else {
return;
}
if (punc & KDB_SP_SPACEB) {
lkmd_printf(" ");
}
if (punc & KDB_SP_VALUE) {
lkmd_printf(kdb_machreg_fmt0, addr);
}
if (symtab_p2->sym_name) {
if (punc & KDB_SP_VALUE) {
lkmd_printf(" ");
}
if (punc & KDB_SP_PAREN) {
lkmd_printf("(");
}
if (strcmp(symtab_p2->mod_name, "kernel")) {
lkmd_printf("[%s]", symtab_p2->mod_name);
}
lkmd_printf("%s", symtab_p2->sym_name);
if (addr != symtab_p2->sym_start) {
lkmd_printf("+0x%lx", addr - symtab_p2->sym_start);
}
if (punc & KDB_SP_SYMSIZE) {
lkmd_printf("/0x%lx", symtab_p2->sym_end - symtab_p2->sym_start);
}
if (punc & KDB_SP_PAREN) {
lkmd_printf(")");
}
}
if (punc & KDB_SP_SPACEA) {
lkmd_printf(" ");
}
if (punc & KDB_SP_NEWLINE) {
lkmd_printf("\n");
}
}
/*
* kdb_strdup
*
* kdb equivalent of strdup, for disasm code.
* Inputs:
* str The string to duplicate.
* type Flags to kmalloc for the new string.
* Outputs:
* None.
* Returns:
* Address of the new string, NULL if storage could not be allocated.
* Locking:
* none.
* Remarks:
* This is not in lib/string.c because it uses kmalloc which is not
* available when string.o is used in boot loaders.
*/
char *kdb_strdup(const char *str, gfp_t type)
{
int n = strlen(str)+1;
char *s = kmalloc(n, type);
if (!s) return NULL;
return strcpy(s, str);
}
/*
* kdb_getarea_size
*
* Read an area of data. The kdb equivalent of copy_from_user, with
* kdb messages for invalid addresses.
* Inputs:
* res Pointer to the area to receive the result.
* addr Address of the area to copy.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int kdb_getarea_size(void *res, unsigned long addr, size_t size)
{
int ret = kdba_getarea_size(res, addr, size);
if (ret) {
if (!KDB_STATE(SUPPRESS)) {
lkmd_printf("kdb_getarea: Bad address 0x%lx\n", addr);
KDB_STATE_SET(SUPPRESS);
}
ret = KDB_BADADDR;
}
else {
KDB_STATE_CLEAR(SUPPRESS);
}
return(ret);
}
/*
* kdb_putarea_size
*
* Write an area of data. The kdb equivalent of copy_to_user, with
* kdb messages for invalid addresses.
* Inputs:
* addr Address of the area to write to.
* res Pointer to the area holding the data.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int kdb_putarea_size(unsigned long addr, void *res, size_t size)
{
int ret = kdba_putarea_size(addr, res, size);
if (ret) {
if (!KDB_STATE(SUPPRESS)) {
lkmd_printf("kdb_putarea: Bad address 0x%lx\n", addr);
KDB_STATE_SET(SUPPRESS);
}
ret = KDB_BADADDR;
}
else {
KDB_STATE_CLEAR(SUPPRESS);
}
return(ret);
}
/*
* kdb_getphys
*
* Read data from a physical address. Validate the address is in range,
* use kmap_atomic() to get data
*
* Similar to kdb_getarea() - but for phys addresses
*
* Inputs:
* res Pointer to the word to receive the result
* addr Physical address of the area to copy
* size Size of the area
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
static int kdb_getphys(void *res, unsigned long addr, size_t size)
{
unsigned long pfn;
void *vaddr;
struct page *page;
pfn = (addr >> PAGE_SHIFT);
if (!pfn_valid(pfn))
return 1;
page = pfn_to_page(pfn);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
vaddr = kmap_atomic(page);
#else
vaddr = kmap_atomic(page, KM_USER0);
#endif
memcpy(res, vaddr + (addr & (PAGE_SIZE -1)), size);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
kunmap_atomic(vaddr);
#else
kunmap_atomic(vaddr, KM_USER0);
#endif
return 0;
}
/*
* kdb_getphysword
*
* Inputs:
* word Pointer to the word to receive the result.
* addr Address of the area to copy.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size)
{
int diag;
__u8 w1;
__u16 w2;
__u32 w4;
__u64 w8;
*word = 0; /* Default value if addr or size is invalid */
switch (size) {
case 1:
if (!(diag = kdb_getphys(&w1, addr, sizeof(w1))))
*word = w1;
break;
case 2:
if (!(diag = kdb_getphys(&w2, addr, sizeof(w2))))
*word = w2;
break;
case 4:
if (!(diag = kdb_getphys(&w4, addr, sizeof(w4))))
*word = w4;
break;
case 8:
if (size <= sizeof(*word)) {
if (!(diag = kdb_getphys(&w8, addr, sizeof(w8))))
*word = w8;
break;
}
/* drop through */
default:
diag = KDB_BADWIDTH;
lkmd_printf("kdb_getphysword: bad width %ld\n", (long) size);
}
return(diag);
}
/*
* kdb_getword
*
* Read a binary value. Unlike kdb_getarea, this treats data as numbers.
* Inputs:
* word Pointer to the word to receive the result.
* addr Address of the area to copy.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int kdb_getword(unsigned long *word, unsigned long addr, size_t size)
{
int diag;
__u8 w1;
__u16 w2;
__u32 w4;
__u64 w8;
*word = 0; /* Default value if addr or size is invalid */
switch (size) {
case 1:
if (!(diag = kdb_getarea(w1, addr)))
*word = w1;
break;
case 2:
if (!(diag = kdb_getarea(w2, addr)))
*word = w2;
break;
case 4:
if (!(diag = kdb_getarea(w4, addr)))
*word = w4;
break;
case 8:
if (size <= sizeof(*word)) {
if (!(diag = kdb_getarea(w8, addr)))
*word = w8;
break;
}
/* drop through */
default:
diag = KDB_BADWIDTH;
lkmd_printf("kdb_getword: bad width %ld\n", (long) size);
}
return(diag);
}
/*
* kdb_putword
*
* Write a binary value. Unlike kdb_putarea, this treats data as numbers.
* Inputs:
* addr Address of the area to write to..
* word The value to set.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int kdb_putword(unsigned long addr, unsigned long word, size_t size)
{
int diag;
__u8 w1;
__u16 w2;
__u32 w4;
__u64 w8;
switch (size) {
case 1:
w1 = word;
diag = kdb_putarea(addr, w1);
break;
case 2:
w2 = word;
diag = kdb_putarea(addr, w2);
break;
case 4:
w4 = word;
diag = kdb_putarea(addr, w4);
break;
case 8:
if (size <= sizeof(word)) {
w8 = word;
diag = kdb_putarea(addr, w8);
break;
}
/* drop through */
default:
diag = KDB_BADWIDTH;
lkmd_printf("kdb_putword: bad width %ld\n", (long) size);
}
return(diag);
}
/*
* kdb_task_state_string
*
* Convert a string containing any of the letters DRSTCZEUIMA to a mask
* for the process state field and return the value. If no argument is
* supplied, return the mask that corresponds to environment variable PS,
* DRSTCZEU by default.
* Inputs:
* s String to convert
* Outputs:
* none.
* Returns:
* Mask for process state.
* Locking:
* none.
* Notes:
* The mask folds data from several sources into a single long value, so
* be carefull not to overlap the bits. TASK_* bits are in the LSB,
* special cases like UNRUNNABLE are in the MSB. As of 2.6.10-rc1 there
* is no overlap between TASK_* and EXIT_* but that may not always be
* true, so EXIT_* bits are shifted left 16 bits before being stored in
* the mask.
*/
#define UNRUNNABLE (1UL << (8*sizeof(unsigned long) - 1)) /* unrunnable is < 0 */
#define RUNNING (1UL << (8*sizeof(unsigned long) - 2))
#define IDLE (1UL << (8*sizeof(unsigned long) - 3))
#define DAEMON (1UL << (8*sizeof(unsigned long) - 4))
unsigned long
kdb_task_state_string(const char *s)
{
long res = 0;
if (!s && !(s = kdbgetenv("PS"))) {
s = "DRSTCZEU"; /* default value for ps */
}
while (*s) {
switch (*s) {
case 'D': res |= TASK_UNINTERRUPTIBLE; break;
case 'R': res |= RUNNING; break;
case 'S': res |= TASK_INTERRUPTIBLE; break;
case 'T': res |= TASK_STOPPED; break;
case 'C': res |= TASK_TRACED; break;
case 'Z': res |= EXIT_ZOMBIE << 16; break;
case 'E': res |= EXIT_DEAD << 16; break;
case 'U': res |= UNRUNNABLE; break;
case 'I': res |= IDLE; break;
case 'M': res |= DAEMON; break;
case 'A': res = ~0UL; break;
default:
lkmd_printf("%s: unknown flag '%c' ignored\n", __FUNCTION__, *s);
break;
}
++s;
}
return res;
}
/*
* kdb_task_state_char
*
* Return the character that represents the task state.
* Inputs:
* p struct task for the process
* Outputs:
* none.
* Returns:
* One character to represent the task state.
* Locking:
* none.
*/
char
kdb_task_state_char (const struct task_struct *p)
{
int cpu = kdb_process_cpu(p);
struct kdb_running_process *krp = kdb_running_process + cpu;
char state = (p->state == 0) ? 'R' :
(p->state < 0) ? 'U' :
(p->state & TASK_UNINTERRUPTIBLE) ? 'D' :
(p->state & TASK_STOPPED) ? 'T' :
(p->state & TASK_TRACED) ? 'C' :
(p->exit_state & EXIT_ZOMBIE) ? 'Z' :
(p->exit_state & EXIT_DEAD) ? 'E' :
(p->state & TASK_INTERRUPTIBLE) ? 'S' : '?';
if (p->pid == 0) {
/* Idle task. Is it really idle, apart from the kdb interrupt? */
if (!kdb_task_has_cpu(p) || krp->irq_depth == 1) {
/* There is a corner case when the idle task takes an
* interrupt and dies in the interrupt code. It has an
* interrupt count of 1 but that did not come from kdb.
* This corner case can only occur on the initial cpu,
* all the others were entered via the kdb IPI.
*/
if (cpu != kdb_initial_cpu || KDB_STATE_CPU(KEYBOARD, cpu))
state = 'I'; /* idle task */
}
}
else if (!p->mm && state == 'S') {
state = 'M'; /* sleeping system daemon */
}
return state;
}
/*
* kdb_task_state
*
* Return true if a process has the desired state given by the mask.
* Inputs:
* p struct task for the process
* mask mask from kdb_task_state_string to select processes
* Outputs:
* none.
* Returns:
* True if the process matches at least one criteria defined by the mask.
* Locking:
* none.
*/
unsigned long kdb_task_state(const struct task_struct *p, unsigned long mask)
{
char state[] = { kdb_task_state_char(p), '\0' };
return (mask & kdb_task_state_string(state)) != 0;
}
struct kdb_running_process kdb_running_process[NR_CPUS];
/* Save the state of a running process and invoke kdb_main_loop. This is
* invoked on the current process on each cpu (assuming the cpu is responding).
*/
int kdb_save_running(struct pt_regs *regs, kdb_reason_t reason,
kdb_reason_t reason2, int error, kdb_dbtrap_t db_result)
{
struct kdb_running_process *krp = kdb_running_process + smp_processor_id();
krp->p = current;
krp->regs = regs;
krp->seqno = kdb_seqno;
krp->irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
kdba_save_running(&(krp->arch), regs);
return kdb_main_loop(reason, reason2, error, db_result, regs);
}
/*
* kdb_unsave_running
*
* Reverse the effect of kdb_save_running.
* Inputs:
* regs struct pt_regs for the process
* Outputs:
* Updates kdb_running_process[] for this cpu.
* Returns:
* none.
* Locking:
* none.
*/
void kdb_unsave_running(struct pt_regs *regs)
{
struct kdb_running_process *krp = kdb_running_process + smp_processor_id();
kdba_unsave_running(&(krp->arch), regs);
krp->seqno = 0;
}
/*
* kdb_print_nameval
*
* Print a name and its value, converting the value to a symbol lookup
* if possible.
* Inputs:
* name field name to print
* val value of field
* Outputs:
* none.
* Returns:
* none.
* Locking:
* none.
*/
void
kdb_print_nameval(const char *name, unsigned long val)
{
kdb_symtab_t symtab;
lkmd_printf(" %-11.11s ", name);