计算机操作系统实验运行用户态程序.docx

上传人:b****5 文档编号:6136191 上传时间:2023-01-04 格式:DOCX 页数:17 大小:134.54KB
下载 相关 举报
计算机操作系统实验运行用户态程序.docx_第1页
第1页 / 共17页
计算机操作系统实验运行用户态程序.docx_第2页
第2页 / 共17页
计算机操作系统实验运行用户态程序.docx_第3页
第3页 / 共17页
计算机操作系统实验运行用户态程序.docx_第4页
第4页 / 共17页
计算机操作系统实验运行用户态程序.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

计算机操作系统实验运行用户态程序.docx

《计算机操作系统实验运行用户态程序.docx》由会员分享,可在线阅读,更多相关《计算机操作系统实验运行用户态程序.docx(17页珍藏版)》请在冰豆网上搜索。

计算机操作系统实验运行用户态程序.docx

计算机操作系统实验运行用户态程序

西北工业大学操作系统实验实验报告

一、实验目的

掌握在GeekOS系统用户态模式下加载并运行可执行程序的方法。

二、实验要求

1.按照实验讲义P127页中的设计要求,实现在用户态模式下加载并运行可执行程序的代码,给出关键函数的代码以及实验结果。

三、实验过程及结果

答:

核心函数代码如下:

==================user.c===============

//产生一个进程(用户态)

intSpawn(constchar*program,constchar*command,structKernel_Thread**pThread)

