1 首先是 设置 CR0
//读取cr0 寄存器
static inline uint32_t read_cr0() {
uint32_t cr0;
__asm__ __volatile__("mov %%cr0, %[v]":[v]"=r"(cr0));
return cr0;
}
//写 cr0
static inline void write_cr0(uint32_t v) {
__asm__ __volatile__("mov %[v], %%cr0"::[v]"r"(v));
}
然后是使用
//设置cr0 最低位
uint32_t cr0 = read_cr0();
write_cr0(cr0 | (1 << 0));
2 然后是 进行园跳转, 清空流水线
//进行远眺转
static inline void far_jump(uint32_t selector, uint32_t offset) {
uint32_t addr[] = {offset, selector };
__asm__ __volatile__("ljmpl *(%[a])"::[a]"r"(addr));
}
然后是使用
far_jump(8, (uint32_t)protect_mode_entry);
然后还要再 start.s 中做一些设置。
// 32位保护模式下的代码
.code32
.text
.global protect_mode_entry
.extern load_kernel
protect_mode_entry:
jmp .
为了使C语言编译通过,还需要 加上一个函数定义
//仅仅为了使C 函数通过。
void protect_mode_entry (void);