?? ????? (Kernel Programming) - PowerPoint PPT Presentation

About This Presentation
Title:

?? ????? (Kernel Programming)

Description:

(Kernel Programming) Lecture #9 System Call (4) * System Call ( ) System Call ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 45
Provided by: neg59
Category:

less

Transcript and Presenter's Notes

Title: ?? ????? (Kernel Programming)


1
?? ?????(Kernel Programming)
  • Lecture 9

2
? ?
  • 1. Kernel Programming ???
  • (1) Kernel Programming?
  • (2) Kernel vs Application
  • (3) Kernel programming ?? ??
  • (4) Kernel Interface ??
  • 2. System call ????
  • (1) System call???
  • (2) POSIX API? System calls
  • (3) System call ?? ??
  • (4) System call ????
  • (5) System call ??

3
1. ?? ?????
4
Kernel Programming ???
  • Linux kernel core ?? ??
  • Linux kernel ???? ??
  • Linux kernel ?? ?????

5
Kernel vs. Application (1)
  • ?? ??
  • Application Program ???? ????? ??
  • Kernel ??????? ?? system call?? ???? ???? ????
    ?? ?????? ??

6
Kernel vs. Application (2)
  • Library ??
  • Application Program ?? library(C ?? ????? ?? ???
    ????? ?)? link ?? ??? ? ??.
  • Kernel kernel ???? export ?? ???? ??? ? ??.
  • Kernel mode vs User mode
  • Application Program
  • CPU? user mode?? ??
  • ????? ?? ???? ?? ???? ?? ???? ?? ??? ????.
  • Kernel
  • CPU? kernel mode?? ??
  • ?? ??? ??? ????.

7
Kernel vs. Application (3)
  • Address Space
  • Application Program? Kernel Program? ?? ?? ???
    ???? ??? ???, ???? ??? ?? ?? address space? ???
    ??.

8
Kernel vs. Application (4)
  • Namespace pollution
  • Application Program ?? ???? ??????? ? ??? ???
    ??? ???? ?? ??.
  • Kernel Program ?? ???? ?? ??? ?? ????? ??? ???
    ??? ???? ??? ??? ??.

9
Kernel programming ?? ?? (1)
  • Library
  • stdio.h ? ?? ?? ?????? ???? ?? ??? include???
    ???.
  • ?? /usr/include/linux ? /usr/include/asm ??? ???
    ???? ?? include ??.
  • Namespace pollution
  • ?? ??? link?? ?? ?? ??? static?? ?? ?? ?? ???
    link? symbol? symbol table ??
  • EXPORT_NO_SYMBOLS
  • EXPORT_SYMBOL(name)
  • ?? ??? ? ??? prefix? ?? ??.
  • Ex sys_open()

10
Kernel programming ?? ?? (2)
  • Fault handling
  • Kernel? ???? ??? ?? ??? ??? ?? ??? ????? ??? ????
    ???? ??? ?????.
  • ?? ?? ?? ?? ? ?? ?? ??? ???? ???? ??.
  • Address space
  • ??? ???? stack? ??? ???? ??, ???? ???? ??? ???
    ????? ? ??? ?????, recursion? ?? ???? ??? ????
    ??.
  • Application? data? ?? ?? ?? ??? ??(call by
    reference) ? ????? ??.

11
Kernel programming ?? ?? (3)
  • ??
  • ???? ?? MMX ??? ??? ? ??.

12
Kernel Interface ?? (1)
  • ?? ??
  • Kernel program? ???? library? ???? ??? kernel??
    export ?? ??? ?? ??? ? ??.
  • Kernel interface ?? ??
  • Kernel ?? ???? ?? ? kernel programming? ?? ????
    ??? ??? ?? ??? ? ??.
  • Port I/O
  • Interrupt
  • Memory
  • Synchronization
  • Kernel message ??
  • Device Driver registration

13
Kernel Interface ?? (2)
  • I/O device ? data? ?? ?? ?? ??
  • unsigned inb(unsigned port)
  • Port?? 1byte? ???.
  • unsigned inw(unsigned port)
  • Port?? 2byte? ???.
  • unsigned inl(unsigned port)
  • Port?? 4byte? ???.
  • unsigned outb(char value, unsigned port)
  • Port? 1byte value? ??.
  • unsigned outw(short int value, unsigned port)
  • Port? 2byte value? ??.
  • unsigned outl(long int value, unsigned port)
  • Port? 4byte value? ??.

14
Kernel Interface ?? (3)
  • I/O device ? data? ?? ?? ?? ?? (??)
  • void insb(unsigned port, void addr, unsigned
    long count)
  • Port?? count bytes? ??? ???? addr ???? ??
  • void insw(unsigned port, void addr, unsigned
    long count)
  • Port?? 16bit count ?? ??? ???? addr ???? ??
  • void insl(unsigned port, void addr, unsigned
    long count)
  • Port?? 32bit count ?? ??? ???? addr ???? ??

