Lecture 2 - OS Structure¶
System Calls¶
- An interface which kernel provides to the user space to interact with the kernel.
- Syscall : proviledged instruction
Example Ilustration -- Write()¶
- First
printf()
function is called, thenwrite()
function is called. In thewrite()
function, there is a system call to the kernel. [with syscall number $0x1] - Then, go into kernel space, and the kernel will do the actual writing to the file descriptor.
1)
kernel_entry code
will be called -- Saved all user space registers 2) callswrite syscall handler
-- Get from syscall_table, which is an array
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count)
{
return ksys_write(fd, buf, count);
}
- After write finish, call
ret_to_user
- Restore all saved user space registers
- Transfer control flow to user space
System Call Implementation¶
System-call interface
maintains a table indexed according to numbers assigned to each system call. Each entry in the table points to the entry point of the system call.
- The system call interface invokes the intended system call in OS kernel and returns status of the system call and any return values
- The caller needs to know nothing about how the system call is implemented
Example : Copy a file¶
- Use
strace
to trace system calls strace cp file1 file2
strace -c cp file1 file2
-- Count the number of system calls
2>&1
-- Redirect stderr to stdout|wc -l
-- Count the number of lines
Time spent in system calls¶
time ap start_kernel
-- Time spent in system calls
-
time
count multiple pid and sum them up, so it seems that the time spent in system calls is more than the actual time spent in the system calls. -
time grep -Rn start_kernel
System Call Parameter Passing¶
Three general methods used to pass parameters to the OS
- Simplest: pass the parameters in registers
- In some cases, may be more parameters than registers
- Parameters stored in a block, or table, in memory, and address of block passed as a parameter in a register
- This approach taken by Linux and Solaris
- Parameters placed, or pushed, onto the stack by the program and popped off the stack by the operating system
- Block and stack methods do not limit the number or length of parameters being passed
System Service¶
Linkers and Loaders¶
- Where does static variable goes? --
.data
section - Where does static constant goes? --
.rodata
section -
Why unintialized in
.bss
instead of.data
? -- To save space, as.bss
is not stored in the file -
readelf -h main
- Entry Address
- Magic Number :
0x7f 0x45 0x4c 0x46
-- ELF : Linux defines the format of the file using magic number
Linking¶
Static Linking¶
- All needed code is packed in single binary, leading to large binary
- 可以移植性强
Dynamic linking¶
- Reuse libraries to reduce ELF file size.
- How to resolve library calls?
- This dump the
.interp
section of the ELF file, which contains the path of the dynamic linker - It is the loader who resolves lib calls.
- lib call: like
printf()
- loader:
ld-linux-aarch64.so.1
Running a Binary¶
- While for static linking, the mapping is much less.
- Memory layout is in user space ?
- User space: stack, heap, data, text
- Kernel space: kernel code, kernel data, kernel stack
Questions¶
- Who setups ELF file mapping? -- kernel: execve() system call
- Who setups stack and heap?
- Who setups libraries?
- Dynamic linking has to do more system calls
Setup a Binary¶
Static Binary¶
- In
readelf
we see that entry of themain.static
is0x400640
- We find that
0x400640
is the address of thestart
function in themain.static
binary objdump-d a.out
regs->pc = pc
herepc
is the address of thestart
function --elf_entry
Dynamic Binary¶
- For dynamic binary, the elf_entry --
interp_elf_ex -> e_entry
ld.so
-- Loader resolves the library calls- So loader has to be called first, then the
start
function
Why Applications are Operating System Specific¶
System calls are different -- name / number
Apps can be multi-operating system * Written in interpreted language like Python, Ruby, and interpreter available on multiple operating systems * App written in language that includes a VM containing the running app (like Java) * Use standard language (like C), compile separately on each operating system to run on each
Application Binary Interface (ABI) is architecture equivalent of API, defines how different components of binary code can interface for a given operating system on a given architecture, CPU, etc
Operating-System Design and Implementation¶
Operating System Structure¶
General-purpose OS is very large program
Various ways to structure ones
- Simple structure –MS-DOS
- Monolithic –Unix, Linux
- Layered –an abstraction
-
Microkernel –Mach
Building and Booting an Operating System¶
Operating System Debugging¶
创建日期: 2024年9月30日 11:27:07