finish exec3.2

This commit is contained in:
KAAAsS 2021-05-13 23:07:46 +08:00
parent b83368fb1f
commit 61fad2c531
Signed by: KAAAsS
GPG Key ID: D56625F3E671882F
3 changed files with 17 additions and 0 deletions

View File

@ -247,17 +247,21 @@ void process_create_root(char *bin_name)
char *binary = NULL;
int ret;
// 从内存硬盘读取相关文件,使用 CPIO 格式
ret = ramdisk_read_file(bin_name, &binary);
BUG_ON(ret < 0);
BUG_ON(binary == NULL);
// 创建 PCB 并设置自身 Cap
root_process = process_create();
// 创建主线程与其 Cap
thread_cap = thread_create_main(root_process, ROOT_THREAD_STACK_BASE,
ROOT_THREAD_STACK_SIZE,
ROOT_THREAD_PRIO, TYPE_ROOT,
0, binary, bin_name);
// 将主线程 Cap 加入进程
root_thread = obj_get(root_process, thread_cap, TYPE_THREAD);
/* Enqueue: put init thread into the ready queue */
obj_put(root_thread);

View File

@ -256,10 +256,12 @@ int thread_create_main(struct process *process, u64 stack_base,
u64 stack;
u64 pc;
// 创建虚拟地址空间
init_vmspace = obj_get(process, VMSPACE_OBJ_ID, TYPE_VMSPACE);
obj_put(init_vmspace);
/* Allocate and setup a user stack for the init thread */
// 创建用户栈 PMO 对象,并设置 Cap
stack_pmo = obj_alloc(TYPE_PMO, sizeof(*stack_pmo));
if (!stack_pmo) {
ret = -ENOMEM;
@ -272,11 +274,13 @@ int thread_create_main(struct process *process, u64 stack_base,
goto out_free_obj_pmo;
}
// 映射线程用户栈
ret = vmspace_map_range(init_vmspace, stack_base, stack_size,
VMR_READ | VMR_WRITE, stack_pmo);
BUG_ON(ret != 0);
/* init thread */
// 初始化线程对象
thread = obj_alloc(TYPE_THREAD, sizeof(*thread));
if (!thread) {
ret = -ENOMEM;
@ -286,15 +290,19 @@ int thread_create_main(struct process *process, u64 stack_base,
/* Fill the parameter of the thread struct */
stack = stack_base + stack_size;
// 加载程序各 ELF 段
pc = load_binary(process, init_vmspace, bin_start, &meta);
// 设置栈
prepare_env((char *)phys_to_virt(stack_pmo->start) + stack_size,
stack, &meta, bin_name);
stack -= ENV_SIZE_ON_STACK;
// 创建线程上下文
ret = thread_init(thread, process, stack, pc, prio, type, aff);
BUG_ON(ret != 0);
// 取得线程 Cap
thread_cap = cap_alloc(process, thread, 0);
if (thread_cap < 0) {
ret = thread_cap;
@ -302,6 +310,7 @@ int thread_create_main(struct process *process, u64 stack_base,
}
/* L1 icache & dcache have no coherence */
// 清空 L1 缓存
flush_idcache();
// return thread;

View File

@ -150,3 +150,7 @@ ELF Section、Segment 两个概念。
**** 切换上下文
返回上下文结构体的首地址就行
*** 练习2
process_create_root、thread_create_main 直接写在注释里面了。