Comments (6)
https://mp.weixin.qq.com/s/SzpN1BNty5aPDZhNdCO5yA
这里我写了一篇文章并配了视频,希望对您有帮助。
from mykernel.
嗯. 抱歉, 我就是看完所有能找到的资料(包括上面的链接)都没法解答才问的.
from mykernel.
问:
- 进程切换时为什么rbp不像rsp那样存成Thread的成员而是通过压栈处理?
其实rip和rsp是CPU现场中最关键的两个,缺一不可,而rbp则不是最关键的,可以通过间接的方式处理,当然像rsp一样处理也可以工作,但为什么有通用的堆栈机制方法不用而采用特例呢?
- 进程切换完成后next进程的rip指向了
"popq %rbp"
, 它是如何接着上次的中断位置继续执行的?
rsp指向next进程的堆栈栈顶,而栈顶恰恰是"popq %rbp"
- 无法理解启动第一个进程的这句
"pushq %1\n\t"
, 因为此时rbp的值应是my_start_kernel的rbp, 这句又没给rbp赋新值, 有何意义?
这一点你要理解函数调用堆栈框架,只是从头开始的进程栈底稍微特殊一点,空栈也就是rsp和rbp相同。
from mykernel.
- 很容易理解了
- 结合反汇编看看, 也理解了
- 还是没明白.
我对函数调用堆栈有基本的概念, 之前自学过一点反汇编. 通常函数开头都有这两句, 就是为了让rsp和rbp相同.
1211: 55 push %rbp
1212: 48 89 e5 mov %rsp,%rbp
我反汇编my_start_kernel的结果是:
...
ffffffff8185f02a: 48 c7 05 53 50 09 00 movq $0xffffffff818f40a0,0x95053(%rip) # ffffffff818f4088 <my_current_task>
ffffffff8185f031: a0 40 8f 81
ffffffff8185f035: 48 8b 0d 74 90 09 00 mov 0x99074(%rip),%rcx # ffffffff818f80b0 <task+0x4010>
ffffffff8185f03c: 48 8b 15 75 90 09 00 mov 0x99075(%rip),%rdx # ffffffff818f80b8 <task+0x4018>
ffffffff8185f043: 48 89 d4 mov %rdx,%rsp # rsp=rdx=task[pid].thread.sp
ffffffff8185f046: 52 push %rdx # pushq %rdx= `subq $8, %rsp` + `movq %rdx, (%rsp)`
ffffffff8185f047: 51 push %rcx
ffffffff8185f048: c3 retq
ffffffff8185f049: 5d pop %rbp
ffffffff8185f04a: 5d pop %rbp
ffffffff8185f04b: c3 retqs
还是没看出push %rdx
与rbp有什么关系.
from mykernel.
- 很容易理解了
- 结合反汇编看看, 也理解了
- 还是没明白.
我对函数调用堆栈有基本的概念, 之前自学过一点反汇编. 通常函数开头都有这两句, 就是为了让rsp和rbp相同.
1211: 55 push %rbp 1212: 48 89 e5 mov %rsp,%rbp
我反汇编my_start_kernel的结果是:
... ffffffff8185f02a: 48 c7 05 53 50 09 00 movq $0xffffffff818f40a0,0x95053(%rip) # ffffffff818f4088 <my_current_task> ffffffff8185f031: a0 40 8f 81 ffffffff8185f035: 48 8b 0d 74 90 09 00 mov 0x99074(%rip),%rcx # ffffffff818f80b0 <task+0x4010> ffffffff8185f03c: 48 8b 15 75 90 09 00 mov 0x99075(%rip),%rdx # ffffffff818f80b8 <task+0x4018> ffffffff8185f043: 48 89 d4 mov %rdx,%rsp # rsp=rdx=task[pid].thread.sp ffffffff8185f046: 52 push %rdx # pushq %rdx= `subq $8, %rsp` + `movq %rdx, (%rsp)` ffffffff8185f047: 51 push %rcx ffffffff8185f048: c3 retq ffffffff8185f049: 5d pop %rbp ffffffff8185f04a: 5d pop %rbp ffffffff8185f04b: c3 retqs
还是没看出
push %rdx
与rbp有什么关系.
rdx应该是task[pid].thread.sp,也就是next进程的rsp,next进程是一个新程序,为空栈
因此这时要压栈rbp,那就直接压栈rsp就好了,这地方只是我自己的简便写法,实际Linux内核中启动一个新程序的做法要复杂的多。
from mykernel.
"空栈也就是rsp和rbp相同", 这我理解, 但mykernel里的这个场景需要我们手动调整, 因为起先rsp和rbp都是my_start_kernel的. 我认为这段内嵌汇编应该实现让rsp与rbp相等. 但实际只是设置了rsp让它等于rdx, push %rdx
后, rbp还是不等于rsp呀, 因此还是有点懵.
from mykernel.
Related Issues (16)
- 进程首次启动问题未解决 HOT 1
- mykernel中进程调度只是堆栈的切换,能否加入代码段的切换 HOT 1
- 是否有视频课程呢 HOT 1
- 5.4内核和3.9.4内核的allnoconfig可能有不同,才导致5.4需要用defconfig HOT 10
- 最新代码有个问题,栈底不会切换 HOT 2
- 允许my_process退出, bzImage会崩溃 HOT 1
- Could not initialize SDL HOT 3
- Where is all the GPRs stored during context switch
- 启动后卡在了Booting from ROM HOT 1
- Issue Title patch 的时候出现 Assignees: Labels: HOT 1
- 孟老师,请问以后会教如何将内核打造为可用的操作系统吗? HOT 1
- Ubuntu22.04版本编译Linux内核5.4.34时遇到兼容性问题解决方案
- patch 生成 HOT 5
- 执行到make的时候会出现报错 HOT 2
- debian 9.5编译linux3.9.4时GCC报错 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mykernel.