2015年5月9日 星期六

about linux driver resource

在撰寫linux driver時,若需要用到其資料(map(iomap,memmap), interrupt ,memory page, and symbol)

這些都可以用來觀察目系統中,對不同的driver所使用的資源有多少.並且可用來看是否有衝突.

keyword:

udev,procfs,interrupt hook,mapping,memory,symbol

1.註冊device,

無論是用register_chrdev() *1, alloc_chrdev_region(),udev(class_create,class_device and class_device_create)*2

均會在出現在 /proc/devices,用來去建立其device inode

Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  7 vcs
 10 misc
Block devices:
  1 ramdisk
259 blkext
  7 loop
  8 sd
 65 sd
 66 sd

其中若是以udev,則需額外有/sys/class/driver class/driver name


提供其驅動所需的規則檔等..



pi@raspberrypi ~/test/driver/gpio_io $ sudo ls /sys/class/
bcm2708_vcio  dma          input             iscsi_session    mmc_host      rtc           sound       uio     vc-sm
bdi           gpio         iscsi_connection  iscsi_transport  net           scsi_device   spidev      vc      vtconsole
block         graphics     iscsi_endpoint    leds             power_supply  scsi_disk     spi_master  vc-cma
bluetooth     hidraw       iscsi_host        mem              raw           scsi_generic  thermal     vchiq
bsg           i2c-adapter  iscsi_iface       misc             rfkill        scsi_host     tty         vc-mem

2. Interrupt Hook


因在嵌入式系統,會用到許多來自內/外的中斷來進行狀態的轉換及操作.


request_irq(),向kernel 去註冊interrup



CPU0
9437   ARMCTRL   3  BCM2708 Timer Tick
   0   ARMCTRL  16  bcm2708_fb dma
4694   ARMCTRL  24  DMA IRQ
0585   ARMCTRL  25  DMA IRQ
7362   ARMCTRL  32  dwc_otg, dwc_otg_pcd, dwc_otg_hcd:usb1
   0   ARMCTRL  49  20200000.gpio:bank0
   0   ARMCTRL  50  20200000.gpio:bank1
2864   ARMCTRL  65  ARM Mailbox IRQ
   2   ARMCTRL  66  VCHIQ doorbell
   1   ARMCTRL  75
 524   ARMCTRL  83  uart-pl011
4012   ARMCTRL  84  mmc0
       usb_fiq
   0

 


3.mapping


又分為io memory mapping and memory mapping.*3


/proc/ioports and /proc/iomem *4



$sudo less /proc/iomem
 
 
00000000-1bffffff : System RAM
  00008000-0078ebe7 : Kernel code
  007e4000-008f27a7 : Kernel data
20000000-20000fff : bcm2708_vcio
20003000-20003fff : bcm2708_systemtimer
20006000-20006fff : bcm2708_usb
  20006000-20006fff : dwc_otg
20007000-20007fff : bcm2708_dma.0
  20007000-20007fff : bcm2708_dma
20100000-201000ff : bcm2708_powerman.0
20200000-202000b3 : /soc/gpio
20201000-20201fff : dev:f1
  20201000-20201fff : uart-pl011
20300000-203000ff : mmc-bcm2835.0
  20300000-203000ff : mmc0
20980000-2099ffff : bcm2708_usb
  20980000-2099ffff : dwc_otg

4.memory (page)


在kernel中,所使用全域變數等也會在kernel中去檢查.像是cache,及page)*5



