# Lab5: Process

## 0x00 The Concepts and Syscalls

It’s hard to define what a process is. Usually it is a procedure in which the OS selects an executable file and performs the dynamic execution. During the execution there will be many interaction between the process and the hardware or virtual resources, and we know those are handled by the OS through syscalls. Besides, there are some important syscalls specially made for process management: fork/exec/waitpid:

• fork: When a process (let’s name it A) call fork, the kernel will create a new process (let’s name it B) which is almost identical to A: they have exactly same stack, .text segment or other data segment content, and every registers except a0 which stores the return value of the syscall. They are in different address spaces but the bytes stores in these address space are exactly same at the moment fork returns. The only way a process can figure out whether it is the new process or the old parent is the returen value of fork: 0 for the new born process and pid of child process for the parent. This parent-child relation is very important in unix-like OS.
• exec: This will help us run a new program in the current address space, use it together with fork we can easily create a process that runs a new program.
• waitpid: When a process returns, the memory resources it have consumed cannot be fully recycled through the exit syscall, for eaxmple the current kernel stack. A typical solution for this is to mark the process as zombie, then it’s parent process do the rest recycle work and get the exit status through the waitpid syscall.

## 0x01 Data Structures for Process

RAII is heavily used to help us safe memory management. For a process, we bind its pid, kernel stack, and address space(MemorySet) to a TaskControlBlock. The TCBs ares stored in a tree formed by the parent-child relations(created through fork&exec) between processes:

pub struct TaskControlBlock {
// immutable
pub pid: PidHandle,
pub kernel_stack: KernelStack,
// mutable
}

pub struct TaskControlBlockInner {
pub trap_cx_ppn: PhysPageNum,
pub base_size: usize,
pub priority: usize,
pub pass: usize,
pub memory_set: MemorySet,
pub parent: Option<Weak<TaskControlBlock> >,
pub children: Vec<Arc<TaskControlBlock> >,
pub exit_code: i32,
}


Here we use alloc::sync::Weak to wrap the parent pointer so that there will be no cyclic refernces between the parent and it’s child.

Another significant modification from previous chapter is that we split the original task manager module into Processor and TaskManager.
– The Processor module maintains the CPU’s states, including current process and idle task context. In a single-core CPU environment, we only create one gloable instance of Processor.
– The TaskManager stores all Arcs in a BTreeMap, so that we can easily fetch/remove or add/insert tasks(processes) with our scheduler.

pub struct TaskManager {
scheduler: Box<Scheduler>,
}

pub fn new() -> Self {
let stride_scheduler: Scheduler = Scheduler::new();
Self {
scheduler: Box::new(stride_scheduler),
}
}

// update pass for stride scheduler
}

pub fn fetch(&mut self) -> Option<Arc<TaskControlBlock>> {
let next_pid = loop {
if let Some(next_pid) = self.scheduler.find_next_task() {
// TODO how about wait state
break next_pid
}
} else {
return None;
}

};
}
}


# Lab4: Address Space

Address Space is a significant mechanism in modern operating system. By adding an abstract layer between code and physical memory, it frees the developers from the painful memory arrangement work, helping them focus more on their code other than the hardware stuff.

The following figure gives an overview of how Address Space works:

Having Address Space enabled, the codes can only see the Virtual Address. If a process wants to access any address virt_addr, it will be first translated to Physical Address by CPU’s MMU module according to the process’s page table.

## 0x00 Hardware supports for Multilevel Paging in RISCV

The MMU is disabled by default, thus previously any program are able to access any physical memory. We can enbale the MMU by setting a register named satp:

The above figure shows the meaning of bits in satp:

• MODE controls how the MMU translate address. When MODE = 0 the MMU is disabled, and when ‘MODE’ = 8 the MMU use page table mechanism to translate the address.
• ASID indetifies the address space by id, since we don’t have process implemented yet we just ignore it.
• PPN is the physical page number of the root page table entry.

The address format under page table mechanism consists of two parts: page number and offset:

Each page table entry consists of 3 level virtual page number (vpn) and several flag bits:

With these knowledge we can easily understand how the MMU translates virtual memory address:

### TLB

TLB (Translation Lookaside Buffer) works like some kinds of cache, note that we have to use sfence.vma instruction to refresh it after we change satp or any page table entry.

## 0x01 Address Space of Kernel and User

After satp is enabled, the memory of kernel and user applications are seperated, we need to carefully handle the interaction between different address spaces. In rCore, the designers use a Trampoline to bridge the kernel and usermode applications:

