9.1 Kernel Programming ??? - PowerPoint PPT Presentation

About This Presentation
Title:

9.1 Kernel Programming ???

Description:

Title: 1 Author: negi Last modified by: Administrator Created Date: 2/20/2004 5:23:31 AM Document presentation format: – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 63
Provided by: negi
Category:

less

Transcript and Presenter's Notes

Title: 9.1 Kernel Programming ???


1
?9? ???? ???? ??
2
? ?
  • 9.1 Kernel Programming ???
  • 9.1.1 Kernel Programming?
  • 9.1.2 Kernel vs Application
  • 9.1.3 Kernel programming ?? ??
  • 9.1.4 Kernel Interface ??
  • 9.2 System call ????
  • 9.2.1 System call???
  • 9.2.2 POSIX API? System calls
  • 9.2.3 System call ?? ????
  • 9.2.4 System call ?? ??
  • 9.2.5 System call ??
  • 9.3 Device Driver
  • 9.3.1 Device Driver?
  • 9.3.1 Device Driver? ??
  • 9.3.3 Device Driver ??
  • 9.3.4 ??? Driver ?????? ???
  • 9.3.5 Makefile ???

3
9.1 Kernel Programming ???
  • Linux kernel core ?? ??
  • Linux kernel ???? ??
  • Linux kernel ?? ?????

4
9.1.1 Kernel vs Application
  • ?? ??
  • Application Program ???? ????? ??
  • Kernel ??????? ?? system call?? ???? ???? ????
    ?? ??? ??? ??

Application
Kernel
System call
Interrupt
5
9.1.1 Kernel vs Application
  • Library
  • Kernel kernel?? export ?? ??? ??? ? ??.
  • Application Program ?? library? link ?? ??? ?
    ??.
  • Kernel mode vs User mode
  • Kernel Program ?? ?? ????.
  • Application Program user mode?? ???? ????? ??
    ???? ?? ???? ?? ???? ?? ??? ????.

6
9.1.1 Kernel vs Application
  • Address Space
  • Application Program? Kernel Program? ?? ?? ???
    ???? ??? ???, ???? ??? ?? ?? address space? ???
    ??.

1 G byte
4 G byte
3 G byte
7
9.1.1 Kernel vs Application
  • Namespace pollution
  • Application Program ?? ???? ??????? ? ??? ???
    ??? ???? ?? ??.
  • Kernel Program ?? ???? ?? ??? ?? ????? ??? ???
    ??? ???? ??? ??? ??.

8
9.1.2 Kernel programming ?? ??
  • 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()

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

10
9.1.2 Kernel programming ?? ??
  • ??
  • ???? ?? MMX ??? ??? ? ??.

11
9.1.3 Kernel Interface ??
  • ?? ??
  • Kernel program? ???? library? ???? ??? kernel??
    export ?? ??? ?? ??? ? ??.
  • Kernel interface ?? ??
  • Kernel ?? ???? ?? ? kernel programming? ?? ????
    ??? ??? ?? ??? ? ??.
  • Port I/O
  • Interrupt
  • Memory
  • Synchronization
  • Kernel message ??
  • Device Driver register

12
9.1.3 Kernel Interface ??
  • 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? ??.

13
9.1.3 Kernel Interface ??
  • 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 ???? ??
  • 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? ??.

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

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

16
9.1.3 Kernel Interface ??
  • ????? ?? ? ??? ?? ??(or ???)
  • void free_irq(unsigned int irq)
  • request_irq()?? ??? irq? ???

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

18
9.1.3 Kernel Interface ??
  • Kernel?? ?? ???? ??? ? ???? ???
  • void vmalloc(unsigned int len)
  • ?? ??? ??
  • ?? ?? ??
  • ?? ?? ???? ???? ??? ??? ??
  • void vmfree(void addr)
  • vmalloc()?? ?? ?? ?? ???? ??