{

//TODO("Spawnaprocessbyreadinganexecutablefromafilesystem");

intrc;

char*exeFileData=0;

ulong_texeFileLength;

structUser_Context*userContext=0;

structKernel_Thread*process=0;

structExe_FormatexeFormat;

if((rc=Read_Fully(program,(void**)&exeFileData,&exeFileLength))!

=0)

{

Print("FailedtoReadFile%s!

\n",program);

gotofail;

}

if((rc=Parse_ELF_Executable(exeFileData,exeFileLength,&exeFormat))!

=0)

{

Print("FailedtoParseELFFile!

\n");

gotofail;

}

if((rc=Load_User_Program(exeFileData,exeFileLength,&exeFormat,command,&userContext))!

=0)

{

Print("FailedtoLoadUserProgram!

\n");

gotofail;

}

//在堆分配方式下释放内存并再次初始化exeFileData

Free(exeFileData);

exeFileData=0;

/*开始用户进程,调用Start_User_Thread函数创建一个进程并使其进入准备运行队列*/

process=Start_User_Thread(userContext,false);

if(process!

=0){

KASSERT(process->refCount==2);

/*返回核心进程的指针*/

*pThread=process;

rc=process->pid;//记录当前进程的ID

}

else

rc=ENOMEM;

returnrc;

fail:

//如果新进程创建失败则注销User_Context对象

if(exeFileData!

=0)

Free(exeFileData);//释放内存

if(userContext!

=0)

Destroy_User_Context(userContext);//销毁进程对象

returnrc;

}

-------------------------------------

//切换至用户上下文

voidSwitch_To_User_Context(structKernel_Thread*kthread,structInterrupt_State*state)

{

staticstructUser_Context*s_currentUserContext;/*lastusercontextused*/

//externintuserDebug;

structUser_Context*userContext=kthread->userContext;

KASSERT(!

Interrupts_Enabled());

if(userContext==0){//userContext为0表示此进程为核心态进程就不用切换地址空间

return;

}

if(userContext!

=s_currentUserContext){

ulong_tesp0;

//if(userDebug)Print("A[%p]\n",kthread);

Switch_To_Address_Space(userContext);//为用户态进程时则切换地址空间

esp0=((ulong_t)kthread->stackPage)+PAGE_SIZE;

//if(userDebug)

//Print("S[%lx]\n",esp0);

/*新进程的核心栈.*/

Set_Kernel_Stack_Pointer(esp0);//设置内核堆栈指针

/*Newusercontextisactive*/

s_currentUserContext=userContext;

}

}

==================elf.c====================

intParse_ELF_Executable(char*exeFileData,ulong_texeFileLength,structExe_Format*exeFormat)

{

inti;

elfHeader*head=(elfHeader*)exeFileData;

programHeader*proHeader=(programHeader*)(exeFileData+head->phoff);

KASSERT(exeFileData!

=NULL);

KASSERT(exeFileLength>head->ehsize+head->phentsize*head->phnum);

KASSERT(head->entry%4==0);

exeFormat->numSegments=head->phnum;

exeFormat->entryAddr=head->entry;

for(i=0;iphnum;i++)

{

exeFormat->segmentList[i].offsetInFile=proHeader->offset;

exeFormat->segmentList[i].lengthInFile=proHeader->fileSize;

exeFormat->segmentList[i].startAddress=proHeader->vaddr;

exeFormat->segmentList[i].sizeInMemory=proHeader->memSize;

exeFormat->segmentList[i].protFlags=proHeader->flags;

proHeader++;

}

return0;

}

===================userseg.c===================

//需在此文件各函数前增加一个函数,此函数的功能是按给定的大小创建一个用户级进程上下文,具体实现如下:

//函数功能:

按给定的大小创建一个用户级进程上下文

staticstructUser_Context*Create_User_Context(ulong_tsize)

{

structUser_Context*UserContext;

size=Round_Up_To_Page(size);

UserContext=(structUser_Context*)Malloc(sizeof(structUser_Context));

if(UserContext!

=0)

UserContext->memory=Malloc(size);

//为核心态进程

else

gotofail;

//内存为空

if(0==UserContext->memory)

gotofail;

memset(UserContext->memory,'\0',size);

UserContext->size=size;

//以下为用户态进程创建LDT(段描述符表)

//新建一个LDT描述符

UserContext->ldtDescriptor=Allocate_Segment_Descriptor();

if(0==UserContext->ldtDescriptor)

gotofail;

//初始化段描述符

Init_LDT_Descriptor(UserContext->ldtDescriptor,UserContext->ldt,NUM_USER_LDT_ENTRIES);

//新建一个LDT选择子

UserContext->ldtSelector=Selector(KERNEL_PRIVILEGE,true,Get_Descriptor_Index(UserContext->ldtDescriptor));

//新建一个文本段描述符

Init_Code_Segment_Descriptor(

&UserContext->ldt[0],

(ulong_t)UserContext->memory,

size/PAGE_SIZE,

USER_PRIVILEGE

);

//新建一个数据段

Init_Data_Segment_Descriptor(

&UserContext->ldt[1],

(ulong_t)UserContext->memory,

size/PAGE_SIZE,

USER_PRIVILEGE

);

//新建数据段和文本段选择子

UserContext->csSelector=Selector(USER_PRIVILEGE,false,0);

UserContext->dsSelector=Selector(USER_PRIVILEGE,false,1);

//将引用数清0

UserContext->refCount=0;

returnUserContext;

fail:

if(UserContext!

=0){

if(UserContext->memory!

=0){

Free(UserContext->memory);

}

Free(UserContext);

}

return0;

}

--------------------------------------------

//摧毁用户上下文

voidDestroy_User_Context(structUser_Context*userContext)

{

//TODO("DestroyaUser_Context");

//释放占用的LDT

Free_Segment_Descriptor(userContext->ldtDescriptor);

userContext->ldtDescriptor=0;

//释放内存空间

Free(userContext->memory);

userContext->memory=0;

//释放userContext本身占用的内存

Free(userContext);

userContext=0;

}

----------------------------------------------

intLoad_User_Program(char*exeFileData,ulong_texeFileLength,structExe_Format*exeFormat,constchar*command,structUser_Context**pUserContext)

{

//TODO("Loadauserexecutableintoausermemoryspaceusingsegmentation");

inti;

ulong_tmaxva=0;//要分配的最大内存空间

unsignednumArgs;//进程数目

ulong_targBlockSize;//参数块的大小

ulong_tsize,argBlockAddr;//参数块地址

structUser_Context*userContext=0;

//计算用户态进程所需的最大内存空间

for(i=0;inumSegments;++i){

//elf.h

structExe_Segment*segment=&exeFormat->segmentList[i];

ulong_ttopva=segment->startAddress+segment->sizeInMemory;/*FIXME:

rangecheck*/

if(topva>maxva)

maxva=topva;

}

Get_Argument_Block_Size(command,&numArgs,&argBlockSize);//获取参数块信息

size=Round_Up_To_Page(maxva)+DEFAULT_USER_STACK_SIZE;

argBlockAddr=size;

size+=argBlockSize;

userContext=Create_User_Context(size);//按相应大小创建一个进程

if(userContext==0)//如果为核心态进程

return-1;

for(i=0;inumSegments;++i){

structExe_Segment*segment=&exeFormat->segmentList[i];

//根据段信息将用户程序中的各段内容复制到分配的用户内存空间

memcpy(userContext->memory+segment->startAddress,exeFileData+segment->offsetInFile,segment->lengthInFile);

}

//格式化参数块

Format_Argument_Block(userContext->memory+argBlockAddr,numArgs,argBlockAddr,command);

//初始化数据段,堆栈段及代码段信息

userContext->entryAddr=exeFormat->entryAddr;

userContext->argBlockAddr=argBlockAddr;

userContext->stackPointerAddr=argBlockAddr;

//将初始化完毕的User_Context赋给*pUserContext

*pUserContext=userContext;

return0;//成功

}

----------------------------------------------

//将用户态的进程复制到内核缓冲区

boolCopy_From_User(void*destInKernel,ulong_tsrcInUser,ulong_tbufSize)

{

structUser_Context*UserContext=g_currentThread->userContext;

//--:

checkifmemoryifvalidated

if(!

Validate_User_Memory(UserContext,srcInUser,bufSize))

returnfalse;

memcpy(destInKernel,UserContext->memory+srcInUser,bufSize);

returntrue;

}

-----------------------------------------

//将内核态的进程复制到用户态

boolCopy_To_User(ulong_tdestInUser,void*srcInKernel,ulong_tbufSize)

{

structUser_Context*UserContext=g_currentThread->userContext;

if(!

Validate_User_Memory(UserContext,destInUser,bufSize))

returnfalse;

memcpy(UserContext->memory+destInUser,srcInKernel,bufSize);

returntrue;

}

----------------------------------------

//切换到用户地址空间

voidSwitch_To_Address_Space(structUser_Context*userContext)

{

ushort_tldtSelector=userContext->ldtSelector;/*SwitchtotheLDTofthenewusercontext*/

__asm____volatile__("lldt%0":

:

"a"(ldtSelector));

}

=================kthread.c===============

添加头文件#include

----------------------------------

//创建一个用户进程

/*static*/voidSetup_User_Thread(structKernel_Thread*kthread,structUser_Context*userContext)

{

ulong_teflags=EFLAGS_IF;

unsignedcsSelector=userContext->csSelector;//CS选择子

unsigneddsSelector=userContext->dsSelector;//DS选择子

Attach_User_Context(kthread,userContext);

//初始化用户态进程堆栈,使之看上去像刚被中断运行一样

//分别调用Push函数将以下数据压入堆栈

Push(kthread,dsSelector);//数据选择子

Push(kthread,userContext->stackPointerAddr);//堆栈指针

Push(kthread,eflags);//Eflags

Push(kthread,csSelector);//文本选择子

Push(kthread,userContext->entryAddr);//程序计数器

Push(kthread,0);//错误代码(0)

Push(kthread,0);//中断号(0)

//初始化通用寄存单元,将ESI用户传递参数块地址

Push(kthread,0);/*eax*/

Push(kthread,0);/*ebx*/

Push(kthread,0);/*edx*/

Push(kthread,0);/*edx*/

Push(kthread,userContext->argBlockAddr);/*esi*/

Push(kthread,0);/*edi*/

Push(kthread,0);/*ebp*/

//初始化数据段寄存单元

Push(kthread,dsSelector);/*ds*/

Push(kthread,dsSelector);/*es*/

Push(kthread,dsSelector);/*fs*/

Push(kthread,dsSelector);/*gs*/

}

//开始用户进程

structKernel_Thread*Start_User_Thread(structUser_Context*userContext,booldetached)

{

structKernel_Thread*kthread=Create_Thread(PRIORITY_USER,detached);

if(kthread!

=0){

Setup_User_Thread(kthread,userContext);

Make_Runnable_Atomic(kthread);

}

returnkthread;

}

================syscall.c=================

//需在此文件别的函数前增加一个函数,函数名为Copy_User_String,它被函数Sys_PrintString调用,具体实现如下:

staticintCopy_User_String(ulong_tuaddr,ulong_tlen,ulong_tmaxLen,char**pStr)

{intrc=0;

char*str;

if(len>maxLen){//超过最大长度

returnEINVALID;

}

str=(char*)Malloc(len+1);//为字符串分配空间

if(0==str){

rc=ENOMEM;

gotofail;

}

if(!

Copy_From_User(str,uaddr,len)){//从用户空间中复制数据

rc=EINVALID;

Free(str);

gotofail;

}

str[len]='\0';

//成功

*pStr=str;

fail:

returnrc;

}

-----------------------------------------

staticintSys_Exit(structInterrupt_State*st

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1