1 / 34

Linux System Call

Development Software Tools for Embedded Systems 4/23. Linux System Call. 9765531 李宜亭 9762581 顏偉倫 9762593 許宏榮. Outline. Introduction System Call List System Call Flow System Call Initialization Set System Gate System Call Number System Call Table Enter & Exit System Call

Download Presentation

Linux System Call

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Development Software Tools for Embedded Systems 4/23 Linux System Call 9765531 李宜亭 9762581 顏偉倫 9762593 許宏榮

  2. Outline • Introduction • System Call List • System Call Flow • System Call Initialization • Set System Gate • System Call Number • System Call Table • Enter & Exit System Call • Enter System Call - int 0x80 • Exit System Call - iret • Fast Enter & Exit System Call • sysenter_setup() • enable_sep_cpu() • Enter System Call – sysenter • Exit System Call - sysexit

  3. Outline • Int 0x80 vs sysenter & sysexit • Parameter passing • System Call Implementation • How to Use System Call ? • Reference Book

  4. Introduction • What is System Call ? • 應用程式與硬體之間的一層介面 • 功用 • Userspace中的process只能透過System Call來存取系統中的資源 • 優點 • 讓程式設計更為容易 • 增加系統安全性 • 讓程式更具移植性 • 使用System Call的方式 • Library API • ex. getpid(), write() • syscall() • 後面會講解範例 • 使用assembly code User Space Kernel Space Service Routine User Program System Call Handler Wrapper Routine (Glibc)

  5. System Call List (部份) System Call Name Third parameter Second parameter First parameter System Call Number

  6. System Call Flow User Space Kernel Space User App main() C Library libc_read() File System System Call sys_read() Kernel entry_32.S fread(c,1,30.filename) Push Argument Load Register EAX=__NR_READ INT 0x80 SAVE_ALL Check limit of EAX syscall_tab[EAX] Check Destination Retrieve Data Copy Data Return RESTORE_ALL iret Check Error return Return

  7. System Call Initialization (Set System Gate) init/main.c

  8. Set System Gate • 軟體interrupt • 當用戶模式行程發出int 0x80指令時,CPU會切換至核心模式,從system_call位址起,著手執行指令 arch/x86/include/asm/desc.h arch/x86/kernel/traps.c arch/x86/include/asm/irq_vectors.h

  9. System Call Number • 分配每一個system call一個唯一的number • 透過此number來判定user請求使用哪一個system call • number被存在%eax中 • kernel根據%eax中的number得知使用者請求的system call arch/x86/include/asm/unistd_32.h

  10. System Call Table • 用來儲存每一個system call的entry address • 長整數 = 4 bytes = 32 bits • 此address在建置vmlinux時被確定 • linker 負責計算出每一個system call的entry address,並將其填到sys_call_table相對應的表項中 arch/x86/kernel/entry_32.S

  11. Enter & Exit System Call • Enter System Call • int 0x80 • sysenter • Linux 2.6 • Intel Pentium II • Exit System Call • iret • sysexit • Linux 2.6 • Intel Pentium II

  12. Enter System Call - int 0x80 • System Call Handler • system_call() • 硬體儲存register • 軟體儲存register • SAVE_ALL arch/x86/kernel/entry_32.S

  13. Enter System Call - int 0x80 • 將系統中通用的register的值全部存到stack中 arch/x86/kernel/entry_32.S

  14. Exit System Call - iret • 當結束System Call時 • system_call()從%eax取得其return value • 將之存在”用來儲存%eax之用戶模式值”的stack裡 • 用戶模式行程就能在%eax中找到該System Call的return value • 將Interrupt關掉 • 使用system gate進入kernel mode時不會自動關interrupt • 將interrupt關掉,使其回到user mode期間不再接受外來的interrupt arch/x86/include/asm/irqflags.h arch/x86/kernel/entry_32.S

  15. Exit System Call - iret • 將儲存在stack中的值pop回register • RESTORE_REGS • 執行iret退出kernel space,返回user space • INTERRUPT_RETURN arch/x86/kernel/entry_32.S

  16. Exit System Call - iret arch/x86/include/asm/irqflags.h arch/x86/kernel/entry_32.S

  17. Fast Enter & Exit System Call • 判別是否支援Fast System Call– sysenter & sysexit • sysenter_setup() • 初始化所需的MSR (model-specific register) • enable_sep_cpu() Assembly Level C Level start_kernel() i386_start_kernel() head_32.S init/main.c arch/x86/kernel/head_32.S arch/x86/kernel/head_32.c check_bugs() arch/x86/kernel/cpu/bugs_64.c identify_boot_cpu() arch/x86/kernel/cpu/common.c sysenter_setup() enable_sep_cpu() arch/x86/vdso/vdso32-setup.c arch/x86/vdso/vdso32-setup.c

  18. sysenter_setup() • 4GB的線性address中的倒數第2個page內存VDSO • 支援Fast System Call • vsyscall-sysenter.so • 只支援int 0x80呼叫System Call • vsyscall-int80.so arch/x86/vdso/vdso32-setup.c

  19. enable_sep_cpu() • 因為sysenter & sysexit不具有call & return的關係 • sysenter不會為sysexit保存任何return資訊→需要MSR • 初始化 • 將kernel mode的code segment存入MSR_IA32_SYSENTER_CS • 將kernel mode的stack pointer存入MSR_IA32_SYSENTER_ESP • 將進入system call的entry address存入MSR_IA32_SYSENTER_EIP • ia32_sysenter_target() arch/x86/vdso/vdso32-setup.c

  20. Enter System Call - sysenter • 如果支援sysenter • 使用system call時就會呼叫__kernel_vsyscall • 將MSR_IA32_SYSENTER_CS的值載入%cs • 將MSR_IA32_SYSENTER_ESP的值載入%esp • 將MSR_IA32_SYSENTER_EIP的值載入%eip • 如果不支援sysenter或編成static的ELF

  21. Enter System Call - sysenter sysenter_return arch/x86/kernel/entry_32.S

  22. Exit System Call - sysexit • %edx取得sysenter_return的位址 • %ecx取得user space的stack pointer • 將%edx內容存到%eip • 將%ecx內容存到%esp arch/x86/kernel/entry_32.S arch/x86/include/asm/irqflags.h

  23. Int 0x80 vs sysenter & sysexit • sysenter & sysexit • 優點 • 不需像int 0x80要進行一致性及安全性檢查 • 較快速 • 缺點 • 需使用MSR 紀錄system call的entry資訊

  24. Parameter passing • 參數傳遞 • 不得超過6個參數 • 使用%eax, %ebx, %ecx, %edx, %esi, %edi傳遞 • 參數長度不可超過register可容納長度 • 32bits • getpid()不用傳參數 • mmap需要傳6個參數

  25. System Call Implementation • 從2.6.28開始system call的實作開始使用一種新的macro • SYSCALL_DEFINEx include/linux/syscalls.h fs/read_write.c include/linux/syscalls.h

  26. System Call Implementation • kernel 2.6.28之前的system call實作方式

  27. How to Use System Call • 用system call table 查出你想要用的system call編號 • 利用syscall()指令呼叫system call • 依照system call類型丟入參數

  28. How to Use System Call • usr/include/asm/unisted32.h

  29. How to Use System Call • linux/include/linux/syscalls.h

  30. How to Use System Call • syscall( system call number, parameters… )

  31. How to Use System Call • syscall(40,資料夾位置)

  32. How to Use System Call

  33. Reference Book • Understanding the Linux Kernel, 3e • 書名 : Linux 核心詳解 第三版 • 出版社: O’Reilly • 作者 : 陳建勳 & 蔣大偉 • Linux 2.6內核標準教程 • 出版社: 人民郵電出版社 • 作者 : 河秦 & 王洪濤

  34. Question Time • Thanks~

More Related