x86 - how does internal functions of kernel resolve after paging? - Stack Overflow
I was recently learning about kernel developement where I came across the concept of higher half kernels, until now I used to think that entire kernel must be 1:1 mapped after paging, but it seems that's not the case, which brings me to my question. As after linking, all the function calls/jumps are just hardcoded addresses (i think), how does kernel resolve those addresses as they point to physical memory? Or are those addresses after linking virtual? If so how does the linker script figure out what will be the virtual address for a function? Also how can the kernel be even loaded if those virtual addresses (that gets replaced while linking) are different then physical address?
I was recently learning about kernel developement where I came across the concept of higher half kernels, until now I used to think that entire kernel must be 1:1 mapped after paging, but it seems that's not the case, which brings me to my question. As after linking, all the function calls/jumps are just hardcoded addresses (i think), how does kernel resolve those addresses as they point to physical memory? Or are those addresses after linking virtual? If so how does the linker script figure out what will be the virtual address for a function? Also how can the kernel be even loaded if those virtual addresses (that gets replaced while linking) are different then physical address?
Share Improve this question asked 23 hours ago Hououin_kyoumaHououin_kyouma 293 bronze badges1 Answer
Reset to default 0x86 calls/jumps use relative addressing (like jmp rel32
does RIP += sign_extend(rel32)
), so are position-independent. Only absolute addresses like pointers (data and function) would need to be fixed up when the kernel is relocated if you want them to work from both virtual addresses.
If your bootloader jumps to your kernel entry point at a virtual address that isn't what you want, you can map the desired virt addresses to the same physical pages you're currently executing from, then jump there. (It's fine to have multiple virtual mappings reference the same physical page; x86 CPU caches are required to handle that without corrupting anything.)
If your whole kernel isn't position-independent, a sensible design would be to have some code in the kernel entry point which sets up mappings before the main part of the kernel runs at all. This special part of the kernel could be hand-written in asm, or compiled as position-independent.
Since the rest of the kernel will only run from one virtual address, you can just tell the linker to link your kernel at e.g. 1 or 2GiB below the end of virtual address-space. (gcc -mcmodel=kernel
, like non-PIE Linux kernels used to use, so absolute addresses can be used as sign-extended 32-bit immediates for stuff like mov eax, [array + rdi*4]
)
You'd need some mechanism for the early-boot part of the kernel to tell the main kernel which physical memory it used for the page table, and which physical pages are holding the kernel's code+data+stack.
- 移动互联网迅猛发展连接人与服务成为重要趋势
- 忘记安卓电脑 ChromeWin双系统PC更有前途
- 软件定义存储VS硬件定义存储
- 微软推Surface平板 成硬件合作伙伴竞争对手
- 奇虎诉腾讯索赔1.5亿创天价 双方股价昨齐上涨
- python - Pipenv not working after Debian system upgrade--maybe partial uninstall? - Stack Overflow
- c++ - OpenCV Build Fails with "FilesNVIDIA.obj" Error in CMake and Visual Studio - Stack Overflow
- java - mac update sequoia 15.1 or 15.2 not work UniversalJavaApplicationStub - Stack Overflow
- csv - How to Handle Clob Data Type in Excel When Exporting Data From DB2 Database - Stack Overflow
- html - Disable scrolling. Overflow:hidden not working - Stack Overflow
- rust - Why is the compiler asking for Sized, when I already added it? - Stack Overflow
- matlab - How do I update pixelClassificationLayer() to a custom loss function? - Stack Overflow
- php - Laravel Defer on API requests - Stack Overflow
- algorithmic trading - Why is my python script working sometimes but not others? There seems to be a delay for it to work - Stack
- c++ - Vscode doesn't pass args to program when debugging - Stack Overflow
- GHC unable to find mingwinclude directory on Windows - Stack Overflow
- reactjs - React and Electron application suggesting I'm using invalid hook calls - Stack Overflow