15
Kernel Interface ?? (4)
  • I/O device ? data? ?? ?? ?? ?? (??)
  • void outsb(unsigned port, void addr, unsigned
    long count)
  • Memory? addr?? ???? count bytes? ??? port? ??.
  • void outsw(unsigned port, void addr, unsigned
    long count)
  • Memory? addr?? ???? count 16bit? ??? port? ??.
  • void outsl(unsigned port, void addr, unsigned
    long count)
  • Memory? addr?? ???? count 32bit? ??? port? ??.

16
Kernel Interface ?? (5)
  • I/O device ? data? ?? ?? ?? ?? (??)
  • Pausing I/O
  • ???? ?? ??? device?? ??? ? ?? ??? ??? ? ?? ???
    ??? ??? ? ?? ??? ? ? ??.
  • ?? ??? ??? ?? ?? _p ? ?? ??? ??? ???? ??.
  • ?) inb() ??? ?? inb_p()

17
Kernel Interface ?? (6)
  • ????? ?? ? ??? ?? ??(or ???)
  • cli()/sti()
  • clear/set interrupt enable
  • save_flags(unsigned long flag),
    restore_flags(unsigned long flag)
  • status register? ??? ???? ???? ???
  • ? ???? ?? ?? ??? ?? ??? ??.
  • flag? ?? ??? pass??? ???.
  • int requst_irq(unsigned int irq, void
    (handler)(int), unsigned long flags, const char
    device)
  • ????? IRQ? ????, ? IRQ? ?? interrupt handler? ??
  • void free_irq(unsigned int irq)
  • request_irq()?? ??? irq? ???

18
Kernel Interface ?? (7)
  • Kernel??? ?? ??? ?? ??
  • void kmalloc(unsigned int len, int priority)
  • ?? ??? ??. 128131056byte?? ??
  • priorityGFP_BUFFER, GFP_ATOMIC, GFP_USER,
    GFP_KERNEL
  • ????? ???? ???? ????.
  • void kfree(void obj)
  • kmalloc()?? ?? ?? ?? ???? ??

19
Kernel Interface ?? (8)
  • Kernel??? ?? ??? ?? ?? (??)
  • void vmalloc(unsigned int len)
  • ?? ??? ??
  • ?? ?? ??
  • ?? ?? ???? ???? ??? ??? ??
  • void vmfree(void addr)
  • vmalloc()?? ?? ?? ?? ???? ??

20
Kernel Interface ?? (9)
  • ??? ??? ?? ?? ??? ???? ???? ?? ??
  • unsigned long copy_from_user(void to, const void
    from, unsigned long n)
  • ??? ?????? ???????? n byte?? data ??.
  • unsigned long copy_to_user(void to, const void
    from, unsigned long n)
  • ???????? ??? ?? ??? n byte?? data ??
  • void memset(void s, char c, sizt_t count)
  • ??? s? c? count?? ??(??)
  • put_user(datum, ptr) / get_user(ptr)
  • ??? ??? datum? ????? ???? ?? ???

21
Kernel Interface ?? (10)
  • ??? ??
  • void sleep_on(struct wait_queue q)
  • q? ??? event? sleep??, uninterruptible
  • void sleep_in_interruptible(struct wait_queue
    q)
  • q? ??? event? sleep??, interruptible
  • void wake_up(struct wait_queue q)
  • sleep_on(q)? ?? sleep? task? wakeup
  • void wake_up_interruptible(struct wait_queuq q)
  • sleep_on_interruptible(q)? ?? sleep? task? wakeup

22
Kernel Interface ?? (11)
  • stdout?? ?? ???? ???? ?? ??
  • printk(const char fmt, .)
  • printf? ?? ??
  • printk(LOG_LEVELmessage string)
  • LOG_LEVEL KERN_EMERG, KERN_ALERT, KERN_ERR,
    KERN_WARNING, KER_INFO, KERN_DEBUG
  • ?
  • printk(lt1gtHello, World)
  • printk(KERN_WARNINGwarning \n)

23
Kernel Interface ?? (12)
  • ???? ???? ?? ??
  • int register_xxxdev(unsigned int major, const
    char name,struct file_operations fops)
  • character/block driver? xxxdevmajor? ??
  • xxx blk/chr
  • int unregister_xxxdev(unsigned int major, const
    char name)
  • xxxdevsmajor? ??? ?? device driver? ??
  • int register_netdev(const char name)
  • int unregister_netdev(const char name)
  • MAJOR(kdev_t dev)/MINOR(kdev_t dev)
  • ????dev??? major/minor ??? ??

24
2. System Call
25
System Call (1)
26
System Call (2)
  • user mode process? kernel ?? interface
  • user mode process? ????? kernel ??? ????? ??? ?
    ??
  • ? kernel? ???? ? hardware ? ?? ?? ??
  • user mode process? kernel? ??? ?? ???? ?? ???
    ????? hardware? ???? hardware? ???? ???? kernel
    ?? communication channel? ??.

