Hit-oslab lab4 report

源代码在github库:

https://github.com/Focus5679/hit-oslab

这确实是一个有难度的实验,我参考了其他同学的才完整做出来,但把每一部分弄明白之后,确实受益匪浅。

本次实验的修改主要集中在四个文件:sched.h sched.c system_call.s fork.s

下面我将重新详细整理一遍操作过程:

1.修改sched.c中的schedule函数,使其调用重写后的switch_to函数:

...
struct task_struct *pnext = current; // 初始化为当前进程,如果不需要切换,则继续切换到当前进程
...
if ((*p)->state == TASK_RUNNING && (*p)->counter > c) 
				c = (*p)->counter, next = i, pnext = *p; //记录即将切换的进程pcb指针
...
switch_to(pnext, _LDT(next)); //sched.h中宏定义为_LDT(n)

为了实现上述效果还需要在sched.h中注释/删去原来的switch_to宏定义,并重新声明switch_to函数

extern void switch_to(struct task_struct *pnext, long ldt);

2.重写switch_to函数,由于线程切换需要精准控制,所以需要用汇编来实现该函数

在system_call.s中加入:

.align 2
switch_to:
	pushl %ebp
	movl %esp,%ebp
	pushl %ecx
	pushl %ebx
	pushl %eax
	movl 8(%ebp),%ebx	#取出第一个参数pnext
	cmpl %ebx,current	#比较pnext与前一个进程
	je 1f			#相等则跳过切换过程
	#切换PCB
	#内核栈指针重写
	#内核栈指针切换
	#LDT切换
	movl $0x17,%ecx		#重新获取段寄存器fs的值
	mov %cx,%fs
	cmpl %eax,last_task_used_math
	jne 1f
	clts
1:	popl %eax
	popl %ebx
	popl %ecx
	popl %ebp
	ret

2.1切换PCB指针

movl %ebx,%eax		
xchgl %eax,current	#切换pcb指针,切换后,%eax指向当前进程,ebx指向下一个进程

2.2TSS内核栈指针重写

movl tss,%ecx		#TSS中内核栈指针重写,所有进程共用tss0,所以在tss0的ESP0的偏移为止保存内核栈
addl $4096,%ebx    #利用便宜找到内核栈位置
movl %ebx,ESP0(%ecx)    #这里ESP0为4是因为TSS中内核栈位置在地址偏移为4的地方,参考tss结构体定义就明白了

这里用到原始文件中没用到的常量tss、ESP0,它们需要额外定义,其中tss定义在sched.c中。ESP0一会与其他常量一起讲

extern struct tss_struct* tss = &(init_task.task.tss);

2.3内核指针切换

movl %esp,KERNEL_STACK(%eax)	#将当前栈顶指针存到当前PCB的kernelstack位置
movl 8(%ebp),%ebx	#再取一下ebx,因为前面修改过ebx的值
movl KERNEL_STACK(%ebx),%esp	#将下一个进程的PCB中的kernelstack取出,赋给esp,完成切换

这里注意切换内核栈指针,需要用到pcb中的内核栈指针,但原本sched.h中task_struct结构体定义中并没有该指针的定义,所以需要添加,由于添加位置的不同会造成功能代码的不同,所以这里给出我的添加位置:

由于对结构体进行了添加,所以有一些硬编码需要修改:

sched.h文件中在INIT_TASK处添加kernelstack的初始值

system_call.s文件中常量值

2.4LDT切换

movl 12(%ebp),%ecx	#取出switch第二个参数_LDT(next)
lldt %cx		#修改LDTR寄存器
#下面两句很有意思,建议仔细看实验指导书
movl $0x17,%ecx		#重新获取段寄存器fs的值,保证重新查表
mov %cx,%fs

switch_to函数完整代码:

.align 2
switch_to:
	pushl %ebp
	movl %esp,%ebp
	pushl %ecx
	pushl %ebx
	pushl %eax
	movl 8(%ebp),%ebx	#取出第一个参数pnext
	cmpl %ebx,current	#比较pnext与前一个进程
	je 1f			#相等则跳过切换过程
	#切换PCB
	movl %ebx,%eax		
	xchgl %eax,current	#切换pcb指针,切换后,%eax指向当前进程,ebx指向下一个进程
	#内核栈指针重写
	movl tss,%ecx		#TSS中内核栈指针重写,所有进程共用tss0,所以在tss0的ESP0的偏移为止保存内核栈
	addl $4096,%ebx
	movl %ebx,ESP0(%ecx)
	#内核栈指针切换
	movl %esp,KERNEL_STACK(%eax)	#将当前栈顶指针存到当前PCB的kernelstack位置
	movl 8(%ebp),%ebx	#再取一下ebx,因为前面修改过ebx的值
	movl KERNEL_STACK(%ebx),%esp	#将下一个进程的PCB中的kernelstack取出,赋给esp,完成切换
	#LDT切换
	movl 12(%ebp),%ecx	#取出switch第二个参数_LDT(next)
	lldt %cx		#修改LDTR寄存器
	#finish
	movl $0x17,%ecx		#重新获取段寄存器fs的值
	mov %cx,%fs
	cmpl %eax,last_task_used_math
	jne 1f
	clts