19
9.1.3 Kernel Interface ??
  • ??? ??? ???? ??? ???? ???? ?? ??
  • 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? ????? ???? ?? ???

20
9.1.3 Kernel Interface ??
  • ???
  • 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

21
9.1.3 Kernel Interface ??
  • Standard out ?? ???? ???? ?? ??
  • printk(const char fmt, .)
  • printf? ?? ??
  • printk(LOG_LEVEL_ message)
  • LOG_LEVELKERN_EMERG, KERN_ALERT,
    KERN_ERR,KERN_WARNING, KER_INFO, KERN_DEBUG
  • ?
  • printk(lt1gtHello, World)
  • printk(KERN_WARNINGwarning \n)

22
9.1.3 Kernel Interface ??
  • Device Driver register
  • int register_xxxdev
  • (unsigned int major, const char name,struct
    file_operations fops)
  • character/block driver? xxxdevmajor? ??
  • xxxblk/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 ??? ??

23
9.2 System Call
24
9.2.1 Whats System Calls?
  • user mode process? kernel ?? interface
  • user mode process? ????? kernel ??? ????? ??? ?
    ??. ? kernel? ???? ? hardware ? ?? ?? ??
  • user mode process? kernel? ??? ?? ???? ?? ???
    ????? hardware? ???? hardware? ???? ???? kernel
    ?? communication channel? ??.

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

26
9.2.2POSIX API System calls
  • ?gt ?????? ? open() ?? ? ? sys_open() ??
  • (POSIX API) (??? ?)

27
9.2.3 System Call ?? ????
  • ??
  • ?? kernel?? ???? ?? service? user application? ??
    ? ??? System Call ? ???.
  • ??
  • ?? ??
  • System Call ?? ??
  • System Call ?? ??? ??
  • System Call ?? ?? ??
  • Kernel ??? ? target board? ??
  • user application ??
  • ?? ??? System Call? ???? application ??
  • library ??(??? ??? ?? ???.)
  • Ramdisk? ????? target board? ??

28
9.2.3 System Call ?? ????
  • ??
  • /PXA270/kernel/linux-2.6.11-h270-tku_v1.1/ ?
    target system? kernel source code? ??.
  • ? ????? ???? ???? kernel? ????.

29
9.2.3 System Call ?? ????
  • System Call ?? ??
  • Linux ??? ???? ?? ??? ??? ?? ??? ??? ??? ??.
  • kernel/include/asm-arm/unistd.h ? ? ??? ??? ??
    ??? ?? ?? ??.
  • ? ?? ??? system call? ???? ?? ??
  • kernel/include/asm-arm/unistd.h ??? vi? ??.

30
9.2.3 System Call ?? ????
  • ??? ?? ??? System Call? ????? ??? ? ???? ???.
  • unistd.h ??? ? ??? ?? ?? system call ? 280 ?? ??
    ?? ??? ? ??. ??? system call ? ? ??? ??281???
    ????.
  • __NR_? System Call ????? ???? ????? ? ??
    hybuscall? ??? System Call ?? ??? ????.
  • (sys_hybuscall ? ??? ?? ?? ? ??.)

31
9.2.3 System Call ?? ????
  • System Call ???? System Call ?? ?? ??
  • kernel/arch/arm/kernel/entry-comm.S ?
    sys_call_table??? entry? ???? ??.
  • sys_call_table?? system call ????? ?? ???? ???? ?
    ???? unistd.h? ?? ???? system call ??? ???? ??
    ????.
  • ? sys_call_table? ??? System Call ???? ??
  • entry-comm.S ??? vi? ??.

32
9.2.3 System Call ?? ????
  • entry-comm.S ?? ??? ?? ??? ??? ? ??.
  • ENTRY(sys_call_table) ??? calls.S? include?? ??.
    vi? ??? ?? calls.S? ?? ??.

