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

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

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

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

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

FailedtoParseELFFile!

if((rc=Load_User_Program(exeFileData,exeFileLength,&

exeFormat,command,&

userContext))!

=0)

FailedtoLoadUserProgram!

//在堆分配方式下释放内存并再次初始化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!

Free(exeFileData);

//释放内存

if(userContext!

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;

entryAddr=head->

entry;

for(i=0;

i<

i++)

exeFormat->

segmentList[i].offsetInFile=proHeader->

offset;

segmentList[i].lengthInFile=proHeader->

fileSize;

segmentList[i].startAddress=proHeader->

vaddr;

segmentList[i].sizeInMemory=proHeader->

memSize;

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!

UserContext->

memory=Malloc(size);

//为核心态进程

gotofail;

//内存为空

if(0==UserContext->

memory)

memset(UserContext->

memory,'

\0'

size);

size=size;

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

//新建一个LDT描述符

ldtDescriptor=Allocate_Segment_Descriptor();

ldtDescriptor)

//初始化段描述符

Init_LDT_Descriptor(UserContext->

ldtDescriptor,UserContext->

ldt,NUM_USER_LDT_ENTRIES);

//新建一个LDT选择子

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(

ldt[1],

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

csSelector=Selector(USER_PRIVILEGE,false,0);

dsSelector=Selector(USER_PRIVILEGE,false,1);

//将引用数清0

refCount=0;

returnUserContext;

fail:

=0){

if(UserContext->

memory!

Free(UserContext->

memory);

Free(UserContext);

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

//摧毁用户上下文

voidDestroy_User_Context(structUser_Context*userContext)

DestroyaUser_Context"

//释放占用的LDT

Free_Segment_Descriptor(userContext->

ldtDescriptor);

userContext->

ldtDescriptor=0;

//释放内存空间

Free(userContext->

memory=0;

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

Free(userContext);

userContext=0;

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

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

Loadauserexecutableintoausermemoryspaceusingsegmentation"

ulong_tmaxva=0;

//要分配的最大内存空间

unsignednumArgs;

//进程数目

ulong_targBlockSize;

//参数块的大小

ulong_tsize,argBlockAddr;

//参数块地址

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

for(i=0;

i<

numSegments;

++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;

structExe_Segment*segment=&

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

memcpy(userContext->

memory+segment->

startAddress,exeFileData+segment->

offsetInFile,segment->

lengthInFile);

//格式化参数块

Format_Argument_Block(userContext->

memory+argBlockAddr,numArgs,argBlockAddr,command);

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

entryAddr=exeFormat->

entryAddr;

argBlockAddr=argBlockAddr;

stackPointerAddr=argBlockAddr;

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

*pUserContext=userContext;

//成功

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

boolCopy_From_User(void*destInKernel,ulong_tsrcInUser,ulong_tbufSize)

structUser_Context*UserContext=g_currentThread->

//--:

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)

Validate_User_Memory(UserContext,destInUser,bufSize))

memcpy(UserContext->

memory+destInUser,srcInKernel,bufSize);

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

//切换到用户地址空间

voidSwitch_To_Address_Space(structUser_Context*userContext)

ushort_tldtSelector=userContext->

ldtSelector;

/*SwitchtotheLDTofthenewusercontext*/

__asm____volatile__("

lldt%0"

:

"

a"

(ldtSelector));

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

添加头文件#include<

geekos/user.h>

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

//创建一个用户进程

/*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);

//文本选择子

entryAddr);

//程序计数器

Push(kthread,0);

//错误代码(0)

//中断号(0)

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

/*eax*/

/*ebx*/

/*edx*/

argBlockAddr);

/*esi*/

/*edi*/

/*ebp*/

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

/*ds*/

/*es*/

/*fs*/

/*gs*/

//开始用户进程

structKernel_Thread*Start_User_Thread(structUser_Context*userContext,booldetached)

structKernel_Thread*kthread=Create_Thread(PRIORITY_USER,detached);

if(kthread!

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;

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

rc=EINVALID;

Free(str);

str[len]='

;

//成功

*pStr=str;

staticintSys_Exit(structInterrupt_State*st

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

当前位置:首页 > PPT模板 > 卡通动漫

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

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