操作系统实验——创建系统调用
type
status
date
slug
summary
tags
category
icon
password
comment_flag
SLUGS
实验内容:

一脸懵逼地按照PPT搬完所有代码,不知道原理所在,后悔课上没认真听。下面就做做总结,其中错误希望后面能纠正。
EPOS文件下载涉及到的文件:
系统调用的过程大致如下:

系统调用的用户接口
main.c中调用:
C语言的接口声明
如果实现
time
,\usrapp\include\syscall.h中声明:汇编中例程封装
\userapp\lib\syscall-wrapper.S中完成了接口的的汇编语言实现,先将系统调用号放进
eax
,然后触发软中断0x82
,(epos通过中断进入内核态):如果是实现
time
,那么实现如下:在\include\syscall-nr.h中,定义系统调用号:
中断发生
触发
0x82
中断后,CPU从用户模式切换到内核模式,并完成栈的切换。
保护中断现场,即压入到内核栈中:

中断处理
CPU在内核模式下,根据中断号,将控制传给
hwintXX
(XX为中断号)。
在\kernel\entry.S
中实现,
注:pushal, popal - push/pop EAX,EBX,ECX,EDX,ESP,EBP,ESI,EDI
本应是这样,但在epos中,0x82的中断处理被单独实现——
(\epos\kernel\entry.S中):

系统调用
在
_int0x82_syscall
中,先将内核模式现场压入内核栈,然后调用C语言函数syscall()
(系统调用处理程序),并从传入的参数ctx读取系统调用编号ctx->eax
,通过系统调用编号找到相应的服务例程:下面以
sys_putchar
服务例程为例:
如果实现
sys_time
的调用,则如下:并在\kernel\kernel.h中声明
time_t sys_time();
,然后在\kernel\machdep.c中实现:注:服务例程代表用户进程,并不属于中断上下文,而是进程上下文。因此,系统调用运行过程中,能够访问用户进程的很多信息,能够被其它进程抢占,能够休眠。所以进入syscall()前允许中断,出来后又屏蔽中断。
中断返回
当系统调用完毕后,把控制权交回到发起调用的用户进程前,内核会有一次调度,即恢复现场:

最终运行结果:

Loading...