-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
176 lines (156 loc) · 4.64 KB
/
main.rs
File metadata and controls
176 lines (156 loc) · 4.64 KB
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
//! A kernel module to show a simple kernel memory layout visual (on 64bit)
#![no_std]
#![feature(allocator_api, global_asm)]
use kernel::bindings::{
high_memory, FIXADDR_SIZE, FIXADDR_START, MODULES_END, MODULES_VADDR, PAGE_OFFSET, TASK_SIZE,
VMALLOC_END, VMALLOC_START,
};
use kernel::prelude::*;
use kernel::task::Task;
#[cfg(CONFIG_KASAN)]
use kernel::bindings::{KASAN_SHADOW_END, KASAN_SHADOW_START};
module! {
type: MemLayout,
name: b"mem_layout",
author: b"milan@mdaverde.com",
description: b"A kernel module",
license: b"Dual MIT/GPL",
params: {
show_userspace: bool {
default: false,
permissions: 0, // will not be available in sysfs
description: b"Show few userspace VAS details (default = 0/false)",
},
},
}
// usize
const BYTES_KB: u64 = 2_u64.pow(10);
const BYTES_MB: u64 = 2_u64.pow(20);
const BYTES_GB: u64 = 2_u64.pow(30);
// TODO: move memory calc into macros
// Should use format_args here?
macro_rules! pr_b {
($($pr_args: expr), +) => {
pr_info!("| {:<25} {:0>16x} - {:0>16x} | [{} bytes]\n", $($pr_args), +);
};
}
macro_rules! pr_k {
($($pr_args: expr), +) => {
pr_info!("| {:<25} {:0>16x} - {:0>16x} | [{} KB]\n", $($pr_args), +);
};
}
macro_rules! pr_mb {
($($pr_args: expr), +) => {
pr_info!("| {:<25} {:0>16x} - {:0>16x} | [{} MB]\n", $($pr_args), +);
};
}
macro_rules! pr_mb_gb {
($($pr_args: expr), +) => {
pr_info!("| {:<25} {:0>16x} - {:0>16x} | [{} MB = {} GB]\n", $($pr_args), +);
};
}
struct MemLayout;
impl MemLayout {
fn kernel() -> Result<()> {
pr_info!("-> {}\n", "Kernel layout (decreasing by address)");
#[cfg(CONFIG_ARM)]
pr_info!(
"You're currently running on an ARM machine. Layout printed here may need changes\n"
);
pr_mb!(
"fixmap region:",
FIXADDR_START(),
FIXADDR_START() + FIXADDR_SIZE(),
FIXADDR_SIZE() / BYTES_MB
);
pr_mb!(
"module_region:",
MODULES_VADDR(),
MODULES_END(),
(MODULES_END() - MODULES_VADDR()) / BYTES_MB
);
#[cfg(CONFIG_KASAM)]
pr_mb!(
"KASAN shadow:",
KASAN_SHADOW_START(),
KASAN_SHADOW_END(),
(KASAN_SHADOW_END() - KASAN_SHADOW_START()) / BYTES_MB
);
pr_mb_gb!(
"vmalloc region:",
VMALLOC_START(),
VMALLOC_END(),
(VMALLOC_END() - VMALLOC_START()) / BYTES_MB,
(VMALLOC_END() - VMALLOC_START()) / BYTES_GB
);
let highmem = unsafe { high_memory as u64 };
pr_mb_gb!(
"lowmem region:",
PAGE_OFFSET(),
highmem,
(highmem - PAGE_OFFSET()) / BYTES_MB,
(highmem - PAGE_OFFSET()) / BYTES_GB
);
Ok(())
}
fn userspace() -> Result<()> {
let current = Task::current();
pr_info!(
"-> Current task user VAS layout: {} (pid: {})\n",
current.comm().to_str()?,
current.pid()
);
if let Some(mem_desc) = current.mm() {
let mm = unsafe { *mem_desc.as_ptr() };
pr_b!(
"Process environment:",
mm.env_start,
mm.env_end,
mm.env_end - mm.env_start
);
pr_b!(
"Arguments:",
mm.arg_start,
mm.arg_end,
mm.arg_end - mm.arg_start
);
pr_info!(
"| {:<25} {:0>16x} {:>18} |\n",
"Stack start:",
mm.start_stack,
""
);
pr_k!(
"Heap segment:",
mm.start_brk,
mm.brk,
(mm.brk - mm.start_brk) / BYTES_KB
);
pr_b!(
"Static data segment:",
mm.start_data,
mm.end_data,
mm.end_data - mm.start_data
);
pr_b!(
"Text segment:",
mm.start_code,
mm.end_code,
mm.end_code - mm.start_code
);
pr_info!("-> NOTE: TASK_SIZE = {} GB\n", TASK_SIZE() / BYTES_GB)
} else {
pr_info!("No current->mm_struct found\n");
}
Ok(())
}
}
impl KernelModule for MemLayout {
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
MemLayout::kernel()?;
if *show_userspace.read() {
MemLayout::userspace()?;
}
Ok(MemLayout)
}
}