<div dir="ltr">I like your curiosities and interests in Linux kernel.<a href="http://virtuallyhyper.com/2013/07/rhcsa-and-rhce-chapter-10-the-kernel/">http://virtuallyhyper.com/2013/07/rhcsa-and-rhce-chapter-10-the-kernel/</a><div>
<br></div><div>Instead of answering one by one, I think I will just identify the knowledge you are lacking:</div><div><br></div><div>Memory management (from both x86/intel and linux kernel perspective).</div><div><br></div>
<div>There are many many resources out there for you in these area, eg:<br></div><div><br></div><div><a href="http://en.wikipedia.org/wiki/Page_table">http://en.wikipedia.org/wiki/Page_table</a></div><div><a href="http://en.wikipedia.org/wiki/X86-64">http://en.wikipedia.org/wiki/X86-64</a></div>
<div><br></div><div>(both boring, but just understand it well enough)</div><div><br></div><div><a href="http://wiki.osdev.org/Paging">http://wiki.osdev.org/Paging</a> (good explanation....understand it very very well).<br>
</div><div><br></div><div>The ultimate classic ebook:</div><div><br></div><div><a href="https://www.kernel.org/doc/gorman/pdf/understand.pdf">https://www.kernel.org/doc/gorman/pdf/understand.pdf</a><br></div><div><br></div>
<div>And this blog site has tons of good info on intel/memory etc:</div><div><br></div><div><a href="http://duartes.org/gustavo/blog/post/cpu-rings-privilege-and-protection/">http://duartes.org/gustavo/blog/post/cpu-rings-privilege-and-protection/</a><br>
</div><div><div><a href="http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/">http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/</a><br></div></div><div><br></div><div><a href="http://virtuallyhyper.com/2013/07/rhcsa-and-rhce-chapter-10-the-kernel/">http://virtuallyhyper.com/2013/07/rhcsa-and-rhce-chapter-10-the-kernel/</a><br>
</div><div><br></div><div><a class="" target="_blank" href="http://www.cse.psu.edu/~anand/spring01/linux/memory.ppt" style="color:rgb(85,102,221);text-decoration:none;display:inline-block;max-width:500px;overflow:hidden;padding-bottom:1px;padding-top:4px;text-overflow:ellipsis;white-space:nowrap;font-family:Arial,sans-serif;font-size:12px">http://www.cse.psu.edu/~anand/spring01/linux/memory.ppt</a><br>
</div><div><br></div><div>One more thing:</div><div><br></div><div><span style="font-family:arial,sans-serif;font-size:13px">"readelf -S -W vmlinux" shows u the sections and the address where the different sections are supposed to be loaded in memory. If u replace the vmlinux with the kernel module, eg: ip_tables.ko, then it says:</span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">starting at offset 0x328c blah blah....</font><br></div><div><font face="arial, sans-serif"><br></font></div>
<div><font face="arial, sans-serif">so the loaded address is with respect to ZERO, but then </font><span style="font-family:arial,sans-serif;font-size:13px">the actual module address is:</span></div><div><br></div><div><div>
sudo cat /proc/modules |grep ip_table</div><div><br></div><div>ip_tables 18106 1 iptable_filter, Live 0xf8bf5000<br></div><div><br></div></div><div>So all the output from your readelf, just add 0xf8bf5000 to it and you will get the actual virtual address of that section IN MEMORY.</div>
<div><br></div><div>Just only in memory. In file, the file offset of the section is different. And many parts inside the ELF is also different from memory too: you will need to add the virtual load address (above) to the offset as specified inside the relocation tables (objdump -r), and for each section there is a separate relocation table (all independent from another, meaning that the different section CAN BE loaded to different parts in memory).</div>
<div><br></div><div>Thanks.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Aug 3, 2014 at 11:59 PM, Lucas Tanure <span dir="ltr"><<a href="mailto:tanure@linux.com" target="_blank">tanure@linux.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'm looking for some site, pdf, book etc, that can answer this questions.<br>
For now I have :<br>
<a href="http://unix.stackexchange.com/questions/5124/what-does-the-virtual-kernel-memory-layout-in-dmesg-imply" target="_blank">http://unix.stackexchange.com/questions/5124/what-does-the-virtual-kernel-memory-layout-in-dmesg-imply</a><br>
<br>
<br>
I want to understand a few things about the memory and the execution<br>
of Linux kernel.<br>
Taking from a X86 and grub I have:<br>
<br>
1) Grub loads kernel and root file system in memory, and the vmlinux<br>
has the code to decompress it self, right ? linux<br>
<br>
2) The address of load kernel is always the same ? And It's at<br>
compilation time that is chosen ?<br>
<br>
2a) The kernel takes places in 3g-4g memory place, and user space from 0 to 3gb.<br>
But if the pc has only 256mb of memory ?<br>
And when pc has 16gb of memory, the user space will be split in two ?<br>
<br>
2b) And if kernel has soo many modules that needs more than 1gb to run ?<br>
<br>
2c) How we configure all of that memory configs ? make menuconfig and friends ?<br>
<br>
3) The function A will call functon B. B is at 0xGGGGGG in .text<br>
section, but kernel was loaded in address 0xJJJJJJJJJJ, how A will<br>
find B ?<br>
<br>
4) Please consider this:<br>
$ readelf -S -W vmlinux<br>
There are 37 section headers, starting at offset 0xe05718:<br>
<br>
Section Headers:<br>
[Nr] Name Type Address<br>
Off Size ES Flg Lk Inf Al<br>
[ 0] NULL<br>
0000000000000000 000000 000000 00 0 0 0<br>
[ 1] .text PROGBITS<br>
ffffffff81000000 200000 53129a 00 AX 0 0 4096<br>
[ 2] .notes NOTE<br>
ffffffff8153129c 73129c 0001d8 00 AX 0 0 4<br>
[ 3] __ex_table PROGBITS ffffffff81531480<br>
731480 002018 00 A 0 0 8<br>
[ 4] .rodata PROGBITS<br>
ffffffff81600000 800000 1655ee 00 A 0 0 64<br>
[ 5] __bug_table PROGBITS ffffffff817655f0<br>
9655f0 005424 00 A 0 0 1<br>
[ 6] .pci_fixup PROGBITS ffffffff8176aa18<br>
96aa18 002f88 00 A 0 0 8<br>
[ 7] .tracedata PROGBITS ffffffff8176d9a0<br>
96d9a0 00003c 00 A 0 0 1<br>
[ 8] __ksymtab PROGBITS ffffffff8176d9e0<br>
96d9e0 00e710 00 A 0 0 16<br>
[ 9] __ksymtab_gpl PROGBITS ffffffff8177c0f0<br>
97c0f0 00a150 00 A 0 0 16<br>
[10] __kcrctab PROGBITS ffffffff81786240<br>
986240 007388 00 A 0 0 8<br>
[11] __kcrctab_gpl PROGBITS ffffffff8178d5c8<br>
98d5c8 0050a8 00 A 0 0 8<br>
[12] __ksymtab_strings PROGBITS ffffffff81792670<br>
992670 01cb42 00 A 0 0 1<br>
[13] __init_rodata PROGBITS ffffffff817af1c0<br>
9af1c0 0000e8 00 A 0 0 32<br>
[14] __param PROGBITS ffffffff817af2a8<br>
9af2a8 000b00 00 A 0 0 8<br>
[15] __modver PROGBITS ffffffff817afda8<br>
9afda8 000258 00 A 0 0 8<br>
[16] .data PROGBITS<br>
ffffffff81800000 a00000 0e1180 00 WA 0 0 4096<br>
[17] .vvar PROGBITS<br>
ffffffff818e2000 ae2000 001000 00 WA 0 0 16<br>
[18] .data..percpu PROGBITS 0000000000000000<br>
c00000 015300 00 WA 0 0 4096<br>
[19] .init.text PROGBITS<br>
ffffffff818f9000 cf9000 0503ea 00 AX 0 0 16<br>
[20] .init.data PROGBITS<br>
ffffffff8194a000 d4a000 09e4c8 00 WA 0 0 4096<br>
[21] .x86_cpu_dev.init PROGBITS ffffffff819e84c8<br>
de84c8 000018 00 A 0 0 8<br>
[22] .parainstructions PROGBITS ffffffff819e84e0<br>
de84e0 00bd3c 00 A 0 0 8<br>
[23] .altinstructions PROGBITS ffffffff819f4220<br>
df4220 005f40 00 A 0 0 1<br>
[24] .altinstr_replacement PROGBITS ffffffff819fa160<br>
dfa160 001a69 00 AX 0 0 1<br>
[25] .iommu_table PROGBITS ffffffff819fbbd0<br>
dfbbd0 0000f0 00 A 0 0 8<br>
[26] .apicdrivers PROGBITS ffffffff819fbcc0<br>
dfbcc0 000020 00 WA 0 0 8<br>
[27] .exit.text PROGBITS ffffffff819fbce0<br>
dfbce0 0009bc 00 AX 0 0 1<br>
[28] .smp_locks PROGBITS ffffffff819fd000<br>
dfd000 005000 00 A 0 0 4<br>
[29] .data_nosave PROGBITS ffffffff81a02000<br>
e02000 001000 00 WA 0 0 4<br>
[30] .bss NOBITS<br>
ffffffff81a03000 e03000 122000 00 WA 0 0 4096<br>
[31] .brk NOBITS<br>
ffffffff81b25000 e03000 425000 00 WA 0 0 1<br>
[32] .comment PROGBITS 0000000000000000<br>
e03000 000027 01 MS 0 0 1<br>
[33] .debug_frame PROGBITS 0000000000000000<br>
e03028 002560 00 0 0 8<br>
[34] .shstrtab STRTAB<br>
0000000000000000 e05588 00018a 00 0 0 1<br>
[35] .symtab SYMTAB 0000000000000000<br>
e06058 1a29f8 18 36 43659 8<br>
[36] .strtab STRTAB<br>
0000000000000000 fa8a50 180d92 00 0 0 1<br>
Key to Flags:<br>
W (write), A (alloc), X (execute), M (merge), S (strings), l (large)<br>
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)<br>
O (extra OS processing required) o (OS specific), p (processor specific)<br>
<br>
So the vmlinux is loaded in memory like a dd ?<br>
<br>
5) In my function A, inside the module that I wrote, a non-initialized<br>
variable will take place in non-initialized section that was loaded in<br>
memory ?<br>
Or my modules has a new sections for it's own use, and my module is<br>
loaded my memory like a process, with all his sections?<br>
So how another module or kernel code will fin my exported variable/function ?<br>
<br>
<br>
6) Let's suppose:<br>
I have a int variable, with 17 as content, and the address is 0xGGGGGG.<br>
If I stop the linux in this time, read my memory at address 0xGGGGGG I<br>
will got 17, right ?<br>
0xGGGGGGG will be bigger than 0xc0000000 always, right ?<br>
<br>
<br>
7) Now take int from question and change for:<br>
struct mystruct * foo = (struct mystruct* ) kmalloc(sizeof(struct mystruct));<br>
<br>
I will be able to read at address 0xGGGGGG the struct that created,<br>
and it address will be greater than 0xc0000000, right ?<br>
But for this struct, the memory will be allocated for ever, until I<br>
free the pointer, right ?<br>
<br>
<br>
<br>
Well, this just a start. I really want to understand how kernel is<br>
run, loaded etc. Any help is appreciate, answering my questions, links<br>
to read, books to read.<br>
Actually, I didn't find any book with that kind of information .<br>
<br>
<br>
--<br>
Lucas Tanure<br>
+55 (19) 988176559<br>
<br>
--<br>
To unsubscribe, send a message with 'unsubscribe linux-mm' in<br>
the body to <a href="mailto:majordomo@kvack.org">majordomo@kvack.org</a>. For more info on Linux MM,<br>
see: <a href="http://www.linux-mm.org/" target="_blank">http://www.linux-mm.org/</a> .<br>
Don't email: <a href=mailto:"<a href="mailto:dont@kvack.org">dont@kvack.org</a>"> <a href="mailto:email@kvack.org">email@kvack.org</a> </a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Regards,<br>Peter Teoh
</div>