33
9.2.3 System Call ?? ????
  • calls.S ? ??? ??? ??.
  • ltkernelgt/arch/arm/kernel

34
9.2.3 System Call ?? ????
  • ??? ?? ??? System Call ????? ??? ? ?? ?? ???.

unistd.h? ??? ??? ??
35
9.2.3 System Call ?? ????
  • ?? ?? ????
  • System Call ? ???? ? ??? ??? ????.
  • kernel/kernel/test_syscall.c ? ??? ?? ???.

calls.S ? ??? ??? ??? ??
? ??? ???? ??? ??? ???? ??? ? ???? keyword
36
9.2.3 System Call ?? ????
  • Makefile ??
  • ???? ????? ??? ??? ? ?? ?? ??? ? ? ???
    kernel/kernel/Makefile? ??? ?? ?? ??.

37
9.2.3 System Call ?? ????
  • Kernel image? /tftpboot? ??
  • kernel ?????? ??? ????? ??? ?? ???? target?
    ???? ?? /tftpboot? ??.
  • cd kernel
  • make menuconfig make zImage
  • cp kernel/arch/arm/boot/zImage /tftpboot

38
9.2.3 System Call ?? ????
  • System Call? ???? user application??
  • library? ??? ?? user application? ??? ??

unistd.h ? ??? ???? System Call ????? ??? ??? ???
????. sys ???? ??? ???.
/ linux/unistd.h / define _syscall0(type,name)
type name(void) \ register long __res
__asm__(r0) \ __asm__ __volatile__ \ __sysc
all(name) \ r (__res) \ \
lr) \ __syscall_return(type,__res) \
else define __syscall(name) swi\t
__sys1(__NR_name)\n\t
Software interrupt? ????? assemblier
Unistd?? ??? symbol? ????.
39
9.2.3 System Call ?? ????
  • Test
  • ?? system call? ???? ?? ???? ??
  • ???(?? /tftpboot? zImage? ??) ? target?
    download?? target ? ???? ??? ? host? /home/share
    ???
  • ????? ???? ????? ??

./hybuscall Welcome to the embedded World!!
40
9.2.3 System Call ?? ????
  • ????? ??? ??? ?? ??? ??.
  • Kernel ?? Application ??

unistd.h ? system call ?? ??
calls.S ? System Call ???? ??
system call? ???? application ??
Application ? Target system? download
System Call ?? ?? ??
Kernel ? ???
Applicatin ??
Kernel? Target system? download
41
9.2.4 System Call ?? ??
  • Linux ??? system call ??
  • Interrupt ?? ???? ??
  • Interrupt
  • ?? ??? ??? ???? ?? ? ??? ?? ??? ???? ??? ???? ???
    kerenl?? ??? ????.

42
9.2.4 System Call ?? ??
  • System Call

43
Device Driver
44
??
9.3.1 Device Driver? 9.3.1 Device Driver?
?? 9.3.3 Device Driver ?? 9.3.4 ??? Driver
?????? ??? 9.3.5 Makefile ??? 9.3.6 Driver? ???
??? ?? ? ????
45
9.3.1 Device Driver?(1)
  • ???? hardware ??? ??? ???? software
  • user application? driver?? ??? ??, driver?
    hardware? ????? ??? ??
  • major number? minor number? ???? ??? devices?
    ???? ??
  • device? system memory ?? data? ??? ????
  • kernel ?? ??

46
9.3.1 Device Driver?(2)
  • Kernel

47
9.3.2 Device Driver? ??
48
9.3.3 Device Driver? ??(1)
  • ???? device driver? ??

