1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use core::ops::Deref;
use core::ptr::copy_nonoverlapping;
use core::str::from_utf8;
extern crate alloc;
use alloc::sync::Arc;
use alloc::{
    string::{String, ToString},
    vec,
    vec::Vec,
};
use axconfig::{MAX_USER_HEAP_SIZE, MAX_USER_STACK_SIZE, USER_HEAP_BASE, USER_STACK_TOP};
use axerrno::{AxError, AxResult};
use axhal::mem::VirtAddr;
use axhal::paging::MappingFlags;
use axhal::time::{current_time_nanos, NANOS_PER_MICROS, NANOS_PER_SEC};
use axhal::KERNEL_PROCESS_ID;
use axlog::{debug, info};
use axmem::MemorySet;

use axsignal::signal_no::SignalNo;
use axsync::Mutex;
use axtask::{current, yield_now, AxTaskRef, CurrentTask, TaskId, TaskState, IDLE_TASK, RUN_QUEUE};
use elf_parser::{
    get_app_stack_region, get_auxv_vector, get_elf_entry, get_elf_segments, get_relocate_pairs,
};
use xmas_elf::program::SegmentData;

use crate::flags::WaitStatus;
use crate::futex::clear_wait;
use crate::link::real_path;
use crate::process::{Process, PID2PC, TID2TASK};

use crate::signal::{send_signal_to_process, send_signal_to_thread};

/// 初始化内核调度进程
pub fn init_kernel_process() {
    let kernel_process = Arc::new(Process::new(
        TaskId::new().as_u64(),
        0,
        Mutex::new(Arc::new(Mutex::new(MemorySet::new_empty()))),
        0,
        vec![],
    ));

    axtask::init_scheduler();
    kernel_process.tasks.lock().push(Arc::clone(unsafe {
        IDLE_TASK.current_ref_raw().get_unchecked()
    }));
    PID2PC.lock().insert(kernel_process.pid(), kernel_process);
}

/// return the `Arc<Process>` of the current process
pub fn current_process() -> Arc<Process> {
    let current_task = current();

    let current_process = Arc::clone(PID2PC.lock().get(&current_task.get_process_id()).unwrap());

    current_process
}

/// 退出当前任务
pub fn exit_current_task(exit_code: i32) -> ! {
    let process = current_process();
    let current_task = current();

    let curr_id = current_task.id().as_u64();

    info!("exit task id {} with code _{}_", curr_id, exit_code);
    clear_wait(
        if current_task.is_leader() {
            process.pid()
        } else {
            curr_id
        },
        current_task.is_leader(),
    );
    // 检查这个任务是否有sig_child信号

    if current_task.get_sig_child() || current_task.is_leader() {
        let parent = process.get_parent();
        if parent != KERNEL_PROCESS_ID {
            // 发送sigchild
            send_signal_to_process(parent as isize, 17).unwrap();
        }
    }
    // clear_child_tid 的值不为 0,则将这个用户地址处的值写为0
    let clear_child_tid = current_task.get_clear_child_tid();
    if clear_child_tid != 0 {
        // 先确认是否在用户空间
        if process
            .manual_alloc_for_lazy(clear_child_tid.into())
            .is_ok()
        {
            unsafe {
                *(clear_child_tid as *mut i32) = 0;
            }
        }
    }
    if current_task.is_leader() {
        loop {
            let mut all_exited = true;

            for task in process.tasks.lock().deref() {
                if !task.is_leader() && task.state() != TaskState::Exited {
                    all_exited = false;
                    break;
                }
            }
            if !all_exited {
                yield_now();
            } else {
                break;
            }
        }
        TID2TASK.lock().remove(&curr_id);
        process.set_exit_code(exit_code);

        process.set_zombie(true);

        process.tasks.lock().clear();
        process.fd_manager.fd_table.lock().clear();

        process.signal_modules.lock().clear();

        let mut pid2pc = PID2PC.lock();
        let kernel_process = pid2pc.get(&KERNEL_PROCESS_ID).unwrap();
        // 将子进程交给idle进程
        // process.memory_set = Arc::clone(&kernel_process.memory_set);
        for child in process.children.lock().deref() {
            child.set_parent(KERNEL_PROCESS_ID);
            kernel_process.children.lock().push(Arc::clone(child));
        }
        if let Some(parent_process) = pid2pc.get(&process.get_parent()) {
            parent_process.set_vfork_block(false);
        }
        pid2pc.remove(&process.pid());
        drop(pid2pc);
        drop(process);
    } else {
        TID2TASK.lock().remove(&curr_id);
        // 从进程中删除当前线程
        let mut tasks = process.tasks.lock();
        let len = tasks.len();
        for index in 0..len {
            if tasks[index].id().as_u64() == curr_id {
                tasks.remove(index);
                break;
            }
        }
        drop(tasks);

        process.signal_modules.lock().remove(&curr_id);
        drop(process);
    }
    RUN_QUEUE.lock().exit_current(exit_code);
}