1:	popl %eax
	popl %ebx
	popl %ecx
	popl %ebp
	ret

3.修改fork.c中的copy_process()函数

int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
		long ebx,long ecx,long edx,
		long fs,long es,long ds,
		long eip,long cs,long eflags,long esp,long ss)
{
	struct task_struct *p;
	int i;
	struct file *f;

	p = (struct task_struct *) get_free_page();//获得一个task_struct结构体空间
	
	if (!p)
		return -EAGAIN;
	task[nr] = p;
	*p = *current;	/* NOTE! this doesn't copy the supervisor stack */
	p->state = TASK_UNINTERRUPTIBLE;
	p->pid = last_pid;
	p->father = current->pid;
	p->counter = p->priority;
	p->signal = 0;
	p->alarm = 0;
	p->leader = 0;		/* process leadership doesn't inherit */
	p->utime = p->stime = 0;
	p->cutime = p->cstime = 0;
	p->start_time = jiffies;//设置start_time为jiffies
	//记录新建一个进程
	fprintk(3, "%d\tN\t%d\n", p->pid, jiffies);
	//添加结束
	/*
	p->tss.back_link = 0;
	p->tss.esp0 = PAGE_SIZE + (long) p;
	p->tss.ss0 = 0x10;
	p->tss.eip = eip;
	p->tss.eflags = eflags;
	p->tss.eax = 0;
	p->tss.ecx = ecx;
	p->tss.edx = edx;
	p->tss.ebx = ebx;
	p->tss.esp = esp;
	p->tss.ebp = ebp;
	p->tss.esi = esi;
	p->tss.edi = edi;
	p->tss.es = es & 0xffff;
	p->tss.cs = cs & 0xffff;
	p->tss.ss = ss & 0xffff;
	p->tss.ds = ds & 0xffff;
	p->tss.fs = fs & 0xffff;
	p->tss.gs = gs & 0xffff;
	p->tss.ldt = _LDT(nr);
	p->tss.trace_bitmap = 0x80000000;
	if (last_task_used_math == current)
		__asm__("clts ; fnsave %0"::"m" (p->tss.i387));
	*/
	long *krnstack = (long *)(PAGE_SIZE + (long)p); //找到内核栈位置
	*(--krnstack) = ss & 0xffff;
	*(--krnstack) = esp;
	*(--krnstack) = eflags;
	*(--krnstack) = cs & 0xffff;
	*(--krnstack) = eip;
	//上面五步分别对应用户线程的ss,sp,eflsgs,cs,ip
	*(--krnstack) = ds & 0xffff;
	*(--krnstack) = es & 0xffff;
	*(--krnstack) = fs & 0xffff;
	*(--krnstack) = gs & 0xffff;
	*(--krnstack) = esi;
	*(--krnstack) = edi;
	*(--krnstack) = edx;
	//first_return_from_kernel中需要的pop的(恢复现场)
	*(--krnstack) = (long)first_return_from_kernel;
	//从内核态返回用户态所用的函数
	*(--krnstack) = ebp;
	*(--krnstack) = ecx;
	*(--krnstack) = ebx;
	*(--krnstack) = 0;
	//switch_to 要pop的东西
	p->kernelstack = krnstack;

	if (copy_mem(nr,p)) {
		task[nr] = NULL;
		free_page((long) p);
		return -EAGAIN;
	}
	for (i=0; i<NR_OPEN;i++)
		if ((f=p->filp[i]))
			f->f_count++;
	if (current->pwd)
		current->pwd->i_count++;
	if (current->root)
		current->root->i_count++;
	if (current->executable)
		current->executable->i_count++;
	set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
	set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
	p->state = TASK_RUNNING;	/* do this last, just in case */ 
			//设置进程状态为就绪。所有就绪进程的状态都是
			//TASK_RUNNING(0),被全局变量current指向的是正在运行的进程。
	//记录一个进程进入就绪状态
	fprintk(3, "%d\tJ\t%d\n", p->pid, jiffies);
	//添加结束
	return last_pid;
}

4.在system_call.s中增加first_return_from_kernel()函数

.align 2
first_return_from_kernel:
	popl %edx
	popl %edi
	popl %esi
	pop %gs
	pop %fs
	pop %es
	pop %ds
	iret

在system_call.s中设置first_return_from_kernel为全局可见:

