文章目录
相关查阅博客链接
本书中错误勘误
1、这可能不算是错误 只是有几个数据没有写上去default_prio
我在global.h
里面赋值了31
定义并初始化TSS
本书中 和 现代的操作系统Linux都没有采用硬件厂商提供的那种切换进程直接换TSS的操作 因为那样的话代价太大了 这里的话我们先把TSS基本的数据结构 和 用户进程DPL3的GDT符给弄好
因为在之前我记得是弄过了GDT了 只不过那个时候只弄了内核的描述符 而且那个时候是用汇编弄得 这里就用c语言来弄吧
废话少说 我睡午觉前先把代码和调试弄好了 睡醒了就来写博客了
修改后的global.h
#ifndef __KERNEL_GLOBAL_H
#define __KERNEL_GLOBAL_H
#include "stdint.h"
#define RPL0 0
#define RPL1 1
#define RPL2 2
#define RPL3 3
#define TI_GDT 0
#define TI_LDT 1
//-------------- GDT描述符属性 ------------
#define DESC_G_4K 1
#define DESC_D_32 1
#define DESC_L 0
#define DESC_AVL 0
#define DESC_P 1
#define DESC_DPL_0 0
#define DESC_DPL_1 1
#define DESC_DPL_2 2
#define DESC_DPL_3 3
#define DESC_S_CODE 1
#define DESC_S_DATA DESC_S_CODE
#define DESC_S_SYS 0
#define DESC_TYPE_CODE 8
#define DESC_TYPE_DATA 2
#define DESC_TYPE_TSS 9
/*KERNEL段*/
#define SELECTOR_K_CODE ((1 << 3) + (TI_GDT << 2) + RPL0)
#define SELECTOR_K_DATA ((2 << 3) + (TI_GDT << 2) + RPL0)
#define SELECTOR_K_STACK SELECTOR_K_DATA
#define SELECTOR_K_GS ((3 << 3) + (TI_GDT << 2) + RPL0)
/*这里是TSS*/
/*USER段*/
#define SELECTOR_U_CODE ((5 << 3) + (TI_GDT << 2) + RPL3)
#define SELECTOR_U_DATA ((6 << 3) + (TI_GDT << 2) + RPL3)
#define SELECTOR_U_STACK SELECTOR_U_DATA
#define GDT_ATTR_HIGH ((DESC_G_4K << 7) + (DESC_D_32 << 6) + (DESC_L << 5) + (DESC_AVL << 4))
#define GDT_CODE_ATTR_LOW_DPL3 ((DESC_P << 7) + (DESC_DPL_3 << 5) + (DESC_S_CODE << 4) + DESC_TYPE_CODE)
#define GDT_DATA_ATTR_LOW_DPL3 ((DESC_P << 7) + (DESC_DPL_3 << 5) + (DESC_S_DATA << 4) + DESC_TYPE_DATA)
//-------------- IDT描述符属性 ------------
#define IDT_DESC_P 1
#define IDT_DESC_DPL0 0
#define IDT_DESC_DPL3 3
#define IDT_DESC_32_TYPE 0xE // 32位的门
#define IDT_DESC_16_TYPE 0x6 // 16位的门,不用,定义它只为和32位门区分
#define IDT_DESC_ATTR_DPL0 ((IDT_DESC_P << 7) + (IDT_DESC_DPL0 << 5) + IDT_DESC_32_TYPE)
#define IDT_DESC_ATTR_DPL3 ((IDT_DESC_P << 7) + (IDT_DESC_DPL3 << 5) + IDT_DESC_32_TYPE)
//-------------- TSS描述符属性 ------------
#define TSS_DESC_D 0
#define TSS_ATTR_HIGH ((DESC_G_4K << 7) + (TSS_DESC_D << 6) + (DESC_L << 5) + (DESC_AVL << 4) + 0X0)
#define TSS_ATTR_LOW ((DESC_P << 7) + (DESC_DPL_0 << 5) + (DESC_S_SYS << 4) + DESC_TYPE_TSS)
#define SELECTOR_TSS ((4 << 3) + (TI_GDT << 2) + RPL0)
/*描述符结构*/
struct gdt_desc
{
uint16_t limit_low_word;
uint16_t base_low_word;
uint8_t base_mid_byte;
uint8_t attr_low_byte;
uint8_t limit_high_attr_high;
uint8_t base_high_byte;
};
#endif
编写后的tss.c
注意一下 这里面由于原来的任性 我把GDT位置设置成了0x600
又因为有些部分是自己写的代码 GDT
实际的内存位置是0xc0000603
我先调用了info gdt
看了看base = 0xc0000603
是哈 确实是那么多 那下面的代码相关GDT
内存位置也要修改一下
#include "tss.h"
#include "global.h"
#include "../thread/thread.h"
#include "print.h"
#include "string.h"
struct tss
{
uint32_t backlink;
uint32_t* esp0;
uint32_t ss0;
uint32_t* esp1;
uint32_t ss1;
uint32_t* esp2;
uint32_t ss2;
uint32_t cr3;
uint32_t (*eip) (void);
uint32_t eflags;
uint32_t eax;
uint32_t ecx;
uint32_t edx;
uint32_t ebx;
uint32_t esp;
uint32_t ebp;
uint32_t esi;
uint32_t edi;
uint32_t es;
uint32_t cs;
uint32_t ss;
uint32_t ds;
uint32_t fs;
uint32_t gs;
uint32_t ldt;
uint32_t trace;
uint32_t io_base;
};
struct tss tss;
//内核栈都是位于 pcb头顶一页的地方
void updata_tss_esp(struct task_struct* pthread)
{
tss.esp0 = (uint32_t*)((uint32_t)pthread + PG_SIZE);
}
struct gdt_desc make_gdt_desc(uint32_t* desc_addr,uint32_t limit,uint8_t attr_low,uint8_t attr_high)
{
struct gdt_desc desc;
uint32_t desc_base = (uint32_t) desc_addr;
desc.limit_low_word = limit & 0x0000ffff;
desc.base_low_word = desc_base & 0x0000ffff;
desc.base_mid_byte = ((desc_base & 0x00ff0000) >> 16);
desc.attr_low_byte = (uint8_t)(attr_low);
desc.limit_high_attr_high = (((limit & 0x000f0000) >> 16) + (uint8_t)(attr_high));
desc.base_high_byte = desc_base >> 24;
return desc;
}
void tss_init()
{
put_str("tss_init start\n");
uint32_t tss_size = sizeof(tss);
memset(&tss,0,tss_size);
tss.ss0 = SELECTOR_K_STACK;
tss.io_base = tss_size;
//之前的gdt 个性使然 放在了0xc0000603的位置
*((struct gdt_desc*)0xc0000623) = make_gdt_desc((uint32_t*)&tss,tss_size-1,TSS_ATTR_LOW,TSS_ATTR_HIGH);
*((struct gdt_desc*)0xc000062b) = make_gdt_desc((uint32_t*)0,0xfffff,GDT_CODE_ATTR_LOW_DPL3,\
GDT_ATTR_HIGH);
*((struct gdt_desc*)0xc0000633) = make_gdt_desc((uint32_t*)0,0xfffff,GDT_DATA_ATTR_LOW_DPL3,\
GDT_ATTR_HIGH);
uint64_t gdt_operand = \
((8*7 - 1) | ((uint64_t)(uint32_t)0xc0000603 << 16));
asm volatile("lgdt %0" :: "m"(gdt_operand));
asm volatile("ltr %w0" :: "r"(SELECTOR_TSS));
put_str("tss_init and ltr done\n");
}
编写后的tss.h
#ifndef __USERPROG_TSS_H
#define __USERPROG_TSS_H
#include "../thread/thread.h"
void updata_tss_esp(struct task_struct* pthread);
struct gdt_desc make_gdt_desc(uint32_t* desc_addr,uint32_t limit,uint8_t attr_low,uint8_t attr_high);
void tss_init(void);
#endif
修改后的init.c
#include "init.h"
#include "print.h"
#include "interrupt.h"
#include "../device/timer.h"
#include "memory.h"
#include "../thread/thread.h"
#include "../device/console.h"
#include "../device/keyboard.h"
#include "../userprog/tss.h"
/*负责初始化所有模块 */
void init_all() {
put_str("init_all\n");
idt_init(); // 初始化中断
mem_init();
timer_init();
thread_init();
console_init();
keyboard_init();
tss_init();
}
修改后的MakeFile
BUILD_DIR = ./build
ENTRY_POINT = 0xc0001500
AS = nasm
CC = gcc
LD = ld
LIB = -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/
ASFLAGS = -f elf
CFLAGS = -Wall -m32 -fno-stack-protector $(LIB) -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS = -m elf_i386 -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o \
$(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o $(BUILD_DIR)/switch.o \
$(BUILD_DIR)/debug.o $(BUILD_DIR)/string.o $(BUILD_DIR)/memory.o \
$(BUILD_DIR)/bitmap.o $(BUILD_DIR)/thread.o $(BUILD_DIR)/list.o \
$(BUILD_DIR)/sync.o $(BUILD_DIR)/console.o $(BUILD_DIR)/keyboard.o \
$(BUILD_DIR)/ioqueue.o $(BUILD_DIR)/tss.o
############## c代码编译 ###############
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h \
lib/stdint.h kernel/init.h lib/string.h kernel/memory.h \
thread/thread.h kernel/interrupt.h device/console.h \
device/keyboard.h device/ioqueue.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/init.o: kernel/init.c kernel/init.h lib/kernel/print.h \
lib/stdint.h kernel/interrupt.h device/timer.h kernel/memory.h \
thread/thread.h device/console.h device/keyboard.h userprog/tss.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/interrupt.o: kernel/interrupt.c kernel/interrupt.h \
lib/stdint.h kernel/global.h lib/kernel/io.h lib/kernel/print.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/timer.o: device/timer.c device/timer.h lib/kernel/io.h lib/kernel/print.h \
kernel/interrupt.h thread/thread.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/debug.o: kernel/debug.c kernel/debug.h \
lib/kernel/print.h lib/stdint.h kernel/interrupt.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/string.o: lib/string.c lib/string.h \
kernel/debug.h kernel/global.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/memory.o: kernel/memory.c kernel/memory.h \
lib/stdint.h lib/kernel/bitmap.h kernel/debug.h lib/string.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/bitmap.o: lib/kernel/bitmap.c lib/kernel/bitmap.h kernel/global.h \
lib/string.h kernel/interrupt.h lib/kernel/print.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/thread.o: thread/thread.c thread/thread.h \
lib/stdint.h lib/string.h kernel/global.h kernel/memory.h \
kernel/debug.h kernel/interrupt.h lib/kernel/print.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/list.o: lib/kernel/list.c lib/kernel/list.h \
kernel/interrupt.h lib/stdint.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/sync.o: thread/sync.c thread/sync.h \
lib/stdint.h thread/thread.h kernel/debug.h kernel/interrupt.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/console.o: device/console.c device/console.h \
lib/kernel/print.h thread/sync.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/keyboard.o: device/keyboard.c device/keyboard.h \
lib/kernel/print.h lib/kernel/io.h kernel/interrupt.h \
kernel/global.h lib/stdint.h device/ioqueue.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/ioqueue.o: device/ioqueue.c device/ioqueue.h \
kernel/interrupt.h kernel/global.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/tss.o: userprog/tss.c userprog/tss.h \
kernel/global.h thread/thread.h lib/kernel/print.h
$(CC) $(CFLAGS) $< -o $@
############## 汇编代码编译 ###############
$(BUILD_DIR)/kernel.o: kernel/kernel.S
$(AS) $(ASFLAGS) $< -o $@
$(BUILD_DIR)/print.o: lib/kernel/print.S
$(AS) $(ASFLAGS) $< -o $@
$(BUILD_DIR)/switch.o: thread/switch.S
$(AS) $(ASFLAGS) $< -o $@
############## 链接所有目标文件 #############
$(BUILD_DIR)/kernel.bin: $(OBJS)
$(LD) $(LDFLAGS) $^ -o $@
.PHONY : mk_dir hd clean all
mk_dir:
if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi
hd:
dd if=$(BUILD_DIR)/kernel.bin \
of=/home/cooiboi/bochs/hd60M.img \
bs=512 count=200 seek=9 conv=notrunc
clean:
cd $(BUILD_DIR) && rm -f ./*
build: $(BUILD_DIR)/kernel.bin
all: mk_dir build hd
make all 验证成果
这部分还是老话常说哈make all
bin/bochs -f bochsrc.disk
看哈效果 info gdt
ok了家人们 进入下一部分了昂
实现用户进程
好啦 在编辑这篇博客到这里的时候 已经是和上面编辑的部分的第二天了
其实每一章如果内容相对没有那么多 人相对没有那么懒得时候 我是比较喜欢一天写完一章的 因为比较连续 而且我每天基本上都会给自己立一个Flag 比如今天之前必须做完 如果太多了的话 两天就要完成一章
不知不觉已经到了十一章了 其实昨晚我就完成的差不多了 但是因为人吃完晚饭就想睡觉 而且感觉昨天对进程的创建的整体细节把控的不是很到位
如果我对自己写的博客这部分的内容自己连个轮廓 一些很重要的地方细节都把握不住的话 我自己写博客都肯定不好意思 所以还是打算睡一觉 第二天早上起来精精神神的理一下思路 每个地方都去看看是怎么回事 调试好 能把自己说服再来写这篇博客
我觉得这部分看书 还是看文字很多时候看的脑袋晕晕的
多说无益 肯定是看整体的代码 切入进去才有个整体的概念
而且到这里下面写一下 创建进程切换的思路 和之前内存分配的一些没有体会到的地方
进程相关的内存分配理解
内存分配的理解
原来我还没有理解到 这里说一下
我们的内存分配是 内核有一个专门的虚拟内存管理池 而进程是每个进程都有一个专门的虚拟内存管理池
物理内存的话 还是只有两个 内核物理内存池 和 用户物理内存池
怎么保证每个进程中操作系统都是可见的 因为每个进程都有一个单独的页表
单独的页表里面共有的部分是内核部分的页目录项 操作系统创建好后的内存地址就是固定的 这样的话 操作系统对于每个进程都是可见的
怎么保证进程的独立空间呢 大家都用相同的虚拟地址怎么才能不冲突呢 之前说过的的每个进程独享一个页表 那么相同的虚拟页目录项相对应的物理地址是可以不同的 分配是由操作系统来做的
但是为什么进程每个都还要一个独用的虚拟内存管理池呢 因为对于一个进程里面的资源 是不可以用相同的虚拟地址 里面的地址都是唯一的
上面部分就是关于相关进程有关的内存分配的理解了 我觉得应该还是没有什么问题的
进程相关的创建进程理解
创建进程的理解
等这部分笼统的说完后 下面就直接放代码咯
反正代码的部分很多地方我都还是打了注释
创建进程大概的流程是
1、先在内核中分配一页内存 为线程thread
分配一页pcb
2、初始化线程后 为我们的进程的虚拟内存位图分配内存
3、创造线程 此线程为了调用start_process
初始化函数
(备注 start_process
中把进程的环境给初始化好 即可调用intr_exit iretd
跑路 当然这是后话)
4、紧接着把进程的页表空间分配好 并且初始化好
5、把含有start_process
的线程放到就绪列表中 就等着被调用了
6、线程schedule
调用后 发现pgdir
非空 换页表重新装载 并调用start_process
7、start_process
做了一大堆事情 并把分配了一页内存给栈空间 之后就jm intr_exit
切换去了
8、各种pop
之后 cs
ip
ds
各种寄存器已经被切换了 进入用户进程
9、切换进程时 只需要把TSS
中的esp0
切换即可 即内核线程栈
好了 写了那么多了 总得来说 轮廓就已经是这样了 接下来一步步跟着写代码即可 如果我以后忘记了进程是怎么切换的 创建的还可以回来看看 哈哈 挺好的 下面直接放代码咯
修改后的thread.h
就增加一条struct virtual_addr userprog_vaddr;
为了方便还是全部代码给完
#ifndef __THREAD_THREAD_H
#define __THREAD_THREAD_H
#include "stdint.h"
#include "list.h"
#include "../kernel/memory.h"
#define PG_SIZE 4096
extern struct list thread_ready_list,thread_all_list;
typedef void thread_func(void*); //这里有点不懂定义的什么意思 搜了搜博客 发现是函数声明
enum task_status
{
TASK_RUNNING, // 0
TASK_READY, // 1
TASK_BLOCKED, // 2
TASK_WAITING, // 3
TASK_HANGING, // 4
TASK_DIED // 5
};
/* intr_stack 用于处理中断被切换的上下文环境储存 */
/* 这里我又去查了一下 为什么是反着的 越在后面的参数 地址越高 */
struct intr_stack
{
uint32_t vec_no; //中断号
uint32_t edi;
uint32_t esi;
uint32_t ebp;
uint32_t esp_dummy;
uint32_t ebx;
uint32_t edx;
uint32_t ecx;
uint32_t eax;
uint32_t gs;
uint32_t fs;
uint32_t es;
uint32_t ds;
uint32_t err_code;
void (*eip) (void); //这里声明了一个函数指针
uint32_t cs;
uint32_t eflags;
void* esp;
uint32_t ss;
};
/* 线程栈 保护线程环境 */
struct thread_stack
{
uint32_t ebp;
uint32_t ebx;
uint32_t edi;
uint32_t esi;
void (*eip) (thread_func* func,void* func_arg); //和下面的相互照应 以ret 汇编代码进入kernel_thread函数调用
void (*unused_retaddr); //占位数 在栈顶站住了返回地址的位置 因为是汇编ret
thread_func* function; //进入kernel_thread要调用的函数地址
void* func_arg; //参数指针
};
struct task_struct
{
uint32_t* self_kstack; //pcb中的 kernel_stack 内核栈
enum task_status status; //线程状态
uint8_t priority; //特权级
uint8_t ticks; //在cpu 运行的滴答数 看ticks 来判断是否用完了时间片
uint32_t elapsed_ticks; //一共执行了多久
char name[16];
struct list_elem general_tag; //就绪队列中的连接节点
struct list_elem all_list_tag; //总队列的连接节点
uint32_t* pgdir; //进程自己页表的虚拟地址 线程没有
struct virtual_addr userprog_vaddr; //用户进程的虚拟空间
uint32_t stack_magic; //越界检查 因为我们pcb上面的就是我们要用的栈了 到时候还要越界检查
};
struct task_struct* running_thread(void);
void kernel_thread(thread_func* function,void* func_arg);
void thread_create(struct task_struct* pthread,thread_func function,void* func_arg);
void init_thread(struct task_struct* pthread,char* name,int prio);
struct task_struct* thread_start(char* name,int prio,thread_func function,void* func_arg);
void make_main_thread(void);
void schedule(void);
void thread_init(void);
void thread_block(enum task_status stat);
void thread_unblock(struct task_struct* pthread);
#endif
修改后的global.h
里面还加了#define default_prio 31
#ifndef __KERNEL_GLOBAL_H
#define __KERNEL_GLOBAL_H
#include "stdint.h"
#define RPL0 0
#define RPL1 1
#define RPL2 2
#define RPL3 3
#define TI_GDT 0
#define TI_LDT 1
#define EFLAGS_MBS (1 << 1)
#define EFLAGS_IF_1 (1 << 9)
#define EFLAGS_IF_0 0
#define EFLAGS_IOPL_3 (3 << 12)
#define EFLAGS_IOPL_0 (0 << 12)
#define DIV_ROUND_UP(X,STEP) ((X + STEP - 1) / STEP)
#define default_prio 31
#define USER_STACK3_VADDR (0xc0000000 - 0x1000)
//-------------- GDT描述符属性 ------------
#define DESC_G_4K 1
#define DESC_D_32 1
#define DESC_L 0
#define DESC_AVL 0
#define DESC_P 1
#define DESC_DPL_0 0
#define DESC_DPL_1 1
#define DESC_DPL_2 2
#define DESC_DPL_3 3
#define DESC_S_CODE 1
#define DESC_S_DATA DESC_S_CODE
#define DESC_S_SYS 0
#define DESC_TYPE_CODE 8
#define DESC_TYPE_DATA 2
#define DESC_TYPE_TSS 9
/*KERNEL段*/
#define SELECTOR_K_CODE ((1 << 3) + (TI_GDT << 2) + RPL0)
#define SELECTOR_K_DATA ((2 << 3) + (TI_GDT << 2) + RPL0)
#define SELECTOR_K_STACK SELECTOR_K_DATA
#define SELECTOR_K_GS ((3 << 3) + (TI_GDT << 2) + RPL0)
/*这里是TSS*/
/*USER段*/
#define SELECTOR_U_CODE ((5 << 3) + (TI_GDT << 2) + RPL3)
#define SELECTOR_U_DATA ((6 << 3) + (TI_GDT << 2) + RPL3)
#define SELECTOR_U_STACK SELECTOR_U_DATA
#define GDT_ATTR_HIGH ((DESC_G_4K << 7) + (DESC_D_32 << 6) + (DESC_L << 5) + (DESC_AVL << 4))
#define GDT_CODE_ATTR_LOW_DPL3 ((DESC_P << 7) + (DESC_DPL_3 << 5) + (DESC_S_CODE << 4) + DESC_TYPE_CODE)
#define GDT_DATA_ATTR_LOW_DPL3 ((DESC_P << 7) + (DESC_DPL_3 << 5) + (DESC_S_DATA << 4) + DESC_TYPE_DATA)
//-------------- IDT描述符属性 ------------
#define IDT_DESC_P 1
#define IDT_DESC_DPL0 0
#define IDT_DESC_DPL3 3
#define IDT_DESC_32_TYPE 0xE // 32位的门
#define IDT_DESC_16_TYPE 0x6 // 16位的门,不用,定义它只为和32位门区分
#define IDT_DESC_ATTR_DPL0 ((IDT_DESC_P << 7) + (IDT_DESC_DPL0 << 5) + IDT_DESC_32_TYPE)
#define IDT_DESC_ATTR_DPL3 ((IDT_DESC_P << 7) + (IDT_DESC_DPL3 << 5) + IDT_DESC_32_TYPE)
//-------------- TSS描述符属性 ------------
#define TSS_DESC_D 0
#define TSS_ATTR_HIGH ((DESC_G_4K << 7) + (TSS_DESC_D << 6) + (DESC_L << 5) + (DESC_AVL << 4) + 0X0)
#define TSS_ATTR_LOW ((DESC_P << 7) + (DESC_DPL_0 << 5) + (DESC_S_SYS << 4) + DESC_TYPE_TSS)
#define SELECTOR_TSS ((4 << 3) + (TI_GDT << 2) + RPL0)
/*描述符结构*/
struct gdt_desc
{
uint16_t limit_low_word;
uint16_t base_low_word;
uint8_t base_mid_byte;
uint8_t attr_low_byte;
uint8_t limit_high_attr_high;
uint8_t base_high_byte;
};
#endif
修改后的memory.c
#include "memory.h"
#include "stdint.h"
#include "print.h"
#include "bitmap.h"
#include "debug.h"
#include "string.h"
#include "../thread/sync.h"
#include "../thread/thread.h"
#define PG_SIZE 4096
#define MEM_BITMAP_BASE 0Xc009a000 //位图开始存放的位置
#define K_HEAP_START 0xc0100000 //内核栈起始位置
#define PDE_IDX(addr) ((addr & 0xffc00000) >> 22)
#define PTE_IDX(addr) ((addr & 0x003ff000) >> 12)
struct pool
{
struct bitmap pool_bitmap; //位图来管理内存使用
uint32_t phy_addr_start; //内存池开始的起始地址
uint32_t pool_size; //池容量
struct lock lock;
};
struct pool kernel_pool ,user_pool; //生成内核内存池 和 用户内存池
struct virtual_addr kernel_vaddr; //内核虚拟内存管理池
void* vaddr_get(enum pool_flags pf,uint32_t pg_cnt)
{
int vaddr_start = 0,bit_idx_start = -1;
uint32_t cnt = 0;
if(pf == PF_KERNEL)
{
bit_idx_start = bitmap_scan(&kernel_vaddr.vaddr_bitmap,pg_cnt);
if(bit_idx_start == -1) return NULL;
while(cnt < pg_cnt)
bitmap_set(&kernel_vaddr.vaddr_bitmap,bit_idx_start + (cnt++),1);
vaddr_start = kernel_vaddr.vaddr_start + bit_idx_start * PG_SIZE;
}
else
{
struct task_struct* cur = running_thread();
bit_idx_start = bitmap_scan(&cur->userprog_vaddr.vaddr_bitmap,pg_cnt);
if(bit_idx_start == -1) return NULL;
while(cnt < pg_cnt)
bitmap_set(&cur->userprog_vaddr.vaddr_bitmap,bit_idx_start + (cnt++),1);
vaddr_start = cur->userprog_vaddr.vaddr_start + bit_idx_start * PG_SIZE;
ASSERT((uint32_t)vaddr_start < (0xc0000000 - PG_SIZE));
}
return (void*)vaddr_start;
}
uint32_t* pte_ptr(uint32_t vaddr)
{
uint32_t* pte = (uint32_t*)(0xffc00000 + ((vaddr & 0xffc00000) >> 10) + PTE_IDX(vaddr) * 4);
return pte;
}
uint32_t* pde_ptr(uint32_t vaddr)
{
uint32_t* pde = (uint32_t*) ((0xfffff000) + PDE_IDX(vaddr) * 4);
return pde;
}
void* palloc(struct pool* m_pool)
{
int bit_idx = bitmap_scan(&m_pool->pool_bitmap,1);
if(bit_idx == -1) return NULL;
bitmap_set(&m_pool->pool_bitmap,bit_idx,1);
uint32_t page_phyaddr = ((bit_idx * PG_SIZE) + m_pool->phy_addr_start);
return (void*)page_phyaddr;
}
void page_table_add(void* _vaddr,void* _page_phyaddr)
{
uint32_t vaddr = (uint32_t)_vaddr,page_phyaddr = (uint32_t)_page_phyaddr;
uint32_t* pde = pde_ptr(vaddr);
uint32_t* pte = pte_ptr(vaddr);
if(*pde & 0x00000001)
{
ASSERT(!(*pte & 0x00000001));
if(!(*pte & 0x00000001))
*pte = (page_phyaddr | PG_US_U | PG_RW_W | PG_P_1);
else
{
PANIC("pte repeat");
*pte = (page_phyaddr | PG_US_U | PG_RW_W | PG_P_1);
}
}
else
{
uint32_t pde_phyaddr = (uint32_t)palloc(&kernel_pool);
*pde = (pde_phyaddr | PG_US_U | PG_RW_W | PG_P_1);
memset((void*)((int)pte & 0xfffff000),0,PG_SIZE);
ASSERT(!(*pte & 0x00000001));
*pte = (page_phyaddr | PG_US_U | PG_RW_W | PG_P_1);
}
return;
}
void* malloc_page(enum pool_flags pf,uint32_t pg_cnt)
{
ASSERT(pg_cnt > 0 && pg_cnt < 3840);
void* vaddr_start = vaddr_get(pf,pg_cnt);
if(vaddr_start == NULL) return NULL;
uint32_t vaddr = (uint32_t)vaddr_start,cnt = pg_cnt;
struct pool* mem_pool = pf & PF_KERNEL ? &kernel_pool : &user_pool;
while(cnt-- > 0)
{
void* page_phyaddr = palloc(mem_pool);
if(page_phyaddr == NULL) return NULL;
page_table_add((void*)vaddr,page_phyaddr);
vaddr += PG_SIZE;
}
return vaddr_start;
}
void* get_kernel_pages(uint32_t pg_cnt)
{
void* vaddr = malloc_page(PF_KERNEL,pg_cnt);
if(vaddr != NULL) memset(vaddr,0,pg_cnt*PG_SIZE);
return vaddr;
}
void* get_user_pages(uint32_t pg_cnt)
{
lock_acquire(&user_pool.lock); //用户进程可能会产生冲突 大部分时间都在用户进程 内核进程可以理解基本不会冲突
void* vaddr = malloc_page(PF_USER,pg_cnt);
if(vaddr != NULL) memset(vaddr,0,pg_cnt*PG_SIZE);
lock_release(&user_pool.lock);
return vaddr;
}
void* get_a_page(enum pool_flags pf,uint32_t vaddr)
{
struct pool* mem_pool = (pf == PF_KERNEL) ? &kernel_pool : &user_pool;
lock_acquire(&mem_pool->lock);
struct task_struct* cur = running_thread();
int32_t bit_idx = -1;
//虚拟地址位图置1
if(cur->pgdir != NULL && pf == PF_USER)
{
bit_idx = (vaddr - cur->userprog_vaddr.vaddr_start) / PG_SIZE;
ASSERT(bit_idx > 0);
bitmap_set(&cur->userprog_vaddr.vaddr_bitmap,bit_idx,1);
}
else if(cur->pgdir == NULL && pf == PF_KERNEL)
{
bit_idx = (vaddr - cur->userprog_vaddr.vaddr_start) / PG_SIZE;
ASSERT(bit_idx > 0);
bitmap_set(&kernel_vaddr.vaddr_bitmap,bit_idx,1);
}
else
PANIC("get_a_page:not allow kernel alloc userspace or user alloc kernelspace by get_a_page");
void* page_phyaddr = palloc(mem_pool);
if(page_phyaddr == NULL)
return NULL;
page_table_add((void*)vaddr,page_phyaddr);
lock_release(&mem_pool->lock);
return (void*)vaddr;
}
/* 得到虚拟地址映射的物理地址 */
uint32_t addr_v2p(uint32_t vaddr)
{
uint32_t* pte = pte_ptr(vaddr);
return ((*pte & 0xfffff000) + (vaddr & 0x00000fff));
}
void mem_pool_init(uint32_t all_mem)
{
put_str(" mem_pool_init start!\n");
uint32_t page_table_size = PG_SIZE * 256; //页表占用的大小
uint32_t used_mem = page_table_size + 0x100000; //低端1MB的内存 + 页表所占用的大小
uint32_t free_mem = all_mem - used_mem;
uint16_t all_free_pages = free_mem / PG_SIZE; //空余的页数 = 总空余内存 / 一页的大小
uint16_t kernel_free_pages = all_free_pages /2; //内核 与 用户 各平分剩余内存
uint16_t user_free_pages = all_free_pages - kernel_free_pages; //万一是奇数 就会少1 减去即可
//kbm kernel_bitmap ubm user_bitmap
uint32_t kbm_length = kernel_free_pages / 8; //一位即可表示一页 8位一个数
uint32_t ubm_length = user_free_pages / 8;
//kp kernel_pool up user_pool
uint32_t kp_start = used_mem;
uint32_t up_start = kp_start + kernel_free_pages * PG_SIZE;
kernel_pool.phy_addr_start = kp_start;
user_pool.phy_addr_start = up_start;
kernel_pool.pool_size = kernel_free_pages * PG_SIZE;
user_pool.pool_size = user_free_pages * PG_SIZE;
kernel_pool.pool_bitmap.bits = (void*)MEM_BITMAP_BASE;
user_pool.pool_bitmap.bits = (void*)(MEM_BITMAP_BASE + kbm_length);
kernel_pool.pool_bitmap.btmp_bytes_len = kbm_length;
user_pool.pool_bitmap.btmp_bytes_len = ubm_length;
put_str(" kernel_pool_bitmap_start:");
put_int((int)kernel_pool.pool_bitmap.bits);
put_str(" kernel_pool_phy_addr_start:");
put_int(kernel_pool.phy_addr_start);
put_char('\n');
put_str(" user_pool_bitmap_start:");
put_int((int)user_pool.pool_bitmap.bits);
put_str(" user_pool_phy_addr_start:");
put_int(user_pool.phy_addr_start);
put_char('\n');
bitmap_init(&kernel_pool.pool_bitmap);
bitmap_init(&user_pool.pool_bitmap);
kernel_vaddr.vaddr_bitmap.bits = (void*)(MEM_BITMAP_BASE + kbm_length + ubm_length);
kernel_vaddr.vaddr_bitmap.btmp_bytes_len = kbm_length;
kernel_vaddr.vaddr_start = K_HEAP_START;
bitmap_init(&kernel_vaddr.vaddr_bitmap);
lock_init(&kernel_pool.lock);
lock_init(&user_pool.lock);
put_str(" mem_pool_init done\n");
return;
}
void mem_init()
{
put_str("mem_init start!\n");
uint32_t mem_bytes_total = (*(uint32_t*)(0x800)); //我们把总内存的值放在了0x800 我之前为了显示比较独特 放在了0x800处了
mem_pool_init(mem_bytes_total);
put_str("mem_init done!\n");
return;
}
修改后的memory.h
#ifndef __KERNEL_MEMORY_H
#define __KERNEL_MEMORY_H
#include "stdint.h"
#include "bitmap.h"
struct virtual_addr
{
struct bitmap vaddr_bitmap;
uint32_t vaddr_start;
};
enum pool_flags
{
PF_KERNEL = 1,
PF_USER = 2
};
#define PG_P_1 1
#define PG_P_0 0
#define PG_RW_R 0
#define PG_RW_W 2
#define PG_US_S 0
#define PG_US_U 4
extern struct pool kernel_pool,user_pool;
void* vaddr_get(enum pool_flags pf,uint32_t pg_cnt);
uint32_t* pte_ptr(uint32_t vaddr);
uint32_t* pde_ptr(uint32_t vaddr);
void* palloc(struct pool* m_pool);
void page_table_add(void* _vaddr,void* _page_phyaddr);
void* malloc_page(enum pool_flags pf,uint32_t pg_cnt);
void* get_kernel_pages(uint32_t pg_cnt);
void* get_user_pages(uint32_t pg_cnt);
void* get_a_page(enum pool_flags pf,uint32_t vaddr);
uint32_t addr_v2p(uint32_t vaddr);
void mem_pool_init(uint32_t all_mem);
void mem_init(void);
#endif
编写后的process.c
路径userprog/process.c
#include "tss.h"
#include "process.h"
#include "string.h"
#include "global.h"
#include "memory.h"
#include "print.h"
#include "../thread/thread.h"
#include "../kernel/interrupt.h"
#include "debug.h"
#include "../device/console.h"
void start_process(void* filename_)
{
//schedule线程调度后 来到这里
//特权级0级 到 3级通过 iretd "欺骗" cpu 把用户进程的环境给准备好 iretd即进入
void* function = filename_;
struct task_struct* cur = running_thread();
struct intr_stack* proc_stack = (struct intr_stack*)((uint32_t)cur->self_kstack + sizeof(struct thread_stack));
//环境全存放在intr_stack中了
proc_stack->edi = proc_stack->esi = proc_stack->ebp = proc_stack->esp_dummy = 0;
proc_stack->ebx = proc_stack->edx = proc_stack->ecx = proc_stack->eax = 0;
proc_stack->gs = 0;
proc_stack->ds = proc_stack->es = proc_stack->fs = SELECTOR_U_DATA; //数据段选择子
proc_stack->eip = function; //函数地址 ip
proc_stack->cs = SELECTOR_U_CODE; //cs ip cs选择子
proc_stack->eflags = (EFLAGS_IOPL_0 | EFLAGS_MBS | EFLAGS_IF_1); //不能够关闭中断 ELFAG_IF_1 不然会导致无法调度
proc_stack->esp = (void*)((uint32_t)get_a_page(PF_USER,USER_STACK3_VADDR) + PG_SIZE); //栈空间在0xc0000000以下一页的地方 当然物理内存是操作系统来分配
proc_stack->ss = SELECTOR_U_DATA; //数据段选择子
asm volatile ("movl %0,%%esp;jmp intr_exit": : "g"(proc_stack) : "memory");
}
void page_dir_activate(struct task_struct* p_thread)
{
uint32_t pagedir_phy_addr = 0x100000; //之前设置的页目录表的物理地址
if(p_thread->pgdir != NULL)
{
pagedir_phy_addr = addr_v2p((uint32_t)p_thread->pgdir); //得到实际页目录地址
}
asm volatile ("movl %0,%%cr3" : : "r"(pagedir_phy_addr) : "memory");
}
void process_activate(struct task_struct* p_thread)
{
page_dir_activate(p_thread);
//tss切换需要
if(p_thread->pgdir)
updata_tss_esp(p_thread);
}
uint32_t* create_page_dir(void)
{
uint32_t* page_dir_vaddr = get_kernel_pages(1); //得到内存
if(page_dir_vaddr == NULL)
{
console_put_str("create_page_dir: get_kernel_page failed!\n");
return NULL;
}
memcpy((uint32_t*)((uint32_t)page_dir_vaddr + 0x300*4),(uint32_t*)(0xfffff000+0x300*4),1024); // 256项
uint32_t new_page_dir_phy_addr = addr_v2p((uint32_t)page_dir_vaddr);
page_dir_vaddr[1023] = new_page_dir_phy_addr | PG_US_U | PG_RW_W | PG_P_1; //最后一项是页目录项自己的地址
return page_dir_vaddr;
}
void create_user_vaddr_bitmap(struct task_struct* user_prog)
{
user_prog->userprog_vaddr.vaddr_start = USER_VADDR_START; //位图开始管理的位置
uint32_t bitmap_pg_cnt = DIV_ROUND_UP((0xc0000000 - USER_VADDR_START)/ PG_SIZE / 8,PG_SIZE); //向上取整取虚拟页数
user_prog->userprog_vaddr.vaddr_bitmap.bits = get_kernel_pages(bitmap_pg_cnt);
user_prog->userprog_vaddr.vaddr_bitmap.btmp_bytes_len = (0xc0000000 - USER_VADDR_START) / PG_SIZE / 8;
bitmap_init(&user_prog->userprog_vaddr.vaddr_bitmap);
}
void process_execute(void* filename,char* name)
{
struct task_struct* thread = get_kernel_pages(1); //分配一页空间 得到pcb
init_thread(thread,name,default_prio); //初始化pcb
create_user_vaddr_bitmap(thread); //为虚拟地址位图初始化 分配空间
thread_create(thread,start_process,filename); //创造线程 start_process 之后通过start_process intr_exit跳转到用户进程
thread->pgdir = create_page_dir(); //把页目录表的地址分配了 并且把内核的页目录都给复制过去 这样操作系统对每个进程都可见
enum intr_status old_status = intr_disable();
ASSERT(!elem_find(&thread_ready_list,&thread->general_tag));
list_append(&thread_ready_list,&thread->general_tag); //添加线程 start_process到就绪队列中
ASSERT(!elem_find(&thread_all_list,&thread->all_list_tag));
list_append(&thread_all_list,&thread->all_list_tag);
intr_set_status(old_status);
}
编写后的process.h
#ifndef __USERPROG__PROCESS_H
#define __USERPROG__PROCESS_H
#define USER_VADDR_START 0x00804800
extern void intr_exit(void);
void start_process(void* filename_);
void page_dir_activate(struct task_struct* p_thread);
void process_activate(struct task_struct* p_thread);
uint32_t* create_page_dir(void);
void create_user_vaddr_bitmap(struct task_struct* user_prog);
void process_execute(void* filename,char* name);
#endif
修改后的thread.c
需要修改void schedule()
加一条process_activate(next);
还有头文件 方便期间给完代码
#include "thread.h" //函数声明 各种结构体
#include "stdint.h" //前缀
#include "string.h" //memset
#include "global.h" //不清楚
#include "memory.h" //分配页需要
#include "debug.h"
#include "interrupt.h"
#include "print.h"
#include "../userprog/process.h"
struct task_struct* main_thread; //主线程main_thread的pcb
struct list thread_ready_list; //就绪队列
struct list thread_all_list; //总线程队列
extern void switch_to(struct task_struct* cur,struct task_struct* next);
// 获取 pcb 指针
// 这部分我可以来稍微解释一下
// 我们线程所在的esp 肯定是在 我们get得到的那一页内存 pcb页上下浮动 但是我们的pcb的最起始位置是整数的 除去后面的12位
// 那么我们对前面的取 & 则可以得到 我们的地址所在地
struct task_struct* running_thread(void)
{
uint32_t esp;
asm ("mov %%esp,%0" : "=g"(esp));
return (struct task_struct*)(esp & 0xfffff000);
}
void kernel_thread(thread_func* function,void* func_arg)
{
intr_enable(); //开中断 防止后面的时间中断被屏蔽无法切换线程
function(func_arg);
}
void thread_create(struct task_struct* pthread,thread_func function,void* func_arg)
{
pthread->self_kstack -= sizeof(struct intr_stack); //减去中断栈的空间
pthread->self_kstack -= sizeof(struct thread_stack);
struct thread_stack* kthread_stack = (struct thread_stack*)pthread->self_kstack;
kthread_stack->eip = kernel_thread; //地址为kernel_thread 由kernel_thread 执行function
kthread_stack->function = function;
kthread_stack->func_arg = func_arg;
kthread_stack->ebp = kthread_stack->ebx = kthread_stack->ebx = kthread_stack->esi = 0; //初始化一下
return;
}
void init_thread(struct task_struct* pthread,char* name,int prio)
{
memset(pthread,0,sizeof(*pthread)); //pcb位置清0
strcpy(pthread->name,name);
if(pthread == main_thread)
pthread->status = TASK_RUNNING; //我们的主线程肯定是在运行的
else
pthread->status = TASK_READY; //放到就绪队列里面
pthread->self_kstack = (uint32_t*)((uint32_t)pthread + PG_SIZE); //刚开始的位置是最低位置 栈顶位置+一页得最顶部
//后面还要对这个值进行修改
pthread->priority = prio;
pthread->ticks = prio; //和特权级 相同的时间片
pthread->elapsed_ticks = 0;
pthread->pgdir = NULL; //线程没有单独的地址
pthread->stack_magic = 0x23333333; //设置的魔数 检测是否越界限
}
struct task_struct* thread_start(char* name,int prio,thread_func function,void* func_arg)
{
struct task_struct* thread = get_kernel_pages(1);
init_thread(thread,name,prio);
thread_create(thread,function,func_arg);
ASSERT(!elem_find(&thread_ready_list,&thread->general_tag)); //之前不应该在就绪队列里面
list_append(&thread_ready_list,&thread->general_tag);
ASSERT(!elem_find(&thread_all_list,&thread->all_list_tag));
list_append(&thread_all_list,&thread->all_list_tag);
return thread;
}
//之前在loader.S的时候已经 mov esp,0xc0009f00
//现在的esp已经就在预留的pcb位置上了
void make_main_thread(void)
{
main_thread = running_thread(); //得到main_thread 的pcb指针
init_thread(main_thread,"main",31);
ASSERT(!elem_find(&thread_all_list,&main_thread->all_list_tag));
list_append(&thread_all_list,&main_thread->all_list_tag);
}
void schedule(void)
{
ASSERT(intr_get_status() == INTR_OFF);
struct task_struct* cur = running_thread(); //得到当前pcb的地址
if(cur->status == TASK_RUNNING)
{
ASSERT(!elem_find(&thread_ready_list,&cur->general_tag)); //目前在运行的肯定ready_list是不在的
list_append(&thread_ready_list,&cur->general_tag); //加入尾部
cur->status = TASK_READY;
cur->ticks = cur->priority;
}
else
{}
ASSERT(!list_empty(&thread_ready_list));
struct task_struct* thread_tag = list_pop(&thread_ready_list);
//书上面的有点难理解 代码我写了一个我能理解的
struct task_struct* next = (struct task_struct*)((uint32_t)thread_tag & 0xfffff000);
next->status = TASK_RUNNING;
process_activate(next);
switch_to(cur,next); //esp头顶的是 返回地址 +12是next +8是cur
}
void thread_init(void)
{
put_str("thread_init start!\n");
list_init(&thread_ready_list);
list_init(&thread_all_list);
make_main_thread();
put_str("thread_init done!\n");
}
void thread_block(enum task_status stat)
{
//设置block状态的参数必须是下面三个以下的
ASSERT(((stat == TASK_BLOCKED) || (stat == TASK_WAITING) || stat == TASK_HANGING));
enum intr_status old_status = intr_disable(); //关中断
struct task_struct* cur_thread = running_thread();
cur_thread->status = stat; //把状态重新设置
//调度器切换其他进程了 而且由于status不是running 不会再被放到就绪队列中
schedule();
//被切换回来之后再进行的指令了
intr_set_status(old_status);
}
//由锁拥有者来执行的 善良者把原来自我阻塞的线程重新放到队列中
void thread_unblock(struct task_struct* pthread)
{
enum intr_status old_status = intr_disable();
ASSERT(((pthread->status == TASK_BLOCKED) || (pthread->status == TASK_WAITING) || (pthread->status == TASK_HANGING)));
if(pthread->status != TASK_READY)
{
//被阻塞线程 不应该存在于就绪队列中)
ASSERT(!elem_find(&thread_ready_list,&pthread->general_tag));
if(elem_find(&thread_ready_list,&pthread->general_tag))
PANIC("thread_unblock: blocked thread in ready_list\n"); //debug.h中定义过
//让阻塞了很久的任务放在就绪队列最前面
list_push(&thread_ready_list,&pthread->general_tag);
//状态改为就绪态
pthread->status = TASK_READY;
}
intr_set_status(old_status);
}
修改后的Main.c
#include "print.h"
#include "init.h"
#include "debug.h"
#include "string.h"
#include "memory.h"
#include "../thread/thread.h"
#include "interrupt.h"
#include "../device/console.h"
#include "../device/ioqueue.h"
#include "../device/keyboard.h"
#include "../userprog/process.h"
int a = 0,b = 0;
void test_thread1(void* arg);
void test_thread2(void* arg);
void u_prog_a(void);
void u_prog_b(void);
int main(void) {
put_str("I am kernel\n");
init_all();
thread_start("kernel_thread_a",31,test_thread1,"argA: ");
thread_start("kernel_thread_b",31,test_thread2,"argB: ");
process_execute(u_prog_a,"user_prog_a");
process_execute(u_prog_b,"user_prog_b");
intr_enable();
while(1);
return 0;
}
void test_thread1(void* arg)
{
while(1)
{
console_put_str((char*)arg);
console_put_int(a);
console_put_char(' ');
}
}
void test_thread2(void* arg)
{
while(1)
{
console_put_str((char*)arg);
console_put_int(b);
console_put_char(' ');
}
}
void u_prog_a(void)
{
while(1)
{
++a;
}
}
void u_prog_b(void)
{
while(1)
{
++b;
}
}
修改后的MakeFile
BUILD_DIR = ./build
ENTRY_POINT = 0xc0001500
AS = nasm
CC = gcc
LD = ld
LIB = -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/
ASFLAGS = -f elf
CFLAGS = -Wall -m32 -fno-stack-protector $(LIB) -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS = -m elf_i386 -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o \
$(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o $(BUILD_DIR)/switch.o \
$(BUILD_DIR)/debug.o $(BUILD_DIR)/string.o $(BUILD_DIR)/memory.o \
$(BUILD_DIR)/bitmap.o $(BUILD_DIR)/thread.o $(BUILD_DIR)/list.o \
$(BUILD_DIR)/sync.o $(BUILD_DIR)/console.o $(BUILD_DIR)/keyboard.o \
$(BUILD_DIR)/ioqueue.o $(BUILD_DIR)/tss.o $(BUILD_DIR)/process.o
############## c代码编译 ###############
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h \
lib/stdint.h kernel/init.h lib/string.h kernel/memory.h \
thread/thread.h kernel/interrupt.h device/console.h \
device/keyboard.h device/ioqueue.h userprog/process.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/init.o: kernel/init.c kernel/init.h lib/kernel/print.h \
lib/stdint.h kernel/interrupt.h device/timer.h kernel/memory.h \
thread/thread.h device/console.h device/keyboard.h userprog/tss.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/interrupt.o: kernel/interrupt.c kernel/interrupt.h \
lib/stdint.h kernel/global.h lib/kernel/io.h lib/kernel/print.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/timer.o: device/timer.c device/timer.h lib/kernel/io.h lib/kernel/print.h \
kernel/interrupt.h thread/thread.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/debug.o: kernel/debug.c kernel/debug.h \
lib/kernel/print.h lib/stdint.h kernel/interrupt.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/string.o: lib/string.c lib/string.h \
kernel/debug.h kernel/global.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/memory.o: kernel/memory.c kernel/memory.h \
lib/stdint.h lib/kernel/bitmap.h kernel/debug.h lib/string.h \
thread/sync.h thread/thread.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/bitmap.o: lib/kernel/bitmap.c lib/kernel/bitmap.h kernel/global.h \
lib/string.h kernel/interrupt.h lib/kernel/print.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/thread.o: thread/thread.c thread/thread.h \
lib/stdint.h lib/string.h kernel/global.h kernel/memory.h \
kernel/debug.h kernel/interrupt.h lib/kernel/print.h \
userprog/process.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/list.o: lib/kernel/list.c lib/kernel/list.h \
kernel/interrupt.h lib/stdint.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/sync.o: thread/sync.c thread/sync.h \
lib/stdint.h thread/thread.h kernel/debug.h kernel/interrupt.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/console.o: device/console.c device/console.h \
lib/kernel/print.h thread/sync.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/keyboard.o: device/keyboard.c device/keyboard.h \
lib/kernel/print.h lib/kernel/io.h kernel/interrupt.h \
kernel/global.h lib/stdint.h device/ioqueue.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/ioqueue.o: device/ioqueue.c device/ioqueue.h \
kernel/interrupt.h kernel/global.h kernel/debug.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/tss.o: userprog/tss.c userprog/tss.h \
kernel/global.h thread/thread.h lib/kernel/print.h
$(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/process.o: userprog/process.c userprog/process.h \
lib/string.h kernel/global.h kernel/memory.h lib/kernel/print.h \
thread/thread.h kernel/interrupt.h kernel/debug.h device/console.h
$(CC) $(CFLAGS) $< -o $@
############## 汇编代码编译 ###############
$(BUILD_DIR)/kernel.o: kernel/kernel.S
$(AS) $(ASFLAGS) $< -o $@
$(BUILD_DIR)/print.o: lib/kernel/print.S
$(AS) $(ASFLAGS) $< -o $@
$(BUILD_DIR)/switch.o: thread/switch.S
$(AS) $(ASFLAGS) $< -o $@
############## 链接所有目标文件 #############
$(BUILD_DIR)/kernel.bin: $(OBJS)
$(LD) $(LDFLAGS) $^ -o $@
.PHONY : mk_dir hd clean all
mk_dir:
if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi
hd:
dd if=$(BUILD_DIR)/kernel.bin \
of=/home/cooiboi/bochs/hd60M.img \
bs=512 count=200 seek=9 conv=notrunc
clean:
cd $(BUILD_DIR) && rm -f ./*
build: $(BUILD_DIR)/kernel.bin
all: mk_dir build hd
make all 验收成果
老命令辣 这里两条命令之后就结束了
还是不厌其烦写一下make all
bin/bochs -f bochsrc.disk
其实在写博客之前 这两条命令都是用烂了的 哈哈
因为写这个肯定不免会有很多很多bug 需要一点点调试
最后给出来的代码都是修改了很久之后 对于这部分功能没有出错的代码了
哈哈 不说那么多了 看效果咯
ok了 家人们 肯定有人很好奇 为什么这么数字那么大 因为这个截图是让它运行了还是有一会暂停之后再截图的 所以就是那么大 哈哈
结束语
哈哈 现在已经是中午的11:44了 哈哈 暑假的时光还真是休闲和惬意啊
本来暑假就不应该再那么累的 反过来想想还是对的
忙着忙着就看看手机 就休息一会 哈哈 挺好的 中午了 要去吃饭了
感觉还是挺好的 十一章结束了 待会想个标题就把博客发了 回见!