The virtual address of Trampoline is exactly same across each user spaces and the kernel space. Note that there is a guard page between kernel stacks. Those holes in the address space are settled to prevent buffer overflow damage in kernel stack.

The address space of the kernel is illustrated in the following figure:

The permissions here are critical for system security: no page table can be both writable and executable. Besides, we use identical mapping here, so the kernel can read/write any user space memory in an easy way.

In user mode, the address space is quite familiar to us:

We palce the TrapContext just under the Trampoline.

__all_traps and trap_return shoud take care of the address space switch. Note that for each task, we should set their TaskContext’s initial ra to trap_return. We don’t have the ra pushed in kernel stack for the first time to run a task so we have to handle this manually.

The syscall call stack is:

syscall: user space ecall -> __all_traps(trampoline) -> trap_handler -> do syscall -> trap_return -> __restore -> user space


The switch process is:

## Intel SGX: 基本概念

SGX是Intel实现的可信执行环境，主要面向服务器和桌面端，提供了内存加密(Memory Encryption)、访问控制(Access Control)、远程认证(Remote Attestation)、本地密封(Sealing)等功能。

## 0x00 Overview

1. 每个application分为两部分：安全(可信)部分和不安全(不可信)的部分
2. application启动后，会在受保护的可信内存中加载一块飞地(enclave)
3. application通过把机密数据、代码放到enclave里来保证安全性
4. enclave为application提供调用接口，当application调用enclave内的函数时，其内部的任何内存仅enclave自身可见
5. enclave内存即使ring 0的攻击者也看不到，因为是CPU层面的保护。实际上在SGX的安全模型里OS、BIOS等等都可以被认为是不可信的

1. application 本身包括了自身的代码、数据和enclave
2. enclave里面也有其自身的代码、数据
3. SGX保证enclave里面的代码和数据的integrity和confidentiality
4. enclave的entry points在编译期就确定了
5. enclave可以访问它所在的application里的内存，但是反过来不行
6. 支持多线程

## rCore-OS Lab2: Batch Processing and Privileges

In lab 1, we have made our code work on a bare-metal computer (simulated by QEMU) successfully. However, it can do nothing but print some strings we hardcoded in the program on the terminal. Of course you can make it more complicated, such as factoring a large number, calculating the inverse of a matrix, etc. That’s cool but there are two significant drawbacks of this approach:

1. The CPU runs a single program each time. Since the computing resources are precious(especially in the old time when you don’t have a modern OS), users who have many programs to run have to wait in front of the computer and manually load&start the next program after the previous one finished.
2. Nobody wants to write the SBI and assembly level stuff every time, and it’s a duplication of efforts.

In order to solve these problems, people invented the Simple Batch Processing System, which can load a batch of application programs and automatically execute them one by one. Besides, the Batch Processing System will provide some “library” code such as console output functions which may be reused by many programs.

A new problem arises when we use the batch process system: error handling. The user’s program may (often) run into errors, unconsciously or intentionally. We do not want the error of any program to affect others or the system, so the system should be able to handle these errors and terminate the programs when necessary. To achieve this goal we introduced the Privileges mechanism and isolate user’s code from the system, which we will refer to as user mode and kernel mode. Note that this mechanism requires some support from hardware, and we will illustrate that with code in the following parts.

## 0x00 Privileges mechanism

The underlying reason for implementing the privileges mechanism is the system cannot trust any submitted program. Any errors or attacks could happen and may corrupt the system. We have to restrict users’ programs in an isolated “harmless” environment, where they have no access to 1) arbitrary memory or 2) any over-powerful instructions which may break the computer. In this lab, we mainly focus on the last point.

Prohibiting users’ program from using privileged instructions need the help from CPU. In riscv64, 4 levels of privileges are designed:

Level Encode Name
0 00 U, User/Application
1 01 S, Supervisor
2 10 H, Hypervisor
3 11 M, Machine

All modes, except Machine, have to go through binary interfaces provided by higher levels to control the hardware. The privileges level and their relation in our scenario are shown in the following figure:

The binary interfaces between User mode and Supervisor mode are named Application Binary Interface (ABI), or another more famous one: syscall.

# 声明: 由于经济原因本人已无力继续维护此服务，有愿意接手的人可以点击本行与我联系，谢谢

1. 这个网站购买卡密
2. 在网页中点选 第三方付费接口
3. 将得到的卡密中的APP_IDAPP_KEY分别填入网页中提示填写的两个文本框内
4. 然后截图+粘贴使用即可

