基于mykernel 2.0编写一个操作系统内核
Posted zhouzhouL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于mykernel 2.0编写一个操作系统内核相关的知识,希望对你有一定的参考价值。
一、配置mykernel 2.0,熟悉Linux内核的编译
在ubuntu下运行如下命令
wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch sudo apt install axel axel -n 20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz xz -d linux-5.4.34.tar.xz tar -xvf linux-5.4.34.tar cd linux-5.4.34 patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev make defconfig # Default configuration is based on \'x86_64_defconfig\' make -j$(nproc) sudo apt install qemu # install QEMU qemu-system-x86_64 -kernel arch/x86/boot/bzImage
执行成功后,会跳出如下界面, 从qemu窗口中您可以看到my_start_kernel在执行, 同时my_timer_handler时钟中断处理程序周期性执行
二、基于mykernel 2.0编写一个操作系统内核
在mymain.c基础上继续写进程描述PCB和进程链表管理等代码,在myinterrupt.c的基础上完成进程切换代码,实现一个小的可运行的操作系统内核
参考: https://github.com/mengning/mykernel
将git上提供的mymain.c和myinterrupt.c替换掉本地的文件内容, 再添加一个mypcb.h文件,定义进程控制块管理进程, 完成后执行make命令重新编译,
再执行qemu-system-x86_64 -kernel arch/x86/boot/bzImage命令即可
三、操作系统内核核心功能及运行工作机制
操作系统核心功能主要有: 进程管理, 设备管理,文件管理,处理器管理,存储器管理,
本次实验更深入了解进程结构以及进程切换机制
分析如下代码,理解进程切换运行工作机制:
/* linux/mykernel/mymain.c
* Kernel internal my_start_kernel
* Change IA32 to x86-64 arch, 2020/4/26
* Copyright (C) 2013, 2020 Mengning
*/
#include <linux/types.h> #include <linux/string.h> #include <linux/ctype.h> #include <linux/tty.h> #include <linux/vmalloc.h> #include "mypcb.h" tPCB task[MAX_TASK_NUM]; tPCB * my_current_task = NULL; volatile int my_need_sched = 0; void my_process(void); void __init my_start_kernel(void) // mykernel内核代码的⼊⼝ { int pid = 0; // 初始进程时进程号为0的进程 int i;
// 初始化该进程的信息 /* Initialize process 0*/ task[pid].pid = pid; task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; task[pid].next = &task[pid];
// 构建进程的循环单链表,将各个进程连接起来 /*fork more process */ for(i=1;i<MAX_TASK_NUM;i++) { memcpy(&task[i],&task[0],sizeof(tPCB)); task[i].pid = i; task[i].thread.sp = (unsigned long)(&task[i].stack[KERNEL_STACK_SIZE-1]); task[i].next = task[i-1].next; task[i-1].next = &task[i]; } /* start process 0 by task[0] */ pid = 0; my_current_task = &task[pid];
// 插入编译代码,启动首进程 asm volatile(
// 将初始进场的堆栈栈顶地址放入rsp寄存器里 "movq %1,%%rsp\\n\\t" /* set task[pid].thread.sp to rsp */ "pushq %1\\n\\t" /* push rbp */ "pushq %0\\n\\t" /* push task[pid].thread.ip */ "ret\\n\\t" /* pop task[pid].thread.ip to rip */ : : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ ); } int i = 0; void my_process(void) {
while(1) { i++; if(i%10000000 == 0) { printk(KERN_NOTICE "this is process %d -\\n",my_current_task->pid);
// 检测my_need_sched, 当变为1时,做进程切换
if(my_need_sched == 1) {
my_need_sched = 0;
my_schedule();
}
printk(KERN_NOTICE "this is process %d +\\n",my_current_task->pid);
}
}
}
/* * linux/mykernel/myinterrupt.c * * Kernel internal my_timer_handler * Change IA32 to x86-64 arch, 2020/4/26 * * Copyright (C) 2013, 2020 Mengning * */ #include <linux/types.h> #include <linux/string.h> #include <linux/ctype.h> #include <linux/tty.h> #include <linux/vmalloc.h> #include "mypcb.h" extern tPCB task[MAX_TASK_NUM]; extern tPCB * my_current_task; extern volatile int my_need_sched; volatile int time_count = 0; /* * Called by timer interrupt. * it runs in the name of current running process, * so it use kernel stack of current running process */ void my_timer_handler(void) // 时钟中断处理函数 { if(time_count%1000 == 0 && my_need_sched != 1) { printk(KERN_NOTICE ">>>my_timer_handler here<<<\\n"); my_need_sched = 1; } time_count ++ ; return; } void my_schedule(void) // 进程切换处理函数 { tPCB * next; tPCB * prev; if(my_current_task == NULL || my_current_task->next == NULL) { return; } printk(KERN_NOTICE ">>>my_schedule<<<\\n"); /* schedule */ next = my_current_task->next; // 下一个要进程 prev = my_current_task; // 指向当前进程 if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ // 判断下一个进程的状态是否为可执行 { my_current_task = next; // printk(KERN_NOTICE ">>>switch %d to %d<<<\\n",prev->pid,next->pid);
// 插入编译代码, 进行进程上下文的切换 /* switch to next process */ asm volatile(
// 保存上一个进程的rbp和rsp "pushq %%rbp\\n\\t" /* save rbp of prev */ "movq %%rsp,%0\\n\\t" /* save rsp of prev */
// rsp指向next->thread.sp下一个进程栈 "movq %2,%%rsp\\n\\t" /* restore rsp of next */
// 保存上一个进程的rip "movq $1f,%1\\n\\t" /* save rip of prev */
// 当前rip指向下一个进程的地址 "pushq %3\\n\\t" "ret\\n\\t" /* restore rip of next */
// rbp指向下一个进程的栈底 "1:\\t" /* next process start here */ "popq %%rbp\\n\\t" : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); } return; }
/* * linux/mykernel/mypcb.h * Kernel internal PCB types * Copyright (C) 2013 Mengning */ #define MAX_TASK_NUM 4 // 进程数量 #define KERNEL_STACK_SIZE 1024*2 // 栈大小 /* CPU-specific state of this task */ struct Thread { unsigned long ip; unsigned long sp; }; typedef struct PCB{ int pid; // 进程号 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ // 当前进程状态 unsigned long stack[KERNEL_STACK_SIZE]; /* CPU-specific state of this task */ struct Thread thread; // 线程 unsigned long task_entry; // 进程下执行的线程 struct PCB *next; // 下一个PCB, 所有进程的PCB以链表结构相连 }tPCB; void my_schedule(void);
以上是关于基于mykernel 2.0编写一个操作系统内核的主要内容,如果未能解决你的问题,请参考以下文章