MCPcopy Create free account
hub / github.com/chyyuu/os_kernel_lab / new

Method new

os/src/task/process.rs:74–132  ·  view source on GitHub ↗
(elf_data: &[u8])

Source from the content-addressed store, hash-verified

72 }
73
74 pub fn new(elf_data: &[u8]) -> Arc<Self> {
75 // memory_set with elf program headers/trampoline/trap context/user stack
76 let (memory_set, ustack_base, entry_point) = MemorySet::from_elf(elf_data);
77 // allocate a pid
78 let pid_handle = pid_alloc();
79 let process = Arc::new(Self {
80 pid: pid_handle,
81 inner: unsafe {
82 UPIntrFreeCell::new(ProcessControlBlockInner {
83 is_zombie: false,
84 memory_set,
85 parent: None,
86 children: Vec::new(),
87 exit_code: 0,
88 fd_table: vec![
89 // 0 -> stdin
90 Some(Arc::new(Stdin)),
91 // 1 -> stdout
92 Some(Arc::new(Stdout)),
93 // 2 -> stderr
94 Some(Arc::new(Stdout)),
95 ],
96 signals: SignalFlags::empty(),
97 tasks: Vec::new(),
98 task_res_allocator: RecycleAllocator::new(),
99 mutex_list: Vec::new(),
100 semaphore_list: Vec::new(),
101 condvar_list: Vec::new(),
102 })
103 },
104 });
105 // create a main thread, we should allocate ustack and trap_cx here
106 let task = Arc::new(TaskControlBlock::new(
107 Arc::clone(&process),
108 ustack_base,
109 true,
110 ));
111 // prepare trap_cx of main thread
112 let task_inner = task.inner_exclusive_access();
113 let trap_cx = task_inner.get_trap_cx();
114 let ustack_top = task_inner.res.as_ref().unwrap().ustack_top();
115 let kstack_top = task.kstack.get_top();
116 drop(task_inner);
117 *trap_cx = TrapContext::app_init_context(
118 entry_point,
119 ustack_top,
120 KERNEL_SPACE.exclusive_access().token(),
121 kstack_top,
122 trap_handler as usize,
123 );
124 // add main thread to the process
125 let mut process_inner = process.inner_exclusive_access();
126 process_inner.tasks.push(Some(Arc::clone(&task)));
127 drop(process_inner);
128 insert_into_pid2process(process.getpid(), Arc::clone(&process));
129 // add main thread to scheduler
130 add_task(task);
131 process

Callers

nothing calls this directly

Calls 13

pid_allocFunction · 0.85
cloneFunction · 0.85
dropFunction · 0.85
insert_into_pid2processFunction · 0.85
add_taskFunction · 0.85
get_trap_cxMethod · 0.80
ustack_topMethod · 0.80
get_topMethod · 0.80
exclusive_accessMethod · 0.80
getpidMethod · 0.80
tokenMethod · 0.45

Tested by

no test coverage detected