Printf? ?? ??? ?? ??? ?? ??
???????? ???? ????? ??? ??? ????
??? header. ????? include???
Module_init()? module_exit() ???
insmod? ??? ???? ? ??? ????. ? ?? ???
regiseter_chrdev() ?? ??? ??? ??? ????.
rmmod? ??? ??? ??? ?? ? ??? ????.
unregister_chrdev() ?? ??? ??? ??? ????.
application?? ?? ??? driver?? ?? ??? mapping
49
9.3.3 Device Driver? ??(2)
  • include ltlinux/module.hgt
  • include ltlinux/fs.hgt
  • include ltlinux/kernel.hgt
  • include ltlinux/init.hgt
  • define DEV_NAME SKELETON"
  • int skeleton_major 0
  • int result
  • ///////////// ? ? ? ? //////////////
  • int skeleton_open (struct inode inode, struct
    file filp)
  • int skeleton_release (struct inode inode, struct
    file filp)
  • ssize_t skeleton_read (struct file filp,
    unsigned int buf, size_t count, loff_t f_pos)
  • ssize_t skeleton_write (struct file filp,
    unsigned int buf, size_t count, loff_t f_pos)
  • int skeleton_ioctl(struct inode inode, struct
    file filp, unsigned int cmd, unsigned int arg)
  • int skeleton_init()
  • void skeleton_exit()

??? ??? ???? ?? register_chrdev()? parameter??
0?? ???? major?? ????? ???? ???? ?????
register_chrdev()??? return?? ?? ????. 0?? ??? ??
?? ??
application?? open()??? driver? ???? ???? ??
application?? close()??? driver? ??? ???? ??
application?? kernel??? ??? ?? ???? ??
50
9.3.3 Device Driver? ??(3)
  • int device_open (struct inode inode, struct file
    filp)
  • printk("test_open start!!!\n")
  • return 0
  • int device_release (struct inode inode, struct
    file filp)
  • return 0
  • ssize_t device_read (struct file filp, unsigned
    int buf, size_t count, loff_t f_pos)
  • printk("read() \n")
  • return 0
  • ssize_t device_write (struct file filp, unsigned
    int buf, size_t count, loff_t f_pos)
  • return 0

Application?? read??? ???? terminal? ?? ??
51
9.3.3 Device Driver? ??(4)
  • int device_ioctl(struct inode inode, struct file
    filp, unsigned int cmd, unsigned int arg)
  • printk("ioctl() \n")
  • return 0
  • struct file_operations test_fops
  • .open device_open,
  • .read device_read,
  • .write device_write,
  • .ioctl device_ioctl,
  • .release device_release,
  • module_init(skeleton_init)
  • module_exit(skeleton_exit)