/// 返回应用程序入口,用户栈底,用户堆底
pub fn load_app(
    name: String,
    mut args: Vec<String>,
    envs: &Vec<String>,
    memory_set: &mut MemorySet,
) -> AxResult<(VirtAddr, VirtAddr, VirtAddr)> {
    if name.ends_with(".sh") {
        args = [vec![String::from("busybox"), String::from("sh")], args].concat();
        return load_app("busybox".to_string(), args, envs, memory_set);
    }
    let elf_data = if let Ok(ans) = axfs::api::read(name.as_str()) {
        ans
    } else {
        // exit(0)
        return Err(AxError::NotFound);
    };
    let elf = xmas_elf::ElfFile::new(&elf_data).expect("Error parsing app ELF file.");
    debug!("app elf data length: {}", elf_data.len());
    if let Some(interp) = elf
        .program_iter()
        .find(|ph| ph.get_type() == Ok(xmas_elf::program::Type::Interp))
    {
        let interp = match interp.get_data(&elf) {
            Ok(SegmentData::Undefined(data)) => data,
            _ => panic!("Invalid data in Interp Elf Program Header"),
        };

        let interp_path = from_utf8(interp).expect("Interpreter path isn't valid UTF-8");
        // remove trailing '\0'
        let interp_path = interp_path.trim_matches(char::from(0)).to_string();
        let real_interp_path = real_path(&interp_path);
        args = [vec![real_interp_path.clone()], args].concat();
        return load_app(real_interp_path, args, envs, memory_set);
    }
    info!("args: {:?}", args);
    let elf_base_addr = Some(0x400_0000);
    axlog::warn!("The elf base addr may be different in different arch!");
    // let (entry, segments, relocate_pairs) = parse_elf(&elf, elf_base_addr);
    let entry = get_elf_entry(&elf, elf_base_addr);
    let segments = get_elf_segments(&elf, elf_base_addr);
    let relocate_pairs = get_relocate_pairs(&elf, elf_base_addr);
    for segment in segments {
        memory_set.new_region(
            segment.vaddr,
            segment.size,
            segment.flags,
            segment.data.as_deref(),
            None,
        );
    }

    for relocate_pair in relocate_pairs {
        let src: usize = relocate_pair.src.into();
        let dst: usize = relocate_pair.dst.into();
        let count = relocate_pair.count;
        unsafe { copy_nonoverlapping(src.to_ne_bytes().as_ptr(), dst as *mut u8, count) }
    }

    // Now map the stack and the heap
    let heap_start = VirtAddr::from(USER_HEAP_BASE);
    let heap_data = [0_u8].repeat(MAX_USER_HEAP_SIZE);
    memory_set.new_region(
        heap_start,
        MAX_USER_HEAP_SIZE,
        MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER,
        Some(&heap_data),
        None,
    );
    info!(
        "[new region] user heap: [{:?}, {:?})",
        heap_start,
        heap_start + MAX_USER_HEAP_SIZE
    );

    let auxv = get_auxv_vector(&elf, elf_base_addr);

    let stack_top = VirtAddr::from(USER_STACK_TOP);
    let stack_size = MAX_USER_STACK_SIZE;

    let (stack_data, stack_bottom) = get_app_stack_region(args, envs, auxv, stack_top, stack_size);
    memory_set.new_region(
        stack_top,
        stack_size,
        MappingFlags::USER | MappingFlags::READ | MappingFlags::WRITE,
        Some(&stack_data),
        None,
    );
    info!(
        "[new region] user stack: [{:?}, {:?})",
        stack_top,
        stack_top + stack_size
    );
    Ok((entry, stack_bottom.into(), heap_start))
}

