[RustSBI output]
[kernel] Hello, world!
AAAAAAAAAAA [1/5]
BBBBBBBBBB [1/2]
....
CCCCCCCCCC [2/3]
AAAAAAAAAAA [3/5]
Test write_b OK!
[kernel] Application exited with code 0
CCCCCCCCCC [3/3]
...
[kernel] Application exited with code 0
[kernel] Panicked at src/task/mod.rs:106 All applications completed!
Outline
Lab Objectives and Steps
2. Multiprogramming Batch OS design
Application Design
Sawtooth OS: Support Application Loading
Eoraptor OS: Support Multiprogramming and Cooperative Scheduling
Coelophysis OS: Time-sharing Multi-task OS
Software Architecture
Code Structure: Application
Build application
└── user
├── build.py (Add: use build.py to build applications so that the physical address space they occupy are disjoint)
├── Makefile (Modify: use build.py to build applications)
└── src (various applications)
Code structure: Complete task management
Improve OS: Loader module loads and executes programs
├── os
│ └── src
│ ├── batch.rs (Delete: This is divided into two submodules, loader and task)
│ ├── config.rs (Add: Save some kernel configurations)
│ ├── loader.rs (Add: Load the application into memory and manage it)
│ ├── main.rs (Modify: Modify the main function)
│ ├── syscall (Modify: Add some syscalls)
Code structure: Process switching
Improve OS: TaskManager module manages/switches execution of programs
├── os
│ └── src
│ ├── task (Add: task sub-module is mainly responsible for task management)
│ │ ├── context.rs (Introduce Task context `TaskContext` )
│ │ ├── mod.rs (Global task manager and provides some interfaces to other modules)
│ │ ├── switch.rs (Interprets the assembly code of task switching as the Rust interface __switch)
│ │ ├── switch.S (Assembly code for task switching)
│ │ └── task.rs (Definition of task control block `TaskControlBlock` and task status `TaskStatus`)
Outline
Lab Objectives and Steps
Multiprogramming Batch OS design
3. Application Design
Sawtooth OS: Support Application Loading
Eoraptor OS: Support Multiprogramming and Cooperative Scheduling
Since different applications will be loaded to different locations, the BASE_ADDRESS in their linker script linker.ld are all different.
Write a script customization tool build.py, which customizes its own link script for each application
Application start address = base address + number * 0x20000
Yield system call
//00write_a.rsfnmain() -> i32 {
for i in0..HEIGHT {
for _ in0..WIDTH {
print!("A");
}
println!(" [{}/{}]", i + 1, HEIGHT);
yield_(); //yield the CPU
}
println!("Test write_a OK!");
0
}
Eoraptor OS: Support Multiprogramming and Cooperative Scheduling
Coelophysis OS: Time-sharing Multi-task OS
Sawtooth OS: Support application loading
The Permian "sawtooth OS" supports multiple applications residing in memory to form a multiprogramming operating system – Multiprog OS;
MultiProgramming Loading
Apps are loaded in different ways.
All applications are loaded into memory when kernel is being initialized
To avoid overwriting, they need to be loaded to different physical addresses
MultiProgramming Loading
fnget_base_i(app_id: usize) -> usize {
APP_BASE_ADDRESS + app_id * APP_SIZE_LIMIT
}
let base_i = get_base_i(i);
// load app from data section to memorylet src = (app_start[i]..app_start[i + 1]);
let dst = (base_i.. base_i+src. len());
dst.copy_from_slice(src);
Program execution
Execution timing
When the initialization of the multiprogramming is complete
When an application ends or an error occurs
How to switch?
Call the run_next_app function to switch to the first/next application
Switch to the next program
From kernel mode to user mode
From user mode to kernel mode
Switch to the next program
Jump to application i (The entry point of application i is entry(i))
Switch to the user stack stack(i)
Program Execution
Now we complete "Sawtooth OS", which supports loading applications into memory
Outline
...
Sawtooth OS: Support Application Loading
5. Eoraptor OS: Support Multiprogramming and Cooperative Scheduling
Task switching
Trap control flow switching
Cooperative Scheduling
Coelophysis OS: Time-sharing Multi-task OS
Support multiprogramming cooperative scheduling
Cooperative multi-programming: One application actively yields the CPU and switches to another application to continue execution, thereby improving the overall efficiency of the system;
Task switching
Process
Process: A dynamic execution process on a data collection of a program with certain independent functions. Also known as Task.
Time slice
The execution/idle slice in a time slice of application execution is called "computing task slice" or "idle task slice", collectively called task slice
A task switch represents the switch between Trap control flows in the kernel from two different applications
Task switching does not involve privilege level switching; while Trap switching involves privilege level switching
Task switching only saves the partial registers that the compiler agrees that callee function should save; while Trap switching needs to save all general-purpose registers
Both Task switching and Trap switching are transparent to the applications
Control Flow
Control flow of the program -- compilation principle
The execution sequence in units of instructions, statements or basic blocks of a program
Control flow of the processor -- principle of computer composition
The control transfer sequence of the program counter in the processor
Common Control Flow: control flow from the perspective of application programmer
Control flow is the execution sequence of an application program written by the application programmer, and these sequences are preset by the programmer
Called Common control flow (CCF)
Exceptional Control Flow: control flow from the perspective of OS programmer
During the execution of the application program, if a system call request is issued, a peripheral interrupt or CPU exception occurs, the previous instruction is still in the code segment of the application program, and the latter instruction will run into the code segment of the OS
This is a "mutation" of the control flow, that is, the control flow breaks away from its execution environment and produces a switch of the execution environment
This "mutant" control flow is called Exceptional Control Flow (ECF)
Control Flow Context (the State of the Execution Environment)
The execution process of common control flow or exceptional control flow, from the perspective of hardware
Starting from the execution of an instruction at the beginning of the control flow, the contents of all physical resources accessible by the instruction, including all general-purpose registers, privilege-related special registers, and memory accessed by the instruction, will gradually change with the execution of the instruction
The physical resource content of the control flow after executing a certain instruction, that is, the physical/virtual resource content that ensures that the control flow instruction can continue to be executed correctly at the next moment is called Control Flow Context (Context), it can also be referred to as the state of the execution environment in which the control flow occurs
For the currently practiced OS, there is no virtual resource, and the content of the physical resource is general register/CSR register
Control Flow Context (the State of the Execution Environment)
Function call context
The control flow context during a function call (executing a function switch)
Interrupt/exception/trap context
The control flow context when handling interrupt/exception/trap switching code in the OS
Task (process) context
The control flow context when tasks (processes) in the OS execute related switching codes
Outline
...
Sawtooth OS: Support Application Loading
Eoraptor OS: Support Multiprogramming and Cooperative Scheduling
Task Switching
Trap control flow switching
Cooperative Scheduling
Coelophysis OS: Time-sharing Multi-task OS
The challenge of OS: task switching
Perform hacker-level operations between two Trap control flows belonging to different tasks, that is, perform Trap context switching to achieve task switching.
Where is the Trap context?
Where is the Task context?
How to switch tasks?
Where should task switching occur?
Can the task switch back after switching?
Trap control flow switching: pause operation
a special function __switch()
During the period after calling __switch() until it returns, the original Trap control flow A will be suspended and switched out, and the CPU will run another Trap control flow applied in the kernel
Trap control flow switching: resume operation
A special function __switch()
Then at an appropriate moment, the original Trap control flow A will switch back from a certain Trap control flow C (probably not the B it switched to before) to continue execution and finally return
From the perspective of implementation, the core difference between a __switch() function and a normal function is that it switches the stack
Trap control flow switching function __switch()
Trap control flow switching process: state before switching
Stage [1]: Before the Trap control flow A calls __switch(), A’s kernel stack only has the Trap context and the call stack information of the Trap processing function, and B was switched out before
Trap control flow switching process: save task context A
Stage [2]: A saves the current register snapshot of CPU in the task context A's space
Trap control flow switching process: restore task context B
Phase [3]: Read the task context B pointed to by next_task_cx_ptr, restore the ra register, s0~s11 register and sp register
After this step, __switch() can execute a function across two control flows, that is, the switching of the control flow is realized by switching the stack
Trap control flow switching process: execute task code B
Stage [4]: After the CPU executes the ret assembly pseudo-instruction and completes the return of the __switch() function, task B can continue to execute downwards from the position where __switch() was called
__switch() is switched to the kernel stack of task B by restoring the sp register, realizing the switching of control flow, so that a function can be executed across two control flows
If it can be done, we will realize the Eoraptor OS that supports multiprogramming and cooperative scheduling
Outline
LAB Objectives and Steps
Multiprogramming Batch OS design
Application Design
Sawtooth OS: Support Application Loading
Eoraptor OS: Support Multiprogramming and Cooperative Scheduling
6. Coelophysis OS: Time-sharing Multi-task OS
Coelophysis OS: time-sharing multi-task OS
The Triassic "Coelophysis" OS – Time-sharing OS can completely preempt the execution of applications, so that multiple applications can be executed in a fair and efficient time-sharing manner, and the overall efficiency of the system can be improved
The basic idea of time-sharing multi-task OS
Set clock interrupt
Count the time slice used by the task after receiving the clock interrupt
Chapter 3: Multiprogramming and time-sharing multitasking -> chapter3 practice -> get task information -> add a system call sys_task_info()
LAB submission requirements
Mar. 25, 2023;
get some concept from https://www.tutorialandexample.com/types-of-operating-system
Sawtooth Archosaurus Coelophysis
- J. Lyons & Co. for commercial transactions
J. Lyons & Co. is a British restaurant chain, food manufacturing and hotel group founded in 1884.
https://baike.baidu.com/item/EDSAC/7639053
Electronic Delay Storage Automatic Calculator (English: Electronic Delay Storage Automatic Calculator, EDSAC) is an early British computer. In 1946, Professor Maurice Wilkes of the Mathematics Laboratory of the University of Cambridge and his team were inspired by von Neumann's First Draft of a Report on the EDVAC, and designed and built EDSAC based on EDVAC. It was officially put into operation on May 6, 1949. It is the world's first stored-program electronic computer that actually runs.
It was EDSAC that also encountered difficulties in project implementation: not technology, but lack of funds. At the crucial moment, Wilkes successfully persuaded J. Lyons & Co. . The boss invested in the project and finally brought the plan back to life. On May 6, 1949, EDSAC was successfully tested for the first time. It read a program to generate a square table from the tape and executed it, and printed the result correctly. In return for the investment, LyOHS obtained the right to mass-produce EDSAC, which is the LEO computer (Lyons Electronic Office) that was officially put on the market in 1951, which is generally considered to be the first commercialized computer model in the world, so This has also become an interesting fact in the history of computer development: the first manufacturer to produce a commercial computer turned out to be a bakery. Lyons company later became part of the famous "International Computer Co., Ltd.", or ICL, in the UK.