.globl switch_to, first_return_from_kernel

在fork.c中添加函数声明:

extern void first_return_from_kernel(void);

5.编写测试程序main.c

运行结果:

回答问题:

1.

(1)因为内核栈栈顶在pcb指针偏移为4K的位置(PAGE_SIZE大小为4K)

(2)因为更改后不再使用tss进行切换,所以所有进程共用tss0,所以ss0不需要设置。

2.

(1)这里的eax等于0,即返回值这样设置在子进程返回处就会返回0,这也是if(!fork())的原因。

(2)此处的ebx,ecx均来自copy_process函数的参数,保证子进程在创建后可以继续执行与父进程相同的代码。

(3)ebp来自copy_process函数的参数,因为copy_process之后父子进程用的是通一个用户栈,所以将用户栈指针ebp也赋值给子进程,也可以新建一个与父进程相同的用户栈进行设置。

3.

如果不重新设置fs=0x17,则查表时段寄存器会为了节省开销直接查找隐式部分,对LDT的切换就无效了,所以这里重新设置fs,使查表操作重新查找显式部分。如果把该操作放在切换前,如果重置之后时间片到时,则需要switch_to下一个进程,而下一个进程可能也需要进行查表操作,有可能重新填充了隐式部分,导致设置操作失效。(个人猜测0.0)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 I 基础知识9 1 MINIX操作系统简介11 1.1 MINIX与UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.2 探索MINIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.3 编辑器:vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.4 编译器:CC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.5 实习题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 Intel 8086体系结构19 2.1 8086 CPU结构. . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 运算器与指令部件. . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 寄存器组. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.4 主存. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.5 堆栈. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.6 系统启动. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.7 二进制文件结构. . . . . . . . . . . . . . . . . . . . . . . . . . 25 3 ACK 8086汇编语言27 3.1 寻址. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.2 数据移动指令. . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.3 常量、标号、数据与全局变量. . . . . . . . . . . . . . . . . . 29 3.4 运算指令. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5 标志位操纵指令. . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.6 串操作指令. . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.7 跳转、分支与循环. . . . . . . . . . . . . . . . . . . . . . . . 33 3.8 堆栈与子程序. . . . . . . . . . . . . . . . . . . . . . . . . . . 34 5 6 目录 4 实习:Hello World 37 4.1 Hello World程序. . . . . . . . . . . . . . . . . . . . . . . . . 37 4.2 实习题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 II 汇编语言进阶41 5 C与汇编联合开发43 5.1 函数调用. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.2 全局与局部变量. . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.3 编译与连接. . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.4 实习题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 6 中断与I/O 49 6.1 中断. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6.2 输入输出设备. . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.3 编写中断处理程序. . . . . . . . . . . . . . . . . . . . . . . . 51 6.4 实习题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 7 实习:小游戏55 7.1 make工具. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 7.2 实习题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 III 操作系统内核59 8 进程切换61 8.1 进程模型. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 8.2 进程的实现. . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 8.3 进程切换. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.4 中断嵌套. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 8.5 MiniOS中的进程切换. . . . . . . . . . . . . . . . . . . . . . . 68 8.6 操作系统实现原则. . . . . . . . . . . . . . . . . . . . . . . . 69 9 进程通信71 9.1 信号量. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 9.2 消息机制. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.3 消息机制的实现. . . . . . . . . . . . . . . . . . . . . . . . . . 76 9.4 调试消息内核. . . . . . . . . . . . . . . . . . . . . . . . . . . 78 目录7 10 实习:操作系统内核83 10.1 引导操作系统. . . . . . . . . . . . . . . . . . . . . . . . . . . 83 10.2 微内核与层次式操作系统结构. . . . . . . . . . . . . . . . . . 83 10.3 实习题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 IV 轻量操作系统实现87 11 设备驱动程序89 11.1 设备驱动程序原理. . . . . . . . . . . . . . . . . . . . . . . . 89 11.2 键盘设备. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 11.3 屏幕设备. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 11.4 死锁的产生与预防. . . . . . . . . . . . . . . . . . . . . . . . 93 12 存储管理95 12.1 存储管理与系统调用服务进程. . . . . . . . . . . . . . . . . . 95 12.2 进程映像的创建与终止. . . . . . . . . . . . . . . . . . . . . . 97 12.3 替换进程映像. . . . . . . . . . . . . . . . . . . . . . . . . . . 98 13 文件系统101 13.1 文件与目录. . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 13.2 文件与目录管理. . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.3 文件描述符. . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 14 实习:迷你操作系统109 14.1 一个简易的Shell . . . . . . . . . . . . . . . . . . . . . . . . . . 109 14.2 实习题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 附录111 A vi常用命令113 B 虚拟机与外部的文件交换117

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值