实现64位操作系统的问题解决系列:(三)程序4-11 因为__switch_to符号未定义,编译失败
ub16或者ub18上实践田宇大佬的64位操作系统的设计与实现,因为环境的原因,书中提供的代码并不能完全编译成功,前面我写了实现64位操作系统的问题解决系列:(一)和(二)基本编译到程序4-10都不会出问题,但是到程序4-11就会 因为__switch_to符号未定义,编译失败。
问题描述
经过实现64位操作系统的问题解决系列:(一)和(二)
修改代码后,如下
代码1
#define switch_to(prev,next) \
do{ \
__asm__ __volatile__ ( "pushq %%rbp \n\t" \
"pushq %%rax \n\t" \
"movq %%rsp, %0 \n\t" \
"movq %2, %%rsp \n\t" \
"leaq 1f(%%rip), %%rax \n\t" \
"movq %%rax, %1 \n\t" \
"pushq %3 \n\t" \
"jmp __switch_to \n\t" \
"1: \n\t" \
"popq %%rax \n\t" \
"popq %%rbp \n\t" \
:"=m"(prev->thread->rsp),"=m"(prev->thread->rip)\
:"m"(next->thread->rsp),"m"(next->thread->rip),"D"(prev),"S"(next) \
:"memory" \
); \
}while(0)
代码2
static inline void __switch_to(struct task_struct *prev,struct task_struct *next) //最初的代码只有inline
{
init_tss[0].rsp0 = next->thread->rsp0;
set_tss64(init_tss[0].rsp0, init_tss[0].rsp1, init_tss[0].rsp2, init_tss[0].ist1, init_tss[0].ist2, init_tss[0].ist3, init_tss[0].ist4, init_tss[0].ist5, init_tss[0].ist6, init_tss[0].ist7);
__asm__ __volatile__("movq %%fs, %0 \n\t":"=a"(prev->thread->fs));
__asm__ __volatile__("movq %%gs, %0 \n\t":"=a"(prev->thread->gs));
__asm__ __volatile__("movq %0, %%fs \n\t"::"a"(next->thread->fs));
__asm__ __volatile__("movq %0, %%gs \n\t"::"a"(next->thread->gs));
color_printk(WHITE,BLACK,"prev->thread->rsp0:%#018lx\n",prev->thread->rsp0);
color_printk(WHITE,BLACK,"next->thread->rsp0:%#018lx\n",next->thread->rsp0);
}
编译的时候,"jmp __switch_to \n\t" \
,中找不到"__switch_to
这个函数的定义(尽管我们看起来已经定义了他static inline void __switch_to(struct task_struct *prev,struct task_struct *next)
)
折中的解决方案
把static inline void __switch_to(struct task_struct *prev,struct task_struct *next)
函数变成全局函数,让其全局可见。
void __switch_to(struct task_struct *prev,struct task_struct *next)
去掉static inline
便可以编译通过
最终结果
如果有其他问题,留言交流