slabinfo - version: 2.1
# name            <;active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
isofs_inode_cache      0      0    344   23    2 : tunables    0    0    0 : slabdata      0      0      0
udf_inode_cache        0      0    408   10    1 : tunables    0    0    0 : slabdata      0      0      0
nf_conntrack_c082c640     20     80    248   16    1 : tunables    0    0    0 : slabdata      5      5      0
nf_conntrack_expect      0      0    184   22    1 : tunables    0    0    0 : slabdata      0      0      0
cfq_queue              0      0    152   26    1 : tunables    0    0    0 : slabdata      0      0      0
bsg_cmd                0      0    288   14    1 : tunables    0    0    0 : slabdata      0      0      0
mqueue_inode_cache      8      8    512    8    1 : tunables    0    0    0 : slabdata      1      1      0
nat_entry_set          0      0     24  170    1 : tunables    0    0    0 : slabdata      0      0      0
f2fs_inode_cache       0      0    408   10    1 : tunables    0    0    0 : slabdata      0      0      0
nfs_direct_cache       0      0     96   42    1 : tunables    0    0    0 : slabdata      0      0      0
nfs_commit_data        9      9    448    9    1 : tunables    0    0    0 : slabdata      1      1      0
nfs_read_data         42     42    576   14    2 : tunables    0    0    0 : slabdata      3      3      0
nfs_inode_cache        0      0    616   13    2 : tunables    0    0    0 : slabdata      0      0      0
fat_inode_cache       17     20    392   10    1 : tunables    0    0    0 : slabdata      2      2      0
fat_cache              0      0     24  170    1 : tunables    0    0    0 : slabdata      0      0      0
jbd2_revoke_table_s    256    256     16  256    1 : tunables    0    0    0 : slabdata      1      1      0
ext4_inode_cache   22359  22372    584   14    2 : tunables    0    0    0 : slabdata   1598   1598      0
ext4_allocation_context    156    156    104   39    1 : tunables    0    0    0 : slabdata      4      4      0
ext4_io_end          102    204     40  102    1 : tunables    0    0    0 : slabdata      2      2      0
ext4_extent_status   8064   8064     32  128    1 : tunables    0    0    0 : slabdata     63     63      0
configfs_dir_cache      0      0     56   73    1 : tunables    0    0    0 : slabdata      0      0      0
dquot                 25     50    160   25    1 : tunables    0    0    0 : slabdata      2      2      0
kioctx                 0      0    224   18    1 : tunables    0    0    0 : slabdata      0      0      0
posix_timers_cache     23     23    176   23    1 : tunables    0    0    0 : slabdata      1      1      0
rpc_inode_cache        0      0    352   11    1 : tunables    0    0    0 : slabdata      0      0      0
UNIX                  43     45    544   15    2 : tunables    0    0    0 : slabdata      3      3      0
UDP-Lite               0      0    640   12    2 : tunables    0    0    0 : slabdata      0      0      0
RAW                   13     13    608   13    2 : tunables    0    0    0 : slabdata      1      1      0
UDP                   36     36    640   12    2 : tunables    0    0    0 : slabdata      3      3      0
tw_sock_TCP           25    125    160   25    1 : tunables    0    0    0 : slabdata      5      5      0
TCP                   33     72   1344   12    4 : tunables    0    0    0 : slabdata      6      6      0
cachefiles_object_jar      0      0    192   21    1 : tunables    0    0    0 : slabdata      0      0      0
fscache_cookie_jar     85     85     48   85    1 : tunables    0    0    0 : slabdata      1      1      0
eventpoll_pwq        135    306     40  102    1 : tunables    0    0    0 : slabdata      3      3      0
blkdev_queue          32     32   1016    8    2 : tunables    0    0    0 : slabdata      4      4      0
blkdev_requests       29     85    232   17    1 : tunables    0    0    0 : slabdata      5      5      0
biovec-256            10     10   3072   10    8 : tunables    0    0    0 : slabdata      1      1      0
biovec-128            10     50   1536   10    4 : tunables    0    0    0 : slabdata      5      5      0
biovec-64             10     20    768   10    2 : tunables    0    0    0 : slabdata      2      2      0


Node 0, zone   Normal    506     77      2     27      7      3      2      0      0      0      2

5.symbol


當使用全域變數時,*6



$sudo less /proc/kallsyms
c0008000 T stext
c0008000 T _text
c0008078 t __create_page_tables
c0008124 t __turn_mmu_on_loc
c0008130 t __enable_mmu
c0008160 t __vet_atags
c00081c0 T do_undefinstr
c00081c0 T _stext
c00081c0 T __exception_text_start
c00083bc T do_DataAbort
c0008464 T do_PrefetchAbort
c000850c T __exception_text_end
c0008510 T asm_do_IRQ
c0008510 T __irqentry_text_start
c0008540 T handle_fiq_as_nmi
c00085cc T __irqentry_text_end
c00085d0 t __do_fixup_smp_on_up
c00085e4 T fixup_smp
c00085fc t __lookup_processor_type
c0008634 t __lookup_processor_type_data
c0008640 t __error_lpae
c0008644 t __error
c0008644 t __error_p
c000864c t run_init_process
c0008684 t try_to_run_init_process
c00086d0 T do_one_initcall
c00088b4 t match_dev_by_uuid
c00088f0 t rootfs_mount
c0008970 T name_to_dev_t
c0008d20 t init_linuxrc
c0008de4 T calibrate_delay
c00093a4 t vfp_emulate_instruction.isra.0
c00093f8 t vfp_raise_sigfpe.isra.1
c0009478 t vfp_enable
c00094cc t vfp_raise_exceptions
c0009628 T VFP_bounce
c0009760 T vfp_sync_hwstate
c00097e8 t vfp_notifier
c0009948 T vfp_flush_hwstate
c00099d0 T vfp_preserve_user_clear_hwstate
c0009a6c T vfp_restore_user_hwstate
c0009af0 T vfp_kmode_exception

 


 


*1.should be on linux kernel 2.4.x,not commond for 2.6.x and other


*2.udev




*3.io mapping及memory mapping簡單就是分別為將io視為特殊的位置及存取指令或一則無.


*4.若要使用io/memory mapping時,也需去檢查是否已經有被其它driver申請,否則也會出現衝突.


*5.若由,kmalloc,kfree等為Slab allocator 則為cache為單位.故其實是呼叫kmem_cache_create()'kmem_cache_destory())


而vmalloc(),ioremap()等則是以Buddy System,是以page為單位.





*6:全域變數會在整個驅動之內分享


使用EXPORT_SYMBOL來對外公開