-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathdo_kernelseg.sh
More file actions
executable file
·404 lines (349 loc) · 13.1 KB
/
do_kernelseg.sh
File metadata and controls
executable file
·404 lines (349 loc) · 13.1 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
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
#!/bin/bash
# do_kernelseg.sh
# Part of the procmap project:
# https://github.com/kaiwan/procmap
#
# (c) Kaiwan NB
# All arch-specific vars are here!
source ${ARCHFILE} || {
echo "${name}: fatal: could not source ${ARCHFILE} , aborting..."
exit 1
}
KSPARSE_ENTRY="<... K sparse region ...>"
VAS_NONCANONICAL_HOLE="<... 64-bit: non-canonical hole ...>"
export MAPFLAG_WITHIN_REGION=10
export RAM_START_PHYADDR=0x0
#-----------------------s h o w A r r a y -----------------------------
# Parameters:
# $1 : debug print; if 1, it's just a debug print, if 0 we're writing it's
# data to a file in order to process further (sort/etc)
show_gkArray()
{
local i k DIM=6
[ $1 -eq 1 ] && {
echo
decho "gkRow = ${gkRow}"
echo "show_gkArray():
[segname,size,start_kva,end_kva,mode,flags]"
}
for ((i=0; i<${gkRow}; i+=${DIM}))
do
printf "%s," "${gkArray[${i}]}" # segname
let k=i+1
printf "%llu," "${gkArray[${k}]}" # seg size
let k=i+2
printf "%llx," "${gkArray[${k}]}" # start kva
let k=i+3
printf "%llx," "${gkArray[${k}]}" # end kva
let k=i+4
printf "%s," "${gkArray[${k}]}" # mode
let k=i+5
printf "%s" "${gkArray[${k}]}" # flags
printf "\n"
done
} # end show_gkArray()
# Setup the kernel Sparse region at the very top (high) end of the VAS
# in the gkArray[]
# ARCH SPECIFIC ! See the arch-specific config setup func in lib_procmap.sh
# to see the actual values specified; it's sourced here via the
# 'source ${ARCHFILE}' done at the beginning!
setup_ksparse_top()
{
#set -x
gkRow=0
# Require the topmost valid kernel va, query it from the o/p of our
# kernel component, the procmap LKM
local top_kva=0x$(head -n1 ${KSEGFILE} |awk -F"${gDELIM}" '{print $2}')
#locate_region ${top_kva} ${HIGHEST_KVA}
local gap_dec=$((HIGHEST_KVA-top_kva))
if [ ${gap_dec} -gt ${PAGE_SIZE} ]; then
append_kernel_mapping "${KSPARSE_ENTRY}" "${gap_dec}" ${top_kva} \
"${HIGHEST_KVA}" "---" 0
fi
} # end setup_ksparse_top()
# pa2va
# Convert the given phy addr (pa), to a kernel va (kva)
# CAREFUL!
# We do so by exploiting the fact that the kernel direct-maps all platform
# RAM into the kernel segment starting at PAGE_OFFSET (i.e. into the lowmem
# region). So,
# kva = pa + PAGE_OFFSET
# HOWEVER, this ONLY holds true for direct-mapped kernel RAM not for ANYTHING
# else!
# We EXPECT to ONLY be passed a physical addr that maps to the kernel direct-
# mapped addresses - lowmem addr.
#
# NOTE NOTE NOTE
# Wrt the kernel code/data/bss, realized that converting the physical addr
# found in /proc/iomem to a KVA is NOT a simple matter of adding the PA to
# the PAGE_OFFSET value. It's platform-specific! F.e. on the TI BBB AArch32 (AM35xx
# SoC), the Device Tree shows that RAM's mapped at 0x8000 0000 physical addr. We
# have to take this into a/c else we'll calculate the KVA wrongly... So
# kva = (pa - RAM_START_PHYADDR) + PAGE_OFFSET
# If we're not on a DT-based platform (like x86), then RAM_START_PHYADDR is 0...
#
# Parameters:
# $1 : phy addr (pa)
pa2va()
{
# TIP : for bash arithmetic w/ large #s, first calculate in *decimal* base using
# bc(1), then convert it to hex as required (via printf)
local pgoff_dec=$(printf "%llu" 0x${PAGE_OFFSET} 2>/dev/null)
local pa_dec=$(printf "%llu" 0x${1})
local RAM_START_PHYADDR_DEC=$(printf "%llu" ${RAM_START_PHYADDR})
local kva=$(bc <<< "(${pa_dec}-${RAM_START_PHYADDR_DEC})+${pgoff_dec}")
printf "${FMTSPC_VA}" ${kva}
} # end pa2va
get_ram_phyaddr_from_dt()
{
# ... Eg. DT snippet for RAM bank (for the TI BBB) ...
# memory@80000000 {
# device_type = "memory";
# reg = < 0x80000000 0x20000000 >;
# ^^^^^^^^^^ len
# $1 $2 $3 _$4_ $5
# this is the start phy addr of RAM !
# };
local dt_ram=0
[[ ! -d /proc/device-tree ]] && return # no DT, np
# Get it from the token after the @ symbol; easier...
dt_ram=$(dtc -I fs /proc/device-tree/ 2>/dev/null |grep -A3 "memory@"|grep "reg *= *<")
# fetch only the first hex number (following the '<')
RAM_START_PHYADDR=$(echo $dt_ram |awk -F"<" '{print $2}' |awk '{print $1}')
decho "RAM_START_PHYADDR = ${RAM_START_PHYADDR}"
}
# setup_kernelimg_mappings
# Setup mappings for the kernel image itself; this usually consists of (could
# be fewer entries on some arch's):
# sudo grep -w "Kernel" /proc/iomem
# 297c00000-2988031d0 : Kernel code
# 2988031d1-29926c5bf : Kernel data
# 2994ea000-29978ffff : Kernel bss
# BUT, Carefully NOTE - the above are PHYSICAL ADDR, not kva's;
# So, we'll have to convert them to kva's -SEE NOTE BELOW! - and then insert
# them (in order by descending kva) into our gkArray[] data structure.
setup_kernelimg_mappings()
{
local TMPF=/tmp/${name}/kimgpa
local start_pa end_pa mapname
local start_kva end_kva
###--- This appears to be the ONE place where we still require sudo ! ---###
sudo grep -w "Kernel" /proc/iomem > ${TMPF}
#--- loop over the kernel image recs
IFS=$'\n'
local i=1
local REC
local prev_startkva=0 #${TB_256} #0
###--- NOTE NOTE NOTE
# Wrt the kernel code/data/bss, realized that converting the physical addr
# found in /proc/iomem to a KVA is NOT a simple matter of adding the PA to
# the PAGE_OFFSET value. It's platform-specifc! F.e. on the TI BBB AArch32 (AM35xx
# SoC), the Device Tree shows that RAM's mapped at 0x8000 0000 physical addr. We
# have to take this into a/c else we'll calculate the KVA wrongly...
# BBB DTS:
# ...
# memory@80000000 {
# device_type = "memory";
# reg = < 0x80000000 0x20000000 >;
# ^^^^^^^^^^ len
# this is the start phy addr of RAM !
# };
# ...
###---
get_ram_phyaddr_from_dt
for REC in $(cat ${TMPF})
do
#decho "REC: $REC"
start_pa=$(echo "${REC}" |cut -d"-" -f1)
start_pa=$(trim ${start_pa})
end_pa=$(echo "${REC}" |cut -d"-" -f2 |cut -d":" -f1)
end_pa=$(trim ${end_pa})
mapname=$(echo "${REC}" |cut -d":" -f2)
mapname=$(trim ${mapname})
# Convert pa to kva
start_kva=$(pa2va ${start_pa})
#echo "start_kva = ${start_kva}"
end_kva=$(pa2va ${end_pa})
# Write to 'kernel seg' file
# ksegfile record fmt:
# start-kva,end-kva,perms,name
local ekva_dec=$(printf "%llu" 0x${end_kva})
local skva_dec=$(printf "%llu" 0x${start_kva})
local gap=$(bc <<< "(${ekva_dec}-${skva_dec})")
#decho "k img: ${mapname},${gap},${start_kva},${end_kva}"
append_kernel_mapping "${mapname}" ${gap} 0x${start_kva} \
0x${end_kva} "..." ${MAPFLAG_WITHIN_REGION}
# sparse region?
gap=$(bc <<< "(${prev_startkva}-${ekva_dec})")
#gap=$(bc <<< "(${ekva_dec}-${prev_startkva})") # ?
#decho "prev_startkva = ${prev_startkva} ; ekva_dec = ${ekva_dec}"
decho "gap = ${gap}"
# RELOOK !
# the redirection to null dev gets rid of this:
# "./do_kernelseg.sh: line 153: [: -18446635713769246384: integer expression expected"
if [ ${gap} -gt ${PAGE_SIZE} 2>/dev/null ] ; then
local start_kva_sparse=$(printf "0x%llx" ${skva_dec})
local prev_startkva_hex=$(printf "0x%llx" ${prev_startkva})
append_kernel_mapping "${KSPARSE_ENTRY}" "${gap}" ${start_kva_sparse} \
${prev_startkva_hex} "---" 0
fi
let i=i+1
prev_startkva=${skva_dec}
done 1>&2
#----------
# Sort by descending kva!
[ ${DEBUG} -eq 0 ] && rm -f ${TMPF} || true
} # end setup_kernelimg_mappings
#----------- i n t e r p r e t _ k e r n e l _ r e c -------------------
# Interpret record (a CSV 'line' passed as $1) and populate the gkArray[]
# n-dim array.
# Format:
# start_kva,end_kva,mode,name_of_region
# ; kva = kernel virtual address
# eg. $1 =
# 0xffff9be100000000,0xffff9be542800000,rwx,lowmem region
#
# Parameters:
# $1 : the above CSV format string of 4 fields {start_kva,end_kva,mode,region-name}
# $2 : loop index (starts @ 1)
# Populate the global 'n-dim' (n=5) array gkArr.
interpret_kernel_rec()
{
local numcol=$(echo "$1" | tr ',' ' ' | wc -w)
#echo "p1 = $1; nc=$numcol"
[[ ${numcol} -lt 4 ]] && return # req 5 fields in the record (4 as segname could be 1 word)
local gap=0 # size (in bytes, decimal) of the kernel region,
# i.e., end_kva - start_kva
local start_kva=0x$(echo "${1}" |cut -d"${gDELIM}" -f1)
local end_kva=0x$(echo "${1}" |cut -d"${gDELIM}" -f2)
#echo "numcol = $numcol; skva=${start_kva} ekva=${end_kva}"
# Skip comment lines
echo "${start_kva}" | grep -q "^#" && return
local mode=$(echo "${1}" |cut -d"${gDELIM}" -f3)
local name=$(echo "${1}" |cut -d"${gDELIM}" -f4)
[ -z "${name}" ] && segment=" [-unnamed-] "
# Convert hex to dec
local start_dec=$(printf "%llu" ${start_kva})
local end_dec=$(printf "%llu" ${end_kva})
local seg_sz=$(printf "%llu" $((end_dec-start_dec))) # in bytes
# The global 5d-array's format is:
# col0 col1 col2 col3 col4
# row'n' [regname],[size],[start_kva],[end_kva],[mode]
# TODO
# vsyscall: manually place detail into gkArray[]
#------------ Sparse Detection
if [ ${KSPARSE_SHOW} -eq 1 ]; then
DetectedSparse=0
decho "$2: seg=${name} prevseg_name=${prevseg_name} , gkRow=${gkRow} "
# Detect sparse region, and if present, insert into the gArr[].
# Sparse region detected by condition:
# gap = prev_seg_start - this-segment-end > 1 page
if [ $2 -eq 1 ] ; then # ignore the first kernel region
decho "k sparse check: skipping first kernel region"
else
local end_hex=$(printf "0x%llx" ${end_dec})
prevseg_start_kva_hex=$(printf "0x%llx" ${prevseg_start_kva})
decho "@@ gap = prevseg_start_kva_hex: ${prevseg_start_kva_hex} - end_hex: ${end_hex}"
gap=$(bc <<< "(${prevseg_start_kva}-${end_dec})")
#local gap_hex=$(printf "0x%llx" ${gap})
decho "gap = ${gap}"
[ ${gap} -gt ${PAGE_SIZE} ] && DetectedSparse=1
fi
if [ ${DetectedSparse} -eq 1 ]; then
local start_kva_dec=$(bc <<< "(${prevseg_start_kva}-${gap})")
local start_kva_sparse=$(printf "0x%llx" ${start_kva_dec})
append_kernel_mapping "${KSPARSE_ENTRY}" "${gap}" ${start_kva_sparse} \
${prevseg_start_kva_hex} "---" 0
# TODO : count k sparse and u sparse regions seperately!
# Stats
#[ ${SHOW_KSTATS} -eq 1 ] && {
# let gNumSparse=gNumSparse+1
# let gTotalSparseSize=gTotalSparseSize+gap
#}
fi
prevseg_start_kva=${start_dec}
fi
#--------------
#--- Populate the global array
append_kernel_mapping "${name}" ${seg_sz} ${start_kva} ${end_kva} ${mode} 0
[ ${SHOW_KSTATS} -eq 1 ] && {
let gTotalSegSize=${gTotalSegSize}+${seg_sz}
}
prevseg_name=${name}
decho "prevseg_name = ${prevseg_name}
"
} # end interpret_kernel_rec()
# Insert k sparse region from last (lowest) valid k mapping (often, lowmem)
# to first valid kernel va
# Parameters
# $1 : prev segment/mapping start va (in hex)
setup_ksparse_lowest()
{
# The highest uva:
# On 32-bit = it can be the modules region on Aarch32
# On 64-bit = it varies with the arch
# x86_64: Ref: <kernel-src>/Documentation/x86/x86_64/mm.rst
# Start addr | Offset | End addr | Size | VM area description
# 0000000000000000 | 0 | 00007fffffffffff | 128 TB | user-space virtual memory, different per mm
#
# NOTE- this info is encoded into the arch-specific config setup code in
# lib_procmap.sh, pl refer to it.
# The way to perform arithmetic in bash on large #s is to use bc(1);
# AND to to decimal arithmetic and then convert to hex if required!
# calculation: $1 - START_KVA
# ;the START_KVA value is in the ARCHFILE
local kva_dec=$(printf "%llu" ${1})
#local START_KVA_DEC=$(printf "%llu" ${START_KVA})
local gap_dec=$(bc <<< "(${kva_dec}-${START_KVA_DEC})")
#decho "p1 = $1 , START_KVA = ${START_KVA} ; gap_dec=${gap_dec}"
if [ ${gap_dec} -gt ${PAGE_SIZE} ]; then
append_kernel_mapping "${KSPARSE_ENTRY}" "${gap_dec}" 0x${START_KVA} \
${1} "---" 0
fi
} # end setup_ksparse_lowest()
setup_noncanonical_sparse_region()
{
# this is ARCH SPECIFIC and ONLY for 64-bit
# the noncanonical 'hole' spans from 'start kva' down to 'end uva'
if [ ${IS_64_BIT} -eq 1 ]; then
append_kernel_mapping "${VAS_NONCANONICAL_HOLE}" "${NONCANONICAL_REG_SIZE}" \
0x${END_UVA} 0x${START_KVA} "---" 0
fi
}
# populate_kernel_segment_mappings()
populate_kernel_segment_mappings()
{
setup_ksparse_top
setup_kernelimg_mappings
#---------- Loop over the kernel segment data records
export IFS=$'\n'
local i=1
local REC
prevseg_start_kva=0
for REC in $(cat ${KSEGFILE})
do
decho "REC: $REC"
interpret_kernel_rec ${REC} ${i}
#printf "=== %06d / %06d\r" ${i} ${gFileLines}
let i=i+1
done 1>&2
#----------
#exit
# TODO - ins k sparse region from last (lowest) valid k mapping to top end uva
prevseg_start_kva_hex=$(printf "0x%llx" ${prevseg_start_kva})
decho "prevseg_start_kva_hex = ${prevseg_start_kva_hex}"
setup_ksparse_lowest ${prevseg_start_kva_hex}
# Non-canonical sparse region for 64-bit
if [ ${IS_64_BIT} -eq 1 ]; then #-a ${SHOW_USERSPACE} -eq 1 ] ; then
setup_noncanonical_sparse_region
fi
[ ${DEBUG} -eq 1 ] && show_gkArray 1
##################
# Get all the kernel mapping data into a file:
# Reverse sort by 4th field, the hexadecimal end va; simple ASCII sort works
# because numbers 0-9a-f are anyway in alphabetical order
show_gkArray 0 > /tmp/${name}/pmk
sort -t"," -k4 -r /tmp/${name}/pmk > /tmp/${name}/pmkfinal
##################
[ ${DEBUG} -eq 1 ] && cat /tmp/${name}/pmkfinal
} # end populate_kernel_segment_mappings()