1e8168840SSong Liu // SPDX-License-Identifier: GPL-2.0
2e8168840SSong Liu /* Copyright (c) 2020 Facebook */
3e8168840SSong Liu #include "bpf_iter.h"
4e8168840SSong Liu #include <bpf/bpf_helpers.h>
5e8168840SSong Liu 
6e8168840SSong Liu char _license[] SEC("license") = "GPL";
7e8168840SSong Liu 
8e8168840SSong Liu /* Copied from mm.h */
9e8168840SSong Liu #define VM_READ		0x00000001
10e8168840SSong Liu #define VM_WRITE	0x00000002
11e8168840SSong Liu #define VM_EXEC		0x00000004
12e8168840SSong Liu #define VM_MAYSHARE	0x00000080
13e8168840SSong Liu 
14e8168840SSong Liu /* Copied from kdev_t.h */
15e8168840SSong Liu #define MINORBITS	20
16e8168840SSong Liu #define MINORMASK	((1U << MINORBITS) - 1)
17e8168840SSong Liu #define MAJOR(dev)	((unsigned int) ((dev) >> MINORBITS))
18e8168840SSong Liu #define MINOR(dev)	((unsigned int) ((dev) & MINORMASK))
19e8168840SSong Liu 
20e8168840SSong Liu #define D_PATH_BUF_SIZE 1024
21e8168840SSong Liu char d_path_buf[D_PATH_BUF_SIZE] = {};
22e8168840SSong Liu __u32 pid = 0;
23*b3e1331eSKui-Feng Lee __u32 one_task = 0;
24*b3e1331eSKui-Feng Lee __u32 one_task_error = 0;
25e8168840SSong Liu 
proc_maps(struct bpf_iter__task_vma * ctx)26e8168840SSong Liu SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx)
27e8168840SSong Liu {
28e8168840SSong Liu 	struct vm_area_struct *vma = ctx->vma;
29e8168840SSong Liu 	struct seq_file *seq = ctx->meta->seq;
30e8168840SSong Liu 	struct task_struct *task = ctx->task;
31e8168840SSong Liu 	struct file *file;
32e8168840SSong Liu 	char perm_str[] = "----";
33e8168840SSong Liu 
34e8168840SSong Liu 	if (task == (void *)0 || vma == (void *)0)
35e8168840SSong Liu 		return 0;
36e8168840SSong Liu 
37e8168840SSong Liu 	file = vma->vm_file;
38*b3e1331eSKui-Feng Lee 	if (task->tgid != pid) {
39*b3e1331eSKui-Feng Lee 		if (one_task)
40*b3e1331eSKui-Feng Lee 			one_task_error = 1;
41e8168840SSong Liu 		return 0;
42*b3e1331eSKui-Feng Lee 	}
43e8168840SSong Liu 	perm_str[0] = (vma->vm_flags & VM_READ) ? 'r' : '-';
44e8168840SSong Liu 	perm_str[1] = (vma->vm_flags & VM_WRITE) ? 'w' : '-';
45e8168840SSong Liu 	perm_str[2] = (vma->vm_flags & VM_EXEC) ? 'x' : '-';
46e8168840SSong Liu 	perm_str[3] = (vma->vm_flags & VM_MAYSHARE) ? 's' : 'p';
47e8168840SSong Liu 	BPF_SEQ_PRINTF(seq, "%08llx-%08llx %s ", vma->vm_start, vma->vm_end, perm_str);
48e8168840SSong Liu 
49e8168840SSong Liu 	if (file) {
50e8168840SSong Liu 		__u32 dev = file->f_inode->i_sb->s_dev;
51e8168840SSong Liu 
52e8168840SSong Liu 		bpf_d_path(&file->f_path, d_path_buf, D_PATH_BUF_SIZE);
53e8168840SSong Liu 
54e8168840SSong Liu 		BPF_SEQ_PRINTF(seq, "%08llx ", vma->vm_pgoff << 12);
55e8168840SSong Liu 		BPF_SEQ_PRINTF(seq, "%02x:%02x %u", MAJOR(dev), MINOR(dev),
56e8168840SSong Liu 			       file->f_inode->i_ino);
57e8168840SSong Liu 		BPF_SEQ_PRINTF(seq, "\t%s\n", d_path_buf);
58e8168840SSong Liu 	} else {
59e8168840SSong Liu 		BPF_SEQ_PRINTF(seq, "%08llx 00:00 0\n", 0ULL);
60e8168840SSong Liu 	}
61e8168840SSong Liu 	return 0;
62e8168840SSong Liu }
63