/// 当从内核态到用户态时,统计对应进程的时间信息
pub fn time_stat_from_kernel_to_user() {
    let curr_task = current();
    curr_task.time_stat_from_kernel_to_user(current_time_nanos() as usize);
}

#[no_mangle]
/// 当从用户态到内核态时,统计对应进程的时间信息
pub fn time_stat_from_user_to_kernel() {
    let curr_task = current();
    curr_task.time_stat_from_user_to_kernel(current_time_nanos() as usize);
}

/// 统计时间输出
/// (用户态秒,用户态微秒,内核态秒,内核态微秒)
pub fn time_stat_output() -> (usize, usize, usize, usize) {
    let curr_task = current();
    let (utime_ns, stime_ns) = curr_task.time_stat_output();
    (
        utime_ns / NANOS_PER_SEC as usize,
        utime_ns / NANOS_PER_MICROS as usize,
        stime_ns / NANOS_PER_SEC as usize,
        stime_ns / NANOS_PER_MICROS as usize,
    )
}

/// To deal with the page fault
pub fn handle_page_fault(addr: VirtAddr, flags: MappingFlags) {
    axlog::debug!("'page fault' addr: {:?}, flags: {:?}", addr, flags);
    let current_process = current_process();
    axlog::debug!(
        "memory token : {:#x}",
        current_process.memory_set.lock().lock().page_table_token()
    );

    if current_process
        .memory_set
        .lock()
        .lock()
        .handle_page_fault(addr, flags)
        .is_ok()
    {
        axhal::arch::flush_tlb(None);
    } else {
        let _ = send_signal_to_thread(current().id().as_u64() as isize, SignalNo::SIGSEGV as isize);
    }
}

/// 在当前进程找对应的子进程,并等待子进程结束
/// 若找到了则返回对应的pid
/// 否则返回一个状态
///
/// # Safety
///
/// 保证传入的 ptr 是有效的
pub unsafe fn wait_pid(pid: isize, exit_code_ptr: *mut i32) -> Result<u64, WaitStatus> {
    // 获取当前进程
    let curr_process = current_process();
    let mut exit_task_id: usize = 0;
    let mut answer_id: u64 = 0;
    let mut answer_status = WaitStatus::NotExist;
    for (index, child) in curr_process.children.lock().iter().enumerate() {
        if pid == -1 {
            // 任意一个进程结束都可以的
            answer_status = WaitStatus::Running;
            if let Some(exit_code) = child.get_code_if_exit() {
                answer_status = WaitStatus::Exited;
                info!("wait pid _{}_ with code _{}_", child.pid(), exit_code);
                exit_task_id = index;
                if !exit_code_ptr.is_null() {
                    unsafe {
                        // 因为没有切换页表,所以可以直接填写
                        *exit_code_ptr = exit_code << 8;
                    }
                }
                answer_id = child.pid();
                break;
            }
        } else if child.pid() == pid as u64 {
            // 找到了对应的进程
            if let Some(exit_code) = child.get_code_if_exit() {
                answer_status = WaitStatus::Exited;
                info!("wait pid _{}_ with code _{:?}_", child.pid(), exit_code);
                exit_task_id = index;
                if !exit_code_ptr.is_null() {
                    unsafe {
                        *exit_code_ptr = exit_code << 8;
                        // 用于WEXITSTATUS设置编码
                    }
                }
                answer_id = child.pid();
            } else {
                answer_status = WaitStatus::Running;
            }
            break;
        }
    }
    // 若进程成功结束,需要将其从父进程的children中删除
    if answer_status == WaitStatus::Exited {
        curr_process.children.lock().remove(exit_task_id);
        return Ok(answer_id);
    }
    Err(answer_status)
}

/// 以进程作为中转调用task的yield
pub fn yield_now_task() {
    axtask::yield_now();
}

/// 以进程作为中转调用task的sleep
pub fn sleep_now_task(dur: core::time::Duration) {
    axtask::sleep(dur);
}

/// current running task
pub fn current_task() -> CurrentTask {
    axtask::current()
}

/// 设置当前任务的clear_child_tid
pub fn set_child_tid(tid: usize) {
    let curr = current_task();
    curr.set_clear_child_tid(tid);
}

/// Get the task reference by tid
pub fn get_task_ref(tid: u64) -> Option<AxTaskRef> {
    TID2TASK.lock().get(&tid).cloned()
}