资源描述:
《open系统调用在内核中的流程分析》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、open系统调用在内核中的流程分析分三步走,力求讲得比较清楚。以字符设备为例,相对于块设备要简单些。基于2.6.26的内核UnderstandtheLinuxKernel3rd里面其实都讲到了,所以想再深入的话,可以去参考那本书。一)驱动注册open函数都干了些什么?register_chrdev->cdev_add->kobj_mapfile:fs/char_dev.cintregister_chrdev(unsignedintmajor,constchar*name,conststructfile_operations*fops){structchar_devic
2、e_struct*cd;structcdev*cdev;char*s;interr=-ENOMEM;cd=__register_chrdev_region(major,0,256,name);if(IS_ERR(cd))returnPTR_ERR(cd);cdev=cdev_alloc();if(!cdev)gotoout2;cdev->owner=fops->owner;cdev->ops=fops;//注意,在后面的chrdev_open会从cdev再得到fops...}file:fs/char_dev.cintcdev_add(structcdev*p,dev_
3、tdev,unsignedcount){p->dev=dev;p->count=count;returnkobj_map(cdev_map,dev,count,NULL,exact_match,exact_lock,p);}file:fs/char_dev.cstaticstructkobject*exact_match(dev_tdev,int*part,void*data){structcdev*p=data;return&p->kobj;}file:drivers/base/map.cintkobj_map(structkobj_map*domain,dev_t
4、dev,unsignedlongrange,structmodule*module,kobj_probe_t*probe,int(*lock)(dev_t,void*),void*data){unsignedn=MAJOR(dev+range-1)-MAJOR(dev)+1;unsignedindex=MAJOR(dev);unsignedi;structprobe*p;if(n>255)n=255;p=kmalloc(sizeof(structprobe)*n,GFP_KERNEL);if(p==NULL)return-ENOMEM;for(i=0;iowner=m
5、odule;p->get=probe;//此处其实就是exact_matchp->lock=lock;p->dev=dev;p->range=range;p->data=data;}mutex_lock(domain->lock);for(i=0,p-=n;iprobes[index%255];while(*s&&(*s)->rangenext;p->next=*s;*s=p;}mutex_unlock(domain->lock);return0;}【参考UnderstandingTheLinuxKernel13.5.CharacterDeviceDrivers】Th
6、edevicedrivermodeldefinesakobjectmappingdomainforthecharacterdevices,whichisrepresentedbyadescriptoroftypekobj_mapandisreferencedbythecdev_mapglobalvariable.Thekobj_mapdescriptorincludesahashtableof255entriesindexedbythemajornumberoftheintervals.Thehashtablestoresobjectsoftypeprobe,onef
7、oreachregisteredrangeofmajorandminornumbers,whosefieldsarelistedinTable13-9.Whenthekobj_map()functionisinvoked,thespecifiedintervalofdevicenumbersisaddedtothehashtable.Thedatafieldofthecorrespondingprobeobjectpointstothecdevdescriptorofthedevicedriver.Thevalueofthisfieldispasse