---
title: "GeekOS之旅-Project2 (Start a new user address Process)"
author: "holoblog"
date: "2011-10-09T00:24:00Z"
tags: []
slug: "geekos-project2-start-a-new-user-address-process"
draft: false
lastmod: "2011-11-09T12:01:11Z"
id: 1245335
url: "https://holoblog.iteye.com/blog/1245335"
---

这个Project的编译和bochrc的配置没什么特殊之处,所以编译后然后启动bochs会显示如下界面:

![](http://hi.csdn.net/attachment/201110/8/0_131804848210xe.gif)

ok, 可以看到 **Unimplemented feature.:Switch to a new user address space, if necessary.** 这个就是我们的任务了.

这句话比较简洁: 新建新的用户地址空间. 但是我们再来看附带的文档, Project Synopsis .就会发现任务不止这一个 .仔细阅读这段 然后把具体任务整理如下:

1\. 在**src/geekos/user.c**中, 需要实现Spawn()和Switch\_To\_User\_Context()这两个函数,Spawn()这个函数主要用来开启一个新的用户进程,Switch\_To\_User\_Context()用来在被调度之前用来开启一个新的线程去切换用户地址空间.

2.在**src/geekos/elf.c**中需要实现Parse\_ELF\_Executable()这个函数, 这个函数就像Project1的处理的一样,包括要读取的可执行ELF文件的 offset, length 和user address 和 data segments. 基于这些信息,填充于Exe\_Format这个结构体中, 这个结构体会被 Spawn()函数调用.当然这些取决于怎么去加载这个可执行文件. 这个可以直接使用之前Project1的代码.

3.在src/geekos/userseg.c中,实现的函数比较多,有 Destroy\_User\_Context() ,Load\_User\_Program(), Copy\_From\_User(), Copy\_To\_User(), Switch\_To\_Address\_Space()

Destroy\_User\_Context(): 释放用户进程的内存资源

Load\_User\_Program(): 载入部分可执行程序到内存中

Copy\_From\_User() 和 Copy\_To\_User(): 在用户地址空间和内核地址空间之间互相拷贝数据

Switch\_To\_Address\_Space(): 当载入处理器的LDT寄存器时激活用户地址空间

4.在**src/geekos/kthread.c**中需要实现Setup\_User\_Thread()和Start\_User\_Thread()两个函数,Setup\_User\_Thread 是建立进程的内核初始化堆栈,也即 当进程第一个次进入用户态时,来初始化处理器的寄存器中的内容, Start\_User\_Thread()主要是使用User\_Conect这个对象来启动一个新的进程.

从上面分析下来, 基本目标已经明确 . 下面就需要一步一步填充这些代码了.

先从开启一个新的用户进程来开刀, 看下 Spawn的原型:

program: 可执行文件的路径

command: 这个参数包括程序的名字 和 参数

pThread: Kernel\_Thread 指针

返回值: 新的进程的id Otherwise return erro Code

可以根据Spwan函数中的Hints 来完成整个函数:

Read\_Fully() 载入整个可执行文件到内存中,原型在 **vfs.h**中

Parse\_ELF\_Executable() 检验ELF可执行文件是否有效 ,原型在 **elf.h** 中 这个函数还需要我们自己来实现的. 不过之前的Project1以前实现过 直接拿来用就可以了

Load\_User\_Program() 创建一个User\_Context来加载程序,原型在 **userseg.h**中 . 这个函数需要我们来实现的

Start\_User\_Thread() 使用创建出来的 User\_Context,并使进程进入准备队列中 原型在**kthread.h**中, 这个函数也是下面我们实现的

剩下的就是整理Spawn这个函数了,Spawn到此为止.

继续看Switch\_To\_User\_Context()

**kthread:** the thread that is about to execute

**state:** saved processor registers describing the state when the thread wsa interrupted

Set\_Kernel\_Stack\_Pointer()这个函数的原型在**tss.h**中, 用来设置内核栈的指针,也就是说是新进程的核心栈.这里需要了解到 进程硬件上下文的一部分存在TSS段,而剩余部分存放在内核态堆栈中

Switch\_To\_Address\_Space()这个函数的原型是在**userseg.h**中,用来切换地址空间的.

这里要了解下,userContext为用户态进程上下文, 对于内核进程来说,这个指针为空, 而用户进程都拥有自己的User\_Context. 所以, 要判断一个进程是内核进程还是用户态进程,只需要通过userContext字段是否为空来判断.Switch\_To\_User\_Context的实现如下:

下面对src/geekos/userseg.c下几个函数进行实现:

**//Destroy a User\_Context object, including all memory and other resources allocated within it.**

**Load\_User\_Program()的实现可以阅读下:**

Besides loading the executable's text and data segments into memory,you will also need to create two other data structures in theprocess's memory space.

You will need to create a *stack* for the new process.The stack will reside at the top of the user address space.You can use the`DEFAULT_USER_STACK_SIZE` macro in`src/geekos/userseg.c` as the stack size.

You will also need to create an *argument block* datastructure. This data structure creates the`argc` and`argv` arguments that are passed to the`main()`function of the user process.Two functions are defined to help you build theargument block (prototypes in`<geekos/argblock.h>`).`Get_Argument_Block_Size()` takes the*command string*passed to the `Spawn()` function and determines how manycommand line arguments there will be, and how manybytes are required to store the argument block.The`Format_Argument_Block()`function takes the number of arguments and argument block size from`Get_Argument_Block_Size()`and builds the argument block data structure in the memory location you haveallocated for it.

Note that you will need to take the size of the stack and argument blockinto account when you decide how much memory to allocate for theprocess.

然后可以根据Hint 来写:

用户进程块的大小 = Round\_Up\_To\_Page(maxva) + DEFAULT\_USER\_STACK\_SIZE; //Round\_Up\_To\_Page为参数块总大小

然后得到size,通过Create\_User\_Context(size)来创建一个用户进程的上下文,再Copy each executable segment into memory. 这里需要把exeFormat-&gt;segment\[i]的内容拷贝到userContext下.最后是些初始化data,入口地址,堆栈指针操作.

Copy\_From\_User和Copy\_To\_User 用户地址空间和内核地址空间之间互相拷贝数据这两个函数的实现主要根据当前进程的User\_Context,g\_currentThread-&gt;userContext

两者都要用到Validate\_User\_Memory这个函数,这个函数起了防御性作用,判错功能,下面再进行memcpy,下面是实现:

switch\_To\_Address\_Space这个函数要使用LDT 也即 userContext-&gt;ldtSelector 来切换到局部描述符表

通过下面这段ASM:

这个Project要填的内容还真多.还有kthread和syscall的实现. 这里就不细说了,有需要的可以留言询问.

最后实现图如下:

![](http://hi.csdn.net/attachment/201110/8/0_13180903423Id5.gif)