## [DP][扫描线][思维] Codeforces 1313D Happy New Year

### 题解：

(先吐槽下这题竟然2600分，这可是个div2D)

1. 当当前点为某线段的起点时，我们考虑每个 $S$ 中是否包含了第 $i$ 条线段，如果
• 包含了，那么 $dp[i][S]=dp[i-1][S^(1<<p)]+(i到第i+1个关键点的距离)*(S中二进制位为1的奇偶性)$ ,因为我们相当于加上了新的一段；(其中p为当前线段所应在的二进制位，下同)
• 没包含，那么$dp[i][S]=dp[i][S]$ $+(i到第i+1个关键点的距离)*(S中二进制位为1的奇偶性)$，等于说我们的扫描线在这一段仍然是按照之前的状态在扫描。
2. 当当前点为某线段的终点时，我们仍然考虑每个 $S$ 中是否包含了第 $i$ 条线段，如果
• 包含了，由于这条线段到此我们认为结束了，所以这种状态根本不合法，直接设置为$-INF$
• 没包含，那么$dp[i][S]=max(dp[i-1][S],dp[i-1][S^(1<<p)])$ $+(i到第i+1个关键点的距离)*(S中二进制位为1的奇偶性)$，因为我们前面一段可能选了大，也可能不选更大，我们需要取个最优解。

## MathpixCsharp: C#实现的Mathpix Windows开源客户端

21.3.28: 第三方Api支付服务已修复。

## 不填appid和appkey是没法用的

👆强调一下，因为最近没有看使用说明就来跟我说用不了的人太多了。要么注册官方的开发者api，要么使用第三方的api和key。

## 功能支持：

• [x] 截图识别公式转换为Latex代码
• [x] 截图识别公式转换为Office Word公式
• [x] 多显示器支持
• [x] 最小化到系统托盘
• [x] 快捷键截图，Ctrl+Alt+M（与官方客户端相同）
• [x] 官方API支持，每个月可免费用1000次，需要国外信用卡
• [x] 无需信用卡的第三方API支持

## [娱乐] 一个监控NIKE官网NBA球衣打折的QQ机器人

• Windows服务器一台，阿里云ECS即可（Linux也可以但是需要Wine，不如直接原生Windows）
• QQ小号一个，用来作为发送消息的载体

## JetBrains Quest 免费领取三个月全家桶 解密

48 61 76 65 20 79 6f 75 20 73 65 65 6e 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 6f 66 20 74 68 65 20 4a 65 74 42 72 61 69 6e 73 20 77 65 62 73 69 74 65 3f


Have you seen the source code of the JetBrains website?


      O
{o)xxx|===============-
O

Welcome to the JetBrains Quest.

What awaits ahead is a series of challenges. Each one will require a little initiative, a little thinking, and a whole lot of JetBrains to get to the end. Cheating is allowed and in some places encouraged. You have until the 15th of March at 12:00 CET to finish all the quests.
Getting to the end of each quest will earn you a reward.
Let the quest commence!

JetBrains has a lot of products, but there is one that looks like a joke on our Products page, you should start there... (hint: use Chrome Incognito mode)
It’s dangerous to go alone take this key: Good luck! == Jrrg#oxfn$O -===============|xxx(o} O  看到了hint说使用Chrome的无痕模式，那我们无痕模式打开一下Products 页面。顺便，注意他的一句话：带上这个key Good luck! == Jrrg#oxfn$

## 1. 回顾高等数学：多元数量函数的梯度

$$\nabla f_{\boldsymbol{x}} \overset{\underset{\mathrm{def}}{}}{=} \left[ \frac{\partial f }{\partial x_1}, \frac{\partial f }{\partial x_2},\cdots,\frac{\partial f }{\partial x_n} \right]=\frac{\partial f }{\partial \boldsymbol{x}}$$

\begin{aligned} df &= \frac{\partial f}{\partial x_1}dx_1+\frac{\partial f}{\partial x_2}dx_2+\cdots+\frac{\partial f}{\partial x_n}dx_n \\ &=\left[ \frac{\partial f }{\partial x_1}, \frac{\partial f }{\partial x_2},\cdots,\frac{\partial f }{\partial x_n} \right] \left[dx_1, dx_2,\cdots,dx_n \right]^T \\ &=\frac{\partial f }{\partial \boldsymbol{x}} d\boldsymbol{x} \end{aligned}