目录
建立抽象
实现加载
实现sys_execv
!!!提示:因为实现问题没有测试。所以更像是笔记!
exec
函数的作用是用新的可执行文件替换当前进程的程序体。具体来说,exec
会将当前正在运行的用户进程的进程体(包括代码段、数据段、堆、栈等)替换为一个新的可执行文件的进程体。这样,新的程序会接管当前进程的地址空间,继续执行新程序的代码,但该进程的 PID(进程ID)保持不变。也就是说,执行 exec
后,原来进程的地址空间被清除,并且新程序的内容会加载到同样的进程中,继续执行。
为什么需要实现 exec
呢?这个问题的答案与 shell 的工作方式密切相关。在实现一些简单的命令时,我们使用了类似 if-else if
的结构来判断并执行不同的命令。然而,这种方法存在很大的局限性。首先,它无法处理用户输入的新命令,因为我们不能预见到用户会输入什么命令,且每添加一个新命令就需要修改代码并重新编译。这种方式不仅繁琐,而且无法应对外部程序的运行。
exec
的实现解决了这个问题。当 exec
被调用时,它允许用户运行外部程序,而不需要修改 shell 本身的代码。用户输入的命令会被解析,且通过 exec
函数加载并执行对应的外部程序,从而提供了更灵活的命令执行方式。
exec
是一个函数簇,包含多个相关的函数,区别主要在于如何表示程序对象以及是否传入环境变量。例如,execv
函数就不需要传入环境变量,但其他 exec
函数可能会接受额外的环境变量。
当调用 execv
时,如果执行成功,进程将直接跳转到新程序,并不会返回,因此它没有返回值。调用 execv
失败时,它会返回 -1
,并设置错误码。这是因为 exec
执行新程序时,原进程的执行流被完全替换,进程不会再回到原来的位置,因而不需要像传统函数那样返回值。
建立抽象
我们先对exe文件做抽象:
extern void intr_exit(void);
typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
typedef uint16_t Elf32_Half;
/* 32-bit ELF header */
struct Elf32_Ehdr {
unsigned char e_ident[16]; // ELF identification bytes
Elf32_Half e_type; // Type of file (e.g., executable)
Elf32_Half e_machine; // Machine architecture
Elf32_Word e_version; // ELF version
Elf32_Addr e_entry; // Entry point address
Elf32_Off e_phoff; // Program header offset
Elf32_Off e_shoff; // Section header offset
Elf32_Word e_flags; // Processor-specific flags
Elf32_Half e_ehsize; // ELF header size
Elf32_Half e_phentsize; // Program header entry size
Elf32_Half e_phnum; // Number of program headers
Elf32_Half e_shentsize; // Section header entry size
Elf32_Half e_shnum; // Number of section headers
Elf32_Half e_shstrndx; // Section header string table index
};
/* Program header (segment descriptor) */
struct Elf32_Phdr {
Elf32_Word p_type; // Segment type (e.g., PT_LOAD)
Elf32_Off p_offset; // Offset in file
Elf32_Addr p_vaddr; // Virtual address in memory
Elf32_Addr p_paddr; // Physical address (unused)
Elf32_Word p_filesz; // Size of segment in file
Elf32_Word p_memsz; // Size of segment in memory
Elf32_Word p_flags; // Segment flags
Elf32_Word p_align; // Segment alignment
};
/* Segment types */
enum segment_type {
PT_NULL, // Ignore segment
PT_LOAD, // Loadable segment
PT_DYNAMIC, // Dynamic loading information
PT_INTERP, // Name of dynamic loader
PT_NOTE, // Auxiliary information
PT_SHLIB, // Reserved
PT_PHDR // Program header
};
这段代码定义了32位ELF(Executable and Linkable Format)格式的结构体以及相关的常量,用于描述ELF文件的头部和程序段的描述。具体来说,主要包括以下内容:
-
Elf32_Ehdr: 该结构体表示ELF文件的头部,包含了ELF文件的基本信息,如文件标识、类型、机器架构、入口地址、程序头的偏移量等。具体字段的含义如下:
-
e_ident
:ELF文件标识字节,用于标识文件类型和版本。 -
e_type
:文件类型,表明ELF文件是可执行文件、共享库文件还是其他类型。 -
e_machine
:表示机器架构的字段,如x86、ARM等。 -
e_version
:ELF版本,通常为1。 -
e_entry
:程序入口点的地址。 -
e_phoff
:程序头部的偏移量,指向包含程序段信息的位置。 -
e_shoff
:节头部的偏移量,指向包含节信息的位置。 -
e_flags
:处理器特定的标志。 -
e_ehsize
:ELF头部的大小。 -
e_phentsize
:程序头项的大小。 -
e_phnum
:程序头的数量。 -
e_shentsize
:节头项的大小。 -
e_shnum
:节头的数量。 -
e_shstrndx
:节头字符串表的索引。
-
-
Elf32_Phdr: 该结构体表示ELF文件中的程序头(segment descriptor),用于描述文件中的每个段。字段的含义如下:
-
p_type
:段的类型,如可加载段、动态段等。 -
p_offset
:段在文件中的偏移。 -
p_vaddr
:段在内存中的虚拟地址。 -
p_paddr
:段在物理内存中的地址(通常不使用)。 -
p_filesz
:段在文件中的大小。 -
p_memsz
:段在内存中的大小。 -
p_flags
:段的标志,如可读、可写、可执行等。 -
p_align
:段的对齐方式。
-
-
segment_type:该枚举定义了常见的段类型,如:
-
PT_NULL
:表示忽略该段。 -
PT_LOAD
:表示可加载的段(常见的代码和数据段)。 -
PT_DYNAMIC
:动态加载信息。 -
PT_INTERP
:动态加载器的名称。 -
PT_NOTE
:辅助信息。 -
PT_SHLIB
:保留段。 -
PT_PHDR
:程序头。
-
实现加载
/* Load a segment from a file into virtual memory at the specified address */
static bool segment_load(int32_t fd, uint32_t offset, uint32_t filesz,
uint32_t vaddr) {
uint32_t vaddr_first_page =
vaddr & 0xfffff000; // First page of the virtual address
uint32_t size_in_first_page =
PG_SIZE - (vaddr & 0x00000fff); // Size of the segment in the first page
uint32_t occupy_pages = 0;
// If the segment doesn't fit in a single page
if (filesz > size_in_first_page) {
uint32_t left_size = filesz - size_in_first_page;
occupy_pages = ROUNDUP(left_size, PG_SIZE) + 1; // +1 for the first page
} else {
occupy_pages = 1;
}
// Allocate memory for the segment in the process's address space
uint32_t page_idx = 0;
uint32_t vaddr_page = vaddr_first_page;
while (page_idx < occupy_pages) {
uint32_t *pde = pde_ptr(vaddr_page); // Page directory entry
uint32_t *pte = pte_ptr(vaddr_page); // Page table entry
// Allocate memory if PDE or PTE doesn't exist
if (!(*pde & PG_P_1) || !(*pte & PG_P_1)) {
if (!get_a_page(PF_USER, vaddr_page)) {
return false;
}
}
vaddr_page += PG_SIZE;
page_idx++;
}
// Read the segment data from the file and load it into memory
sys_lseek(fd, offset, SEEK_SET);
sys_read(fd, (void *)vaddr, filesz);
return true;
}
函数 segment_load
负责将一个可执行文件中的特定段加载到进程的虚拟内存中,它接收四个参数:文件描述符 fd
,段在文件中的偏移量 offset
,段大小 filesz
,以及段应加载到的虚拟地址 vaddr
。其中 filesz
命名虽然让人容易联想到整个文件大小,但它其实是 ELF 格式中段头部的字段名 p_filesz
,表示当前这个段在文件中的实际大小,因此用作参数名是为了与 ELF 中的结构保持一致。
段的加载实质上就是内核为新进程分配内存的过程。由于程序通常由多个段组成,内核需要对每个段逐一加载。加载时以页为单位进行内存管理,因此即使一个段不满一页,也必须以页为粒度分配内存。变量 vaddr_first_page
是将段的虚拟地址 vaddr
向下对齐到页起始地址,用于确定从哪里开始分配页框。而变量 size_in_first_page
则表示该段在第一页中所占用的字节数,如果 filesz
大于这个值,说明段会跨页,因此接下来计算还需多少页框,最终由 occupy_pages
给出总的页框数。
接下来是页框分配逻辑,考虑到这是 exec 执行新程序的场景,当前进程的页表结构还在用,若某虚拟地址已经存在对应的物理页,则无需重新分配,只需直接复用原页框覆盖其内容即可;否则就通过 get_a_page
分配一个新页框。分配时逐页判断并处理,直到整段的地址空间都被准备好。
页框分配完成后,便可以真正加载段的数据了。首先使用 sys_lseek
将文件读指针移动到段的起始偏移位置 offset
,再用 sys_read
将长度为 filesz
的数据读入到从 vaddr
开始的虚拟地址中。至此,这个段被完整加载进内存。整个过程体现了分段加载、按页管理、懒分配页框的设计思路,也保证了内存使用的灵活性与效率。
/* Load a user program from the filesystem by pathname, return entry point
* address or -1 on failure */
static int32_t load(const char *pathname) {
int32_t ret = -1;
struct Elf32_Ehdr elf_header;
struct Elf32_Phdr prog_header;
k_memset(&elf_header, 0, sizeof(struct Elf32_Ehdr));
int32_t fd = sys_open(pathname, O_RDONLY); // Open the program file
if (fd == -1) {
return -1;
}
// Read the ELF header from the file
if (sys_read(fd, &elf_header, sizeof(struct Elf32_Ehdr)) !=
sizeof(struct Elf32_Ehdr)) {
ret = -1;
goto done;
}
// Verify the ELF header
if (k_memcmp(elf_header.e_ident, "\177ELF\1\1\1", 7) ||
elf_header.e_type != 2 || elf_header.e_machine != 3 ||
elf_header.e_version != 1 || elf_header.e_phnum > 1024 ||
elf_header.e_phentsize != sizeof(struct Elf32_Phdr)) {
ret = -1;
goto done;
}
Elf32_Off prog_header_offset = elf_header.e_phoff;
Elf32_Half prog_header_size = elf_header.e_phentsize;
// Iterate over all program headers
uint32_t prog_idx = 0;
while (prog_idx < elf_header.e_phnum) {
k_memset(&prog_header, 0, prog_header_size);
// Seek to the program header location in the file
sys_lseek(fd, prog_header_offset, SEEK_SET);
// Read the program header from the file
if (sys_read(fd, &prog_header, prog_header_size) != prog_header_size) {
ret = -1;
goto done;
}
// If the segment is loadable, load it into memory
if (PT_LOAD == prog_header.p_type) {
if (!segment_load(fd, prog_header.p_offset, prog_header.p_filesz,
prog_header.p_vaddr)) {
ret = -1;
goto done;
}
}
// Move to the next program header
prog_header_offset += elf_header.e_phentsize;
prog_idx++;
}
ret = elf_header.e_entry; // Return the entry point of the program
done:
sys_close(fd); // Close the file
return ret;
}
函数 load
的核心功能是加载一个 ELF 格式的用户程序文件,并将其段映射到当前进程的虚拟地址空间中。如果加载成功,返回值是该程序的入口地址(即进程执行的起点);如果失败,返回 −1。
函数开始先声明两个结构体变量:elf_header
和 prog_header
,分别用于保存 ELF 文件头和程序段头。在读取 ELF 文件头后(第 102 行),程序紧接着从第 108 行开始验证 ELF 文件是否合法。
首先检查的是 ELF 文件的魔数 e_ident[0-6]
,这 7 个字节应依次为:
-
0x7F
(用八进制\177
表示) -
'E'
(0x45) -
'L'
(0x4C) -
'F'
(0x46) -
1
:32 位格式 -
1
:小端格式 -
1
:版本号
这几项是 ELF 文件的标准标志,如果不匹配,说明该文件不是合法的 ELF 可执行文件。接下来还会检查以下几个字段:
-
e_type
是否为ET_EXEC
(值为 2,代表可执行文件) -
e_machine
是否为EM_386
(值为 3,表示 x86 架构) -
e_version
是否为 1(当前 ELF 版本) -
e_phnum
(程序头数量)是否小于等于 1024 -
e_phentsize
(每个程序头条目的大小)是否等于sizeof(Elf32_Phdr)
这些检查都通过后,才认为这是一个有效的 ELF 可执行文件。
接下来,从 ELF 头中读取段头信息的起始偏移地址 e_phoff
,读取到变量 prog_header_offset
。段头条目的字节大小 e_phentsize
赋给 prog_header_size
,条目总数 e_phnum
用于控制接下来的循环。
然后从第 122 行进入循环,逐个读取每个段头。每次循环会先通过 sys_lseek
将文件指针跳到对应段头位置,然后通过 sys_read
读取一条段头到 prog_header
。第 136 行判断该段是否是 PT_LOAD
类型,也就是是否是可加载段。如果是,就调用 segment_load
,将该段的内容从文件加载到内存对应的虚拟地址。
所有段处理完毕后,从 ELF 头中提取程序入口地址 e_entry
赋给返回值 ret
,这表示程序开始执行的地址。
最后,无论是否加载成功,都会通过 sys_close
关闭打开的 ELF 文件,返回值为加载成功的入口地址或失败的 −1。
总体来说,load
函数的实现非常典型地体现了 ELF 格式的标准解析流程、段式加载方式、虚拟内存分配控制等关键内核概念,是内核启动用户进程的核心部分之一。
实现sys_execv
/* Replace the current process with the program at the specified path */
int32_t sys_execv(const char *path, const char *argv[]) {
uint32_t argc = 0;
while (argv[argc]) {
argc++; // Count the number of arguments
}
// Load the program and get its entry point
int32_t entry_point = load(path);
if (entry_point == -1) { // If loading failed, return -1
return -1;
}
TaskStruct *cur = current_thread(); // Get the current running thread (process)
k_memcpy(cur->name, path, TASK_NAME_ARRAY_SZ); // Update the process name
// Update the stack with the arguments
Interrupt_Stack *intr_0_stack =
(Interrupt_Stack *)((uint32_t)cur + PG_SIZE - sizeof(Interrupt_Stack));
intr_0_stack->ebx = (int32_t)argv;
intr_0_stack->ecx = argc;
intr_0_stack->eip = (void *)entry_point;
intr_0_stack->esp = (void *)KERNEL_V_START; // Set stack pointer to the highest
// user space address
// Jump to the entry point of the new process
asm volatile("movl %0, %%esp; jmp intr_exit"
:
: "g"(intr_0_stack)
: "memory");
return 0;
}
sys_execv
函数的作用是将当前正在运行的进程替换为另一个可执行文件 path
所指定的程序,同时把参数数组 argv
一并传给新程序。这个过程不会返回,一旦成功,当前进程就“变成”了另一个程序。
首先,函数会遍历 argv
,统计参数个数并存入变量 argc
。接着调用 load(path)
试图加载用户程序,如果加载失败(返回 -1),函数立即返回 -1。若加载成功,程序的入口地址会被保存下来,作为后续执行的跳转目标。
之后,函数更新当前进程控制块中的 name
字段,使其反映正在执行的新程序名,这样在通过 ps 等工具查看时会显示为新程序的名字。
然后获取当前线程的内核栈顶地址。此时栈中存储的是旧进程的中断现场,但很快要把这些内容替换掉,准备启动新进程。函数将参数个数 argc
写入栈中保存的 ecx
寄存器位置,将参数数组 argv
的地址写入 ebx
寄存器位置。因为 ebx
通常用于保存基地址,而 ecx
常用于计数,这是一种传统习惯,也便于未来从运行库中取参数。接着将程序入口地址写入 eip
,用于后续跳转执行;再将用户栈指针 esp
初始化为 0xc0000000,即用户空间最高地址,以便新程序使用。
设置完成后,通过内联汇编将 esp 寄存器修改为新的内核栈地址,并跳转到 intr_exit。这个跳转操作会恢复栈中保存的所有寄存器状态,包括 eip、esp 和参数寄存器等,相当于“伪装”从中断中返回,从而进入新程序的执行流程。
因为这个过程是不可逆的,调用成功后不会返回到原来的函数中,所以 return 0 这一行永远不会执行,它的存在只是为了避免编译器报错。整段代码实现的是典型的 exec 功能,用一个新的程序完全替换当前进程的执行内容。
下一篇
从0开始的操作系统手搓教程46——实现wait和exit-CSDN博客文章浏览阅读522次,点赞7次,收藏8次。实现exit和wait(笔记,因为实现问题没有测试)https://blog.csdn.net/charlie114514191/article/details/146144946