[SCSI] Fix printing of variable length commands
[linux-2.6/next.git] / Documentation / vm / page-types.c
blob66e9358e2144233323e8b90ce75f9007999e7b2f
1 /*
2 * page-types: Tool for querying page flags
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; version 2.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
13 * You should find a copy of v2 of the GNU General Public License somewhere on
14 * your Linux system; if not, write to the Free Software Foundation, Inc., 59
15 * Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * Copyright (C) 2009 Intel corporation
19 * Authors: Wu Fengguang <fengguang.wu@intel.com>
22 #define _LARGEFILE64_SOURCE
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <getopt.h>
30 #include <limits.h>
31 #include <assert.h>
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/fcntl.h>
38 * pagemap kernel ABI bits
41 #define PM_ENTRY_BYTES sizeof(uint64_t)
42 #define PM_STATUS_BITS 3
43 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
44 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
45 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
46 #define PM_PSHIFT_BITS 6
47 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
48 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
49 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
50 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
51 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
53 #define PM_PRESENT PM_STATUS(4LL)
54 #define PM_SWAP PM_STATUS(2LL)
58 * kernel page flags
61 #define KPF_BYTES 8
62 #define PROC_KPAGEFLAGS "/proc/kpageflags"
64 /* copied from kpageflags_read() */
65 #define KPF_LOCKED 0
66 #define KPF_ERROR 1
67 #define KPF_REFERENCED 2
68 #define KPF_UPTODATE 3
69 #define KPF_DIRTY 4
70 #define KPF_LRU 5
71 #define KPF_ACTIVE 6
72 #define KPF_SLAB 7
73 #define KPF_WRITEBACK 8
74 #define KPF_RECLAIM 9
75 #define KPF_BUDDY 10
77 /* [11-20] new additions in 2.6.31 */
78 #define KPF_MMAP 11
79 #define KPF_ANON 12
80 #define KPF_SWAPCACHE 13
81 #define KPF_SWAPBACKED 14
82 #define KPF_COMPOUND_HEAD 15
83 #define KPF_COMPOUND_TAIL 16
84 #define KPF_HUGE 17
85 #define KPF_UNEVICTABLE 18
86 #define KPF_HWPOISON 19
87 #define KPF_NOPAGE 20
88 #define KPF_KSM 21
90 /* [32-] kernel hacking assistances */
91 #define KPF_RESERVED 32
92 #define KPF_MLOCKED 33
93 #define KPF_MAPPEDTODISK 34
94 #define KPF_PRIVATE 35
95 #define KPF_PRIVATE_2 36
96 #define KPF_OWNER_PRIVATE 37
97 #define KPF_ARCH 38
98 #define KPF_UNCACHED 39
100 /* [48-] take some arbitrary free slots for expanding overloaded flags
101 * not part of kernel API
103 #define KPF_READAHEAD 48
104 #define KPF_SLOB_FREE 49
105 #define KPF_SLUB_FROZEN 50
106 #define KPF_SLUB_DEBUG 51
108 #define KPF_ALL_BITS ((uint64_t)~0ULL)
109 #define KPF_HACKERS_BITS (0xffffULL << 32)
110 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
111 #define BIT(name) (1ULL << KPF_##name)
112 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
114 static const char *page_flag_names[] = {
115 [KPF_LOCKED] = "L:locked",
116 [KPF_ERROR] = "E:error",
117 [KPF_REFERENCED] = "R:referenced",
118 [KPF_UPTODATE] = "U:uptodate",
119 [KPF_DIRTY] = "D:dirty",
120 [KPF_LRU] = "l:lru",
121 [KPF_ACTIVE] = "A:active",
122 [KPF_SLAB] = "S:slab",
123 [KPF_WRITEBACK] = "W:writeback",
124 [KPF_RECLAIM] = "I:reclaim",
125 [KPF_BUDDY] = "B:buddy",
127 [KPF_MMAP] = "M:mmap",
128 [KPF_ANON] = "a:anonymous",
129 [KPF_SWAPCACHE] = "s:swapcache",
130 [KPF_SWAPBACKED] = "b:swapbacked",
131 [KPF_COMPOUND_HEAD] = "H:compound_head",
132 [KPF_COMPOUND_TAIL] = "T:compound_tail",
133 [KPF_HUGE] = "G:huge",
134 [KPF_UNEVICTABLE] = "u:unevictable",
135 [KPF_HWPOISON] = "X:hwpoison",
136 [KPF_NOPAGE] = "n:nopage",
137 [KPF_KSM] = "x:ksm",
139 [KPF_RESERVED] = "r:reserved",
140 [KPF_MLOCKED] = "m:mlocked",
141 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
142 [KPF_PRIVATE] = "P:private",
143 [KPF_PRIVATE_2] = "p:private_2",
144 [KPF_OWNER_PRIVATE] = "O:owner_private",
145 [KPF_ARCH] = "h:arch",
146 [KPF_UNCACHED] = "c:uncached",
148 [KPF_READAHEAD] = "I:readahead",
149 [KPF_SLOB_FREE] = "P:slob_free",
150 [KPF_SLUB_FROZEN] = "A:slub_frozen",
151 [KPF_SLUB_DEBUG] = "E:slub_debug",
156 * data structures
159 static int opt_raw; /* for kernel developers */
160 static int opt_list; /* list pages (in ranges) */
161 static int opt_no_summary; /* don't show summary */
162 static pid_t opt_pid; /* process to walk */
164 #define MAX_ADDR_RANGES 1024
165 static int nr_addr_ranges;
166 static unsigned long opt_offset[MAX_ADDR_RANGES];
167 static unsigned long opt_size[MAX_ADDR_RANGES];
169 #define MAX_VMAS 10240
170 static int nr_vmas;
171 static unsigned long pg_start[MAX_VMAS];
172 static unsigned long pg_end[MAX_VMAS];
174 #define MAX_BIT_FILTERS 64
175 static int nr_bit_filters;
176 static uint64_t opt_mask[MAX_BIT_FILTERS];
177 static uint64_t opt_bits[MAX_BIT_FILTERS];
179 static int page_size;
181 static int pagemap_fd;
182 static int kpageflags_fd;
184 static int opt_hwpoison;
185 static int opt_unpoison;
187 static const char hwpoison_debug_fs[] = "/debug/hwpoison";
188 static int hwpoison_inject_fd;
189 static int hwpoison_forget_fd;
191 #define HASH_SHIFT 13
192 #define HASH_SIZE (1 << HASH_SHIFT)
193 #define HASH_MASK (HASH_SIZE - 1)
194 #define HASH_KEY(flags) (flags & HASH_MASK)
196 static unsigned long total_pages;
197 static unsigned long nr_pages[HASH_SIZE];
198 static uint64_t page_flags[HASH_SIZE];
202 * helper functions
205 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
207 #define min_t(type, x, y) ({ \
208 type __min1 = (x); \
209 type __min2 = (y); \
210 __min1 < __min2 ? __min1 : __min2; })
212 #define max_t(type, x, y) ({ \
213 type __max1 = (x); \
214 type __max2 = (y); \
215 __max1 > __max2 ? __max1 : __max2; })
217 static unsigned long pages2mb(unsigned long pages)
219 return (pages * page_size) >> 20;
222 static void fatal(const char *x, ...)
224 va_list ap;
226 va_start(ap, x);
227 vfprintf(stderr, x, ap);
228 va_end(ap);
229 exit(EXIT_FAILURE);
232 static int checked_open(const char *pathname, int flags)
234 int fd = open(pathname, flags);
236 if (fd < 0) {
237 perror(pathname);
238 exit(EXIT_FAILURE);
241 return fd;
245 * pagemap/kpageflags routines
248 static unsigned long do_u64_read(int fd, char *name,
249 uint64_t *buf,
250 unsigned long index,
251 unsigned long count)
253 long bytes;
255 if (index > ULONG_MAX / 8)
256 fatal("index overflow: %lu\n", index);
258 if (lseek(fd, index * 8, SEEK_SET) < 0) {
259 perror(name);
260 exit(EXIT_FAILURE);
263 bytes = read(fd, buf, count * 8);
264 if (bytes < 0) {
265 perror(name);
266 exit(EXIT_FAILURE);
268 if (bytes % 8)
269 fatal("partial read: %lu bytes\n", bytes);
271 return bytes / 8;
274 static unsigned long kpageflags_read(uint64_t *buf,
275 unsigned long index,
276 unsigned long pages)
278 return do_u64_read(kpageflags_fd, PROC_KPAGEFLAGS, buf, index, pages);
281 static unsigned long pagemap_read(uint64_t *buf,
282 unsigned long index,
283 unsigned long pages)
285 return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
288 static unsigned long pagemap_pfn(uint64_t val)
290 unsigned long pfn;
292 if (val & PM_PRESENT)
293 pfn = PM_PFRAME(val);
294 else
295 pfn = 0;
297 return pfn;
302 * page flag names
305 static char *page_flag_name(uint64_t flags)
307 static char buf[65];
308 int present;
309 int i, j;
311 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
312 present = (flags >> i) & 1;
313 if (!page_flag_names[i]) {
314 if (present)
315 fatal("unknown flag bit %d\n", i);
316 continue;
318 buf[j++] = present ? page_flag_names[i][0] : '_';
321 return buf;
324 static char *page_flag_longname(uint64_t flags)
326 static char buf[1024];
327 int i, n;
329 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
330 if (!page_flag_names[i])
331 continue;
332 if ((flags >> i) & 1)
333 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
334 page_flag_names[i] + 2);
336 if (n)
337 n--;
338 buf[n] = '\0';
340 return buf;
345 * page list and summary
348 static void show_page_range(unsigned long voffset,
349 unsigned long offset, uint64_t flags)
351 static uint64_t flags0;
352 static unsigned long voff;
353 static unsigned long index;
354 static unsigned long count;
356 if (flags == flags0 && offset == index + count &&
357 (!opt_pid || voffset == voff + count)) {
358 count++;
359 return;
362 if (count) {
363 if (opt_pid)
364 printf("%lx\t", voff);
365 printf("%lx\t%lx\t%s\n",
366 index, count, page_flag_name(flags0));
369 flags0 = flags;
370 index = offset;
371 voff = voffset;
372 count = 1;
375 static void show_page(unsigned long voffset,
376 unsigned long offset, uint64_t flags)
378 if (opt_pid)
379 printf("%lx\t", voffset);
380 printf("%lx\t%s\n", offset, page_flag_name(flags));
383 static void show_summary(void)
385 int i;
387 printf(" flags\tpage-count MB"
388 " symbolic-flags\t\t\tlong-symbolic-flags\n");
390 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
391 if (nr_pages[i])
392 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
393 (unsigned long long)page_flags[i],
394 nr_pages[i],
395 pages2mb(nr_pages[i]),
396 page_flag_name(page_flags[i]),
397 page_flag_longname(page_flags[i]));
400 printf(" total\t%10lu %8lu\n",
401 total_pages, pages2mb(total_pages));
406 * page flag filters
409 static int bit_mask_ok(uint64_t flags)
411 int i;
413 for (i = 0; i < nr_bit_filters; i++) {
414 if (opt_bits[i] == KPF_ALL_BITS) {
415 if ((flags & opt_mask[i]) == 0)
416 return 0;
417 } else {
418 if ((flags & opt_mask[i]) != opt_bits[i])
419 return 0;
423 return 1;
426 static uint64_t expand_overloaded_flags(uint64_t flags)
428 /* SLOB/SLUB overload several page flags */
429 if (flags & BIT(SLAB)) {
430 if (flags & BIT(PRIVATE))
431 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
432 if (flags & BIT(ACTIVE))
433 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
434 if (flags & BIT(ERROR))
435 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
438 /* PG_reclaim is overloaded as PG_readahead in the read path */
439 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
440 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
442 return flags;
445 static uint64_t well_known_flags(uint64_t flags)
447 /* hide flags intended only for kernel hacker */
448 flags &= ~KPF_HACKERS_BITS;
450 /* hide non-hugeTLB compound pages */
451 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
452 flags &= ~BITS_COMPOUND;
454 return flags;
457 static uint64_t kpageflags_flags(uint64_t flags)
459 flags = expand_overloaded_flags(flags);
461 if (!opt_raw)
462 flags = well_known_flags(flags);
464 return flags;
468 * page actions
471 static void prepare_hwpoison_fd(void)
473 char buf[100];
475 if (opt_hwpoison && !hwpoison_inject_fd) {
476 sprintf(buf, "%s/corrupt-pfn", hwpoison_debug_fs);
477 hwpoison_inject_fd = checked_open(buf, O_WRONLY);
480 if (opt_unpoison && !hwpoison_forget_fd) {
481 sprintf(buf, "%s/renew-pfn", hwpoison_debug_fs);
482 hwpoison_forget_fd = checked_open(buf, O_WRONLY);
486 static int hwpoison_page(unsigned long offset)
488 char buf[100];
489 int len;
491 len = sprintf(buf, "0x%lx\n", offset);
492 len = write(hwpoison_inject_fd, buf, len);
493 if (len < 0) {
494 perror("hwpoison inject");
495 return len;
497 return 0;
500 static int unpoison_page(unsigned long offset)
502 char buf[100];
503 int len;
505 len = sprintf(buf, "0x%lx\n", offset);
506 len = write(hwpoison_forget_fd, buf, len);
507 if (len < 0) {
508 perror("hwpoison forget");
509 return len;
511 return 0;
515 * page frame walker
518 static int hash_slot(uint64_t flags)
520 int k = HASH_KEY(flags);
521 int i;
523 /* Explicitly reserve slot 0 for flags 0: the following logic
524 * cannot distinguish an unoccupied slot from slot (flags==0).
526 if (flags == 0)
527 return 0;
529 /* search through the remaining (HASH_SIZE-1) slots */
530 for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
531 if (!k || k >= ARRAY_SIZE(page_flags))
532 k = 1;
533 if (page_flags[k] == 0) {
534 page_flags[k] = flags;
535 return k;
537 if (page_flags[k] == flags)
538 return k;
541 fatal("hash table full: bump up HASH_SHIFT?\n");
542 exit(EXIT_FAILURE);
545 static void add_page(unsigned long voffset,
546 unsigned long offset, uint64_t flags)
548 flags = kpageflags_flags(flags);
550 if (!bit_mask_ok(flags))
551 return;
553 if (opt_hwpoison)
554 hwpoison_page(offset);
555 if (opt_unpoison)
556 unpoison_page(offset);
558 if (opt_list == 1)
559 show_page_range(voffset, offset, flags);
560 else if (opt_list == 2)
561 show_page(voffset, offset, flags);
563 nr_pages[hash_slot(flags)]++;
564 total_pages++;
567 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
568 static void walk_pfn(unsigned long voffset,
569 unsigned long index,
570 unsigned long count)
572 uint64_t buf[KPAGEFLAGS_BATCH];
573 unsigned long batch;
574 long pages;
575 unsigned long i;
577 while (count) {
578 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
579 pages = kpageflags_read(buf, index, batch);
580 if (pages == 0)
581 break;
583 for (i = 0; i < pages; i++)
584 add_page(voffset + i, index + i, buf[i]);
586 index += pages;
587 count -= pages;
591 #define PAGEMAP_BATCH (64 << 10)
592 static void walk_vma(unsigned long index, unsigned long count)
594 uint64_t buf[PAGEMAP_BATCH];
595 unsigned long batch;
596 unsigned long pages;
597 unsigned long pfn;
598 unsigned long i;
600 while (count) {
601 batch = min_t(unsigned long, count, PAGEMAP_BATCH);
602 pages = pagemap_read(buf, index, batch);
603 if (pages == 0)
604 break;
606 for (i = 0; i < pages; i++) {
607 pfn = pagemap_pfn(buf[i]);
608 if (pfn)
609 walk_pfn(index + i, pfn, 1);
612 index += pages;
613 count -= pages;
617 static void walk_task(unsigned long index, unsigned long count)
619 const unsigned long end = index + count;
620 unsigned long start;
621 int i = 0;
623 while (index < end) {
625 while (pg_end[i] <= index)
626 if (++i >= nr_vmas)
627 return;
628 if (pg_start[i] >= end)
629 return;
631 start = max_t(unsigned long, pg_start[i], index);
632 index = min_t(unsigned long, pg_end[i], end);
634 assert(start < index);
635 walk_vma(start, index - start);
639 static void add_addr_range(unsigned long offset, unsigned long size)
641 if (nr_addr_ranges >= MAX_ADDR_RANGES)
642 fatal("too many addr ranges\n");
644 opt_offset[nr_addr_ranges] = offset;
645 opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
646 nr_addr_ranges++;
649 static void walk_addr_ranges(void)
651 int i;
653 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
655 if (!nr_addr_ranges)
656 add_addr_range(0, ULONG_MAX);
658 for (i = 0; i < nr_addr_ranges; i++)
659 if (!opt_pid)
660 walk_pfn(0, opt_offset[i], opt_size[i]);
661 else
662 walk_task(opt_offset[i], opt_size[i]);
664 close(kpageflags_fd);
669 * user interface
672 static const char *page_flag_type(uint64_t flag)
674 if (flag & KPF_HACKERS_BITS)
675 return "(r)";
676 if (flag & KPF_OVERLOADED_BITS)
677 return "(o)";
678 return " ";
681 static void usage(void)
683 int i, j;
685 printf(
686 "page-types [options]\n"
687 " -r|--raw Raw mode, for kernel developers\n"
688 " -d|--describe flags Describe flags\n"
689 " -a|--addr addr-spec Walk a range of pages\n"
690 " -b|--bits bits-spec Walk pages with specified bits\n"
691 " -p|--pid pid Walk process address space\n"
692 #if 0 /* planned features */
693 " -f|--file filename Walk file address space\n"
694 #endif
695 " -l|--list Show page details in ranges\n"
696 " -L|--list-each Show page details one by one\n"
697 " -N|--no-summary Don't show summay info\n"
698 " -X|--hwpoison hwpoison pages\n"
699 " -x|--unpoison unpoison pages\n"
700 " -h|--help Show this usage message\n"
701 "flags:\n"
702 " 0x10 bitfield format, e.g.\n"
703 " anon bit-name, e.g.\n"
704 " 0x10,anon comma-separated list, e.g.\n"
705 "addr-spec:\n"
706 " N one page at offset N (unit: pages)\n"
707 " N+M pages range from N to N+M-1\n"
708 " N,M pages range from N to M-1\n"
709 " N, pages range from N to end\n"
710 " ,M pages range from 0 to M-1\n"
711 "bits-spec:\n"
712 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
713 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
714 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
715 " =bit1,bit2 flags == (bit1|bit2)\n"
716 "bit-names:\n"
719 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
720 if (!page_flag_names[i])
721 continue;
722 printf("%16s%s", page_flag_names[i] + 2,
723 page_flag_type(1ULL << i));
724 if (++j > 3) {
725 j = 0;
726 putchar('\n');
729 printf("\n "
730 "(r) raw mode bits (o) overloaded bits\n");
733 static unsigned long long parse_number(const char *str)
735 unsigned long long n;
737 n = strtoll(str, NULL, 0);
739 if (n == 0 && str[0] != '0')
740 fatal("invalid name or number: %s\n", str);
742 return n;
745 static void parse_pid(const char *str)
747 FILE *file;
748 char buf[5000];
750 opt_pid = parse_number(str);
752 sprintf(buf, "/proc/%d/pagemap", opt_pid);
753 pagemap_fd = checked_open(buf, O_RDONLY);
755 sprintf(buf, "/proc/%d/maps", opt_pid);
756 file = fopen(buf, "r");
757 if (!file) {
758 perror(buf);
759 exit(EXIT_FAILURE);
762 while (fgets(buf, sizeof(buf), file) != NULL) {
763 unsigned long vm_start;
764 unsigned long vm_end;
765 unsigned long long pgoff;
766 int major, minor;
767 char r, w, x, s;
768 unsigned long ino;
769 int n;
771 n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
772 &vm_start,
773 &vm_end,
774 &r, &w, &x, &s,
775 &pgoff,
776 &major, &minor,
777 &ino);
778 if (n < 10) {
779 fprintf(stderr, "unexpected line: %s\n", buf);
780 continue;
782 pg_start[nr_vmas] = vm_start / page_size;
783 pg_end[nr_vmas] = vm_end / page_size;
784 if (++nr_vmas >= MAX_VMAS) {
785 fprintf(stderr, "too many VMAs\n");
786 break;
789 fclose(file);
792 static void parse_file(const char *name)
796 static void parse_addr_range(const char *optarg)
798 unsigned long offset;
799 unsigned long size;
800 char *p;
802 p = strchr(optarg, ',');
803 if (!p)
804 p = strchr(optarg, '+');
806 if (p == optarg) {
807 offset = 0;
808 size = parse_number(p + 1);
809 } else if (p) {
810 offset = parse_number(optarg);
811 if (p[1] == '\0')
812 size = ULONG_MAX;
813 else {
814 size = parse_number(p + 1);
815 if (*p == ',') {
816 if (size < offset)
817 fatal("invalid range: %lu,%lu\n",
818 offset, size);
819 size -= offset;
822 } else {
823 offset = parse_number(optarg);
824 size = 1;
827 add_addr_range(offset, size);
830 static void add_bits_filter(uint64_t mask, uint64_t bits)
832 if (nr_bit_filters >= MAX_BIT_FILTERS)
833 fatal("too much bit filters\n");
835 opt_mask[nr_bit_filters] = mask;
836 opt_bits[nr_bit_filters] = bits;
837 nr_bit_filters++;
840 static uint64_t parse_flag_name(const char *str, int len)
842 int i;
844 if (!*str || !len)
845 return 0;
847 if (len <= 8 && !strncmp(str, "compound", len))
848 return BITS_COMPOUND;
850 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
851 if (!page_flag_names[i])
852 continue;
853 if (!strncmp(str, page_flag_names[i] + 2, len))
854 return 1ULL << i;
857 return parse_number(str);
860 static uint64_t parse_flag_names(const char *str, int all)
862 const char *p = str;
863 uint64_t flags = 0;
865 while (1) {
866 if (*p == ',' || *p == '=' || *p == '\0') {
867 if ((*str != '~') || (*str == '~' && all && *++str))
868 flags |= parse_flag_name(str, p - str);
869 if (*p != ',')
870 break;
871 str = p + 1;
873 p++;
876 return flags;
879 static void parse_bits_mask(const char *optarg)
881 uint64_t mask;
882 uint64_t bits;
883 const char *p;
885 p = strchr(optarg, '=');
886 if (p == optarg) {
887 mask = KPF_ALL_BITS;
888 bits = parse_flag_names(p + 1, 0);
889 } else if (p) {
890 mask = parse_flag_names(optarg, 0);
891 bits = parse_flag_names(p + 1, 0);
892 } else if (strchr(optarg, '~')) {
893 mask = parse_flag_names(optarg, 1);
894 bits = parse_flag_names(optarg, 0);
895 } else {
896 mask = parse_flag_names(optarg, 0);
897 bits = KPF_ALL_BITS;
900 add_bits_filter(mask, bits);
903 static void describe_flags(const char *optarg)
905 uint64_t flags = parse_flag_names(optarg, 0);
907 printf("0x%016llx\t%s\t%s\n",
908 (unsigned long long)flags,
909 page_flag_name(flags),
910 page_flag_longname(flags));
913 static const struct option opts[] = {
914 { "raw" , 0, NULL, 'r' },
915 { "pid" , 1, NULL, 'p' },
916 { "file" , 1, NULL, 'f' },
917 { "addr" , 1, NULL, 'a' },
918 { "bits" , 1, NULL, 'b' },
919 { "describe" , 1, NULL, 'd' },
920 { "list" , 0, NULL, 'l' },
921 { "list-each" , 0, NULL, 'L' },
922 { "no-summary", 0, NULL, 'N' },
923 { "hwpoison" , 0, NULL, 'X' },
924 { "unpoison" , 0, NULL, 'x' },
925 { "help" , 0, NULL, 'h' },
926 { NULL , 0, NULL, 0 }
929 int main(int argc, char *argv[])
931 int c;
933 page_size = getpagesize();
935 while ((c = getopt_long(argc, argv,
936 "rp:f:a:b:d:lLNXxh", opts, NULL)) != -1) {
937 switch (c) {
938 case 'r':
939 opt_raw = 1;
940 break;
941 case 'p':
942 parse_pid(optarg);
943 break;
944 case 'f':
945 parse_file(optarg);
946 break;
947 case 'a':
948 parse_addr_range(optarg);
949 break;
950 case 'b':
951 parse_bits_mask(optarg);
952 break;
953 case 'd':
954 describe_flags(optarg);
955 exit(0);
956 case 'l':
957 opt_list = 1;
958 break;
959 case 'L':
960 opt_list = 2;
961 break;
962 case 'N':
963 opt_no_summary = 1;
964 break;
965 case 'X':
966 opt_hwpoison = 1;
967 prepare_hwpoison_fd();
968 break;
969 case 'x':
970 opt_unpoison = 1;
971 prepare_hwpoison_fd();
972 break;
973 case 'h':
974 usage();
975 exit(0);
976 default:
977 usage();
978 exit(1);
982 if (opt_list && opt_pid)
983 printf("voffset\t");
984 if (opt_list == 1)
985 printf("offset\tlen\tflags\n");
986 if (opt_list == 2)
987 printf("offset\tflags\n");
989 walk_addr_ranges();
991 if (opt_list == 1)
992 show_page_range(0, 0, 0); /* drain the buffer */
994 if (opt_no_summary)
995 return 0;
997 if (opt_list)
998 printf("\n\n");
1000 show_summary();
1002 return 0;