跳转至

Lecture 2 - OS Structure

System Calls

  • An interface which kernel provides to the user space to interact with the kernel.
  • Syscall : proviledged instruction

3

Example Ilustration -- Write()

0 1 2

  • First printf() function is called, then write() function is called. In the write() 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) calls write 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

4

Example : Copy a file

5

  • Use strace to trace system calls
  • strace cp file1 file2
  • strace -c cp file1 file2 -- Count the number of system calls
strace cp main.c main.copy 2>&1 |wc -l #175 lines
  • 2>&1 -- Redirect stderr to stdout
  • |wc -l -- Count the number of lines

7 6

Time spent in system calls

  • time ap start_kernel -- Time spent in system calls

8

  • 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

9

System Call Parameter Passing

10

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

11

Linkers and Loaders

12 13

  • 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 15

  • readelf -h main

  • Entry Address
  • Magic Number : 0x7f 0x45 0x4c 0x46 -- ELF : Linux defines the format of the file using magic number

14

Linking

Static Linking

  • All needed code is packed in single binary, leading to large binary
  • 可以移植性强

16

Dynamic linking

  • Reuse libraries to reduce ELF file size.
  • How to resolve library calls?
readelf -p .interp main
  • 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

17

Running a Binary

18

19

  • While for static linking, the mapping is much less.

20

  • 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

23

  • Who setups stack and heap?
  • Who setups libraries?

24

21 22

  • Dynamic linking has to do more system calls

Setup a Binary

Static Binary

  • In readelf we see that entry of the main.static is 0x400640
  • We find that 0x400640 is the address of the start function in the main.static binary
  • objdump-d a.out

25

26

  • regs->pc = pc here pc is the address of the start function -- elf_entry

27

Dynamic Binary

28 29 30

  • 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

30

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

31

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

  • Good Helper Website

Building and Booting an Operating System

Operating System Debugging


最后更新: 2024年9月30日 11:27:07
创建日期: 2024年9月30日 11:27:07