27
POSIX API System calls (1)
  • POSIX API (Application Programming Interface)
  • ??? ????? ??? ?? ?? ??? ???? ????? ??
  • application? ???? ? ???? ??? ?? ??? ??? ???? ???
    ??? ?
  • ??? ?? ?? ?? ???? ?? ????? porting ?? ?? ?? ?? ??
    ?? ??
  • open(), close(), read(), write() ?
  • System Call
  • ????? ????? ?? ??? ???? ???? ?
  • Linux??? POSIX API? ???? system library ?? ???
    system call ??? ?????? ??? ?? ??? ????

28
POSIX API System calls (2)
29
System Call ?? ?? (1)
  • Linux ??? system call ??
  • Interrupt ?? ???? ??
  • Interrupt
  • ?? ??? ??? ???? ?? ? ??
  • ?? ??? ???? ??? ???? ??? kerenl?? ??? ????

30
System Call ?? ?? (2)
  • System call ??

31
System Call ???? (1)
  • ??
  • ?? kernel?? ???? ?? service? user application? ??
    ? ??? System Call ? ???
  • ?? ??
  • ?? ??
  • System Call ?? ??
  • System Call ?? ??? ??
  • System Call ?? ?? ??
  • Kernel ??? ? target board? ??
  • user application ??
  • ?? ??? System Call? ???? application ??
  • library ??(??? ??? ?? ???) ? root filesystem? ??

32
System Call ???? (2)
  • ? ?
  • Target system? kernel source ????
  • ? HOME/pxa255-pro3/kernel/linux-2.6.21
  • ? ????? ???? ???? kernel? ????.

33
System Call ???? (3)
  • System Call ?? ??
  • Linux ??? ???? ?? ??? ??? ?? ??? ??? ??? ??.
  • kernel/include/asm-arm/unistd.h ? ? ??? ??? ??
    ??? ?? ?? ??.
  • ? ?? ??? system call? ???? ?? ??
  • kernel/include/asm-arm/unistd.h ??? vi? ??.

34
System Call ???? (4)
  • System Call ?? ?? (??)
  • ??? ?? ??? System Call? ????? ??? ? ???? ???.
  • unistd.h ??? ? ??? ?? ?? system call? 347?? ?? ??
    ??? ? ??. ???, ??? system call? ? ??? ?? 348???
    ????.
  • __NR_? System Call ????? ???? ?????, ? ??
    mysyscall? ??? System Call ?? ???
    ????(sys_mysyscall ? ??? ?? ?? ? ??).

35
System Call ???? (5)
  • System Call ???? System Call ?? ?? ??
  • kernel/arch/arm/kernel/entry-common.S ?
    sys_call_table??? entry? ???? ??.
  • sys_call_table?? system call ????? ?? ???? ???? ?
    ???? unistd.h? ?? ???? system call ??? ???? ??
    ????.
  • ? sys_call_table? ??? System Call ???? ??
  • entry-common.S ??? vi? ??.

36
System Call ???? (6)
  • System Call ???? System Call ?? ?? ?? (??)
  • entry-common.S ?? ??? ?? ??? ??? ? ??.
  • ENTRY(sys_call_table) ??? calls.S? include?? ??.
  • vi? ??? ?? calls.S? ?? ??.

37
System Call ???? (7)
  • System Call ???? System Call ?? ?? ?? (??)
  • calls.S ? ??? ??? ??.
  • ??? ?? ??? System Call ????? ??? ? ?? ?? ???.

unistd.h? ??? ??? ??
38
System Call ???? (8)
  • ?? ?? ????
  • System Call ? ???? ? ??? ??? ????.
  • kernel/kernel/mysyscall.c ? ??? ?? ???.

39
System Call ???? (9)
  • Makefile ??
  • ???? ????? ??? ??? ? ?? ?? ??? ? ? ???
    kernel/kernel/Makefile? ??? ?? ?? ??.

40
System Call ???? (10)
  • Kernel ??? ?? ? ?? ??? ??
  • ????? ??? ??? ?? ??? ????? ?? ??? ?? ???? ??????
    ?? ????
  • cd /root/pxa255-pro3/kernel/linux-2.6.21
  • make pro3_defconfig
  • make oldcondig
  • make uImage
  • Kernel ??? ??
  • U-boot bootloader? ?? ?? ??? ?? ???? ?? ???? ????
  • PRO3gt run linuxf

41
System Call ??? (1)
  • System Call ???? ?? user application ??
  • library? ??? ?? user application? ??? ??
  • ?? ???? /pxa255-pro3/test
  • ??? ?? syscall_test.c

42
System Call ??? (2)
  • System Call ???? ?? user application ?? (??)
  • ??? ??? ?????
  • ??? Header ?? ??? ???? ?????? ??

43
System Call ??? (3)
  • ???
  • ???? ??? ????? nfs ????? ??
  • ?? ???? ???? nfs ?????? ???? ??, ??? ??? ????

44
System Call ?? ? ??? ??
  • ????? ??? ???? ??? ??.

?? ??
??? ???? ??
Write a Comment
User Comments (0)
About PowerShow.com