Ioctl()??? ???? ?? ??
???????? char device driver? ?? ?? ??? ????. ?
??? ??? ??? ??? ??? ???? ??. ? application??
open??? ???? ?? driver? test_open()? mapping????
?? ???? ??.
insmod? ???? ??
rmmod? ???? ??
52
9.3.3 Device Driver? ??(5)
  • int skeleton_init(void)
  • result register_chrdev(test_major,
    DEV_NAME, test_fops)
  • if (result lt 0)
  • printk(KERN_WARNING "s can't get major
    d\n", DEV_NAME, test_major)
  • return result
  • printk("lt1gt init module success!!.. s major
    number d\n", DEV_NAME, result)
  • return 0
  • void skeletin_exit(void)
  • if( !unregister_chrdev(result, DEV_NAME) )
  • printk("s cleanup_module success...\n",
    DEV_NAME)
  • else
  • printk("s cleanup_module fail...\n",
    DEV_NAME)

driver? ??? ??? ???? ??, result? major ?? ???
driver? ???? ???? ??
53
9.4 ??? Driver ?????? ???
  • include ltstdio.hgt
  • include ltfcntl.hgt
  • include ltstring.hgt
  • include lterrno.hgt
  • int fd
  • int main(int argc, char argv)
  • if( (fd open("/dev/SKELETON", O_RDWR)) lt
    3)
  • fprintf(stderr, "/dev/testdd device file open
    error!! .. s\n",strerror(errno))
  • return 0
  • read(fd, 0, 0)
  • ioctl(fd, NULL,0)
  • close(fd)
  • return 0

File? ?? ??? ??
Error? ?? header file
mknod? ??? /dev? ???? node(file)? open??.
54
9.5 Makefile ???
??? ???? ??
CC /opt/iwmmxt-1.0.0/bin/arm-linux-gcc KDIR
/PXA270/kernel/linux-2.6.11-h270-tku_v1.1
TEST_TARGET userapp TEST_OBJS
userapp.o TEST_SRCS userapp.c obj-m
skeleton.o build (TEST_TARGET) make -C
(KDIR) SUBDIRS'pwd' modules (TEST_TARGET)
(TEST_OBJS) (CC) -o _at_ (TEST_OBJS) clean
rm -rf .o .ko rm -f(TEST_TARGET)
??? ????? ??? ?? ??
Compile?? ???? ????? ??? ?? ??
??? ????? make module ??
?????? ???
??? ????? ?? ?? ????? ??
?? ????? ?? ?? ?? ????? ???
???? ??? ??
55
9.6 Driver? ??? ??? ?? ? ????(1)
  • Char Device Driver ?? ??
  • ??? device driver? file interface (node? ??)? ??
    ??
  • Device driver? ??? ???? ?? ??? major number? ??
  • ??? ??? ??
  • ?? int register_chrdev(unsigned int major,
    const char
    name, stuct file_operations fops)
  • Major ??? major number. 0?? ???? ?? ?? ? ????
    ??
  • Name device? ??
  • Fops device? ?? file ?? ???
  • ?? int unregister_chrdev(unsigned int major,
    const char name)

56
9.6 Driver? ??? ??? ?? ? ????(2)
user program open close read write
kernel
device driver
file operations device_open device_close
device_read device_write
system call
device
57
9.6 Driver? ??? ??? ?? ? ????(3)
  • Major number? minor number
  • ??? ???? ???? ?? ???? ?? ??? ??
  • Major number ???? ???? ????? ????? ??
  • Minor number ???? ???? ??? ??? ?? ???
    ???? ?? ??
  • ??? ????? ??? major number? ??? ?
  • register_chrdev()? ??? ??? ? major number? ??
  • ?? major number? ???? ??? ?? ??
  • Major? minor ??? ??? ??? ?? ?? inode? i_rdev?
    16bit? ????. ?? 10bit? major, ?? 12bit? minor??.

58
9.6 Driver? ??? ??? ?? ? ????(4)
  • mknod ???? ???? ????? ??? ? ?? ?? ?? ??
  • mknod device file name type major minor
  • Ex mknod SKELETON c 252 0
  • mdev_t ??? major, minor number? ???? ????
  • MAJOR() kdev_t?? major number? ???? ???
  • Ex MAJOR(inode-gti_rdev)
  • MINOR() kdev_t?? minor number? ???? ???
  • cat /proc/devices ???? ?? ??? ???? ???? ??

C? char device drive??, block device drive? b? ??
59
9.6 Driver? ??? ??? ?? ? ????(5)
  • kernel/include/linux/fs.h

??? ???? driver? ??? ???? ??
60
9.6 Driver? ??? ??? ?? ? ????(6)
61
9.6 Driver? ??? ??? ?? ? ????(7)
  • Device Driver? ?? ??

62
9.6 Driver? ??? ??? ?? ? ????(8)
  • ??? ?? ???? ??? skeleton.c, test application?
    userapp.c, ??? makefile? Makefile? ?? ? ????.
  • make ???? ???? ?? 2 files? ?????.
  • make
  • ??? skeleton.ko ? userapp? target?? ????. ?????
    ??? ??? minicom ?? nfs??? ????.
  • ???? nfs ??? ??? ??? ?????.

insmod skeleton.ko // ?? ???? ?? ????
./userapp // ?? ???? ????? ??? ???? ? ??? ??.
Write a Comment
User Comments (0)
About PowerShow.com