{"id":295379,"date":"2025-01-04T12:09:15","date_gmt":"2025-01-04T04:09:15","guid":{"rendered":"https:\/\/gulass.cn\/tsqdxlnhkjnc.html"},"modified":"2025-01-04T12:09:15","modified_gmt":"2025-01-04T04:09:15","slug":"tsqdxlnhkjnc","status":"publish","type":"post","link":"https:\/\/gulass.cn\/tsqdxlnhkjnc.html","title":{"rendered":"\u63d0\u5347\u9a71\u52a8\u6548\u7387\uff1a\u5185\u6838\u7a7a\u95f4\u5185\u5b58\u6620\u5c04\u5230\u7528\u6237\u7a7a\u95f4\u7684\u4f18\u52bf\u4e0e\u5e94\u7528"},"content":{"rendered":"

\u4e3a\u4f55\u8981\u5c06\u5185\u6838\u7a7a\u95f4\u7684\u663e\u5b58\u6620\u5c04\u5230\u7528\u6237\u7a7a\u95f4<\/p>\n

\u6709\u4e9b\u9a71\u52a8\u5728\u4f7f\u7528\u65f6\u987b\u8981\u9891\u7e41\u7684\u64cd\u4f5c\u5185\u6838\u7a7a\u95f4\u7684\u67d0\u4e00\u7247\u663e\u5b58\uff08\u5982\u663e\u793a\u5c4f\u9a71\u52a8\uff0c\u987b\u8981\u9891\u7e41\u7684\u8bfb\u5199\u5185\u5b58\uff09\uff0c\u82e5\u91c7\u7528\u4f20\u7edf\u7684read\u548cwrite\u4f1a\u5b58\u5728\u5927\u91cf\u7684\u663e\u5b58\u62f7\u8d1d\uff08\u7531\u4e8e\u7528\u6237\u7a7a\u95f4\u96be\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5185\u6838\u7a7a\u95f4\u7684\u5730\u5740\uff09\uff0c\u8fd9\u5c06\u51cf\u5c11\u7a0b\u5e8f\u6548\u7387\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u5185\u6838\u7a7a\u95f4\u865a\u62df\u5730\u5740\u6240\u5bf9\u5e94\u7684\u6570\u5b66\u663e\u5b58\u6620\u5c04\u5230\u7528\u6237\u7a7a\u95f4\uff0c\u501f\u6b64\u964d\u4f4e\u663e\u5b58\u62f7\u8d1d\u3002<\/p>\n

\u663e\u5b58\u6620\u5c04\u65f6\u7684\u5e94\u7528\u5c42\u64cd\u4f5c<\/p>\n

\u5728\u5e94\u7528\u5c42\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570void*mmap<\/a>(void*addr,size_tlength,intprot,intflags,intfd,off_toffset)\u5c06\u6587\u4ef6\uff08\u5305\u62ec\u8bbe\u5907\u6587\u4ef6\uff09\u6620\u5c04\u5230\u5e94\u7528\u5c42\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4linux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891<\/strong>\uff0c\u6b64\u65f6\u5e94\u7528\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u540e\u7684\u865a\u62df\u5730\u5740\u8bbf\u95ee\u6587\u4ef6\uff0c\u5728\u4f7f\u7528\u5b8c\u6210\u540e\u5e94\u7528\u5c42\u8fd8\u5e94\u8c03\u7528intmunmap(void*addr,size_tlength)\u6765\u53d6\u6d88mmap\u7684\u6620\u5c04\u3002<\/p>\n

\u663e\u5b58\u6620\u5c04\u65f6\u7684\u9a71\u52a8\u5c42\u5b9e\u73b0<\/p>\n

\u5f53\u5e94\u7528\u5c42\u8c03\u7528mmap\u65f6\u4f1a\u8c03\u7528\u5230\u9a71\u52a8\u5c42\u7684int(*mmap)(structfile*filp,structvm_area_struct*vma)\u51fd\u6570adobe air linux\uff0c\u6b64\u51fd\u6570\u5c06\u6309\u7167\u7528\u6237\u4f20\u9012\u7684\u53c2\u6570\u8c03\u7528intremap_pfn_range(structvm_area_struct*vma,unsignedlongaddr,unsignedlongpfn,unsignedlongsize,pgprot_tprot)\u5b8c\u6210\u663e\u5b58\u6620\u5c04\uff0c\u4e0b\u8fb9\u5bf9\u7740\u4e24\u4e2a\u51fd\u6570\u8fdb\u884c\u8be6\u5c3d\u4ecb\u7ecd\uff1a<\/p>\n

\n

\/** \u5bf9\u5e94\u5230\u5e94\u7528\u5c42\u7684mmap\u51fd\u6570\n * filp \u6587\u4ef6\u53e5\u67c4\n * vma \u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u72ec\u7acb\u7684\u865a\u62df\u5185\u5b58\u533a\u57df\n *\/<\/span>\nint<\/span> (<\/span>*<\/span>mmap)<\/span> (<\/span>struct<\/span> file<\/span> *<\/span>filp,<\/span> struct<\/span> vm_area_struct<\/span> *<\/span>vma)<\/span>\n\/** \u5c06\u7269\u7406\u5730\u5740\u6620\u5c04\u5230\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u5bf9\u4e8e\u7269\u7406\u4e0a\u4e0d\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u4ee5\u5faa\u73af\u8c03\u7528\u6b64\u51fd\u6570\u5c06\u8fd9\u4e9b\u4e0d\u8fde\u7eed\u7684\u7269\u7406\u5185\u5b58\u6620\u5c04\u5230\u8fde\u7eed\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\n * vma \u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u72ec\u7acb\u7684\u865a\u62df\u5185\u5b58\u533a\u57df\n * addr \u8d77\u59cb\u865a\u62df\u5730\u5740\n * pfn \u7269\u7406\u5185\u5b58\u9875\u6846\u53f7\n * size \u6620\u5c04\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u6700\u5c0f\u4e3a\u4e00\u9875\u5185\u5b58\n * prot \u8bbf\u95ee\u6743\u9650\uff0c\u5982\u679c\u4e0d\u60f3\u88abcache\u53ef\u4ee5\u91c7\u7528prot = pgprot_noncached(prot)\u65b9\u5f0f\u6dfb\u52a0nocache\u6807\u5fd7\n *\/<\/span>\nint<\/span> remap_pfn_range<\/span>(<\/span>struct<\/span> vm_area_struct<\/span> *<\/span>vma,<\/span> unsigned<\/span> long<\/span> addr,<\/span> unsigned<\/span> long<\/span> pfn,<\/span> unsigned<\/span> long<\/span> size,<\/span> pgprot_t<\/span> prot)<\/span>\n<\/code><\/pre>\n<\/p>\n

\u600e\u4e48\u83b7\u53d6\u6570\u5b66\u663e\u5b58\u9875\u6846\u53f7<\/p>\n

\u91c7\u7528__get_free_pages\u3001__get_free_page\u3001kmalloc\u5206\u914d\u7684\u7528virt_to_phys\u53ef\u5c06\u865a\u62df\u5730\u5740\u8f6c\u6362\u4e3a\u5316\u5b66\u5730\u5740\uff0c\u4e4b\u540e\u5c06\u5316\u5b66\u5730\u5740\u5de6\u79fbPAGE_SHIFT\u4f4d\u5373\u53ef\u7684\u5230\u5316\u5b66\u5730\u5740\u9875\u6846\u53f7<\/p>\n

\u91c7\u7528vmalloc\u5206\u914d\u7684\u7528vmalloc_to_pfn\u53ef\u5c06\u865a\u62df\u5730\u5740\u8f6c\u6362\u4e3a\u5316\u5b66\u5730\u5740\u9875\u6846\u53f7<\/p>\n

\u9a71\u52a8\u4ee3\u7801\u5b9e\u73b0<\/p>\n

\u9a71\u52a8\u7a0b\u5e8f\u4ee5\u4e00\u4e2a\u5171\u4eab\u663e\u5b58\u5b9e\u73b0\uff0c\u5b83\u5728\u5185\u6838\u7a7a\u95f4\u5206\u914d\u4e00\u7247\u663e\u5b58\uff0c\u5e94\u7528\u5c42\u53ef\u4ee5\u901a\u8fc7read\u8bfb\u53d6\u663e\u5b58\u4e2d\u7684\u5185\u5bb9\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7mmap\u5c06\u663e\u5b58\u6620\u5c04\u5230\u5e94\u7528\u5c42\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u5982\u4e0b\u662f\u76f8\u5e94\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u987b\u8981\u6ce8\u610f\u7684\u662f\u5047\u5982\u5185\u6838\u7a7a\u95f4\u7684\u663e\u5b58\u5728\u6570\u5b66\u5730\u5740\u4e0a\u4e0d\u8fde\u7eed\uff08\u5982\u91c7\u7528vmalloc\u5206\u914d\uff09\uff0c\u800c\u671f\u671b\u6620\u5c04\u5230\u5e94\u7528\u5c42\u865a\u62df\u5730\u5740\u7a7a\u95f4\u662f\u4e00\u7247\u8fde\u7eed\u7684\u5730\u5740\u65f6\u53ef\u4ee5\u901a\u8fc7\u591a\u6b21remap_pfn_range\u5c06\u6570\u5b66\u4e0a\u4e0d\u8fde\u7eed\u7684\u663e\u5b58\u7a7a\u95f4\u6620\u5c04\u5230\u8fde\u7eed\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4<\/p>\n

\n

#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n\/\/#define USING_VMALLOC<\/span>\n#<\/span>define<\/span> GMEM_ORDER<\/span>\t\t3<\/span><\/span><\/span>\n#<\/span>define<\/span> GMEM_PAGES<\/span>\t\t(<\/span>2<\/span>^<\/span>(<\/span>GMEM_ORDER)<\/span>)<\/span><\/span><\/span>\n#<\/span>define<\/span> GMEM_SIZE<\/span>\t\t(<\/span>GMEM_PAGES*<\/span>PAGE_SIZE)<\/span><\/span><\/span>\n\/\/\u6b21\u8bbe\u5907\u53f7\uff0c\u4e3aMISC_DYNAMIC_MINOR\u8868\u793a\u81ea\u52a8\u5206\u914d<\/span>\n#<\/span>define<\/span> GMEM_MINOR<\/span>\t\tMISC_DYNAMIC_MINOR<\/span><\/span>\n\/\/\u8bbe\u5907\u6587\u4ef6\u540d<\/span>\n

linuxvideo\u9a71\u52a8_linux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891_\u89c6\u9891\u9a71\u52a8\u5b89\u88c5<\/p>\n#<\/span>define<\/span> GMEM_NAME<\/span>\t\t\"gmem\"<\/span><\/span>\n\/\/\u5168\u5c40\u5185\u5b58\u5730\u5740<\/span>\nstatic<\/span> uint8_t<\/span> *<\/span>gmem_buffer;<\/span>\n\/\/\u6253\u5f00\u8bbe\u5907<\/span>\nstatic<\/span> int<\/span> gmem_open<\/span>(<\/span>struct<\/span> inode<\/span> *<\/span>inode,<\/span> struct<\/span> file<\/span> *<\/span>file)<\/span>\n{<\/span>\n\treturn<\/span> 0<\/span>;<\/span>\n}<\/span>\n\/\/\u91ca\u653e\u8bbe\u5907<\/span>\nint<\/span> gmem_release<\/span>(<\/span>struct<\/span> inode<\/span> *<\/span>inode,<\/span> struct<\/span> file<\/span> *<\/span>file)<\/span>\n{<\/span>\n\treturn<\/span> 0<\/span>;<\/span>\n}<\/span>\n\/\/\u8bfb\u6570\u636e<\/span>\nssize_t<\/span> gmem_read<\/span>(<\/span>struct<\/span> file<\/span> *<\/span>file,<\/span> char<\/span> __user *<\/span>buffer,<\/span> size_t<\/span> size,<\/span> loff_t<\/span> *<\/span>pos)<\/span>\n{<\/span>\n\tint<\/span> ret;<\/span>\n\tsize_t<\/span> length =<\/span> (<\/span>size ><\/span> GMEM_SIZE)<\/span> ?<\/span> GMEM_SIZE :<\/span> size;<\/span>\n\t\/\/\u62f7\u8d1d\u6570\u636e\u5230\u5e94\u7528\u5c42<\/span>\n\tret =<\/span> copy_to_user<\/span>(<\/span>buffer,<\/span> gmem_buffer,<\/span> length)<\/span>;<\/span>\n\treturn<\/span> length -<\/span> ret;<\/span>\n}<\/span>\nstatic<\/span> int<\/span> gmem_mmap<\/span>(<\/span>struct<\/span> file<\/span> *<\/span>file,<\/span> struct<\/span> vm_area_struct<\/span> *<\/span>vma)<\/span>\n{<\/span>\n\tint<\/span> result =<\/span> 0<\/span>;<\/span>\n#<\/span>ifdef<\/span> USING_VMALLOC<\/span><\/span>\n\t\/* \u91c7\u7528vmalloc\u5206\u914d\u7684\u5185\u5b58\u5728\u7269\u7406\u4e0a\u4e0d\u8fde\u7eed\uff0c\u6240\u4ee5\u8fdb\u884c\u6620\u5c04\u65f6\u6700\u597d\u4e00\u9875\u4e00\u9875\u7684\u8fdb\u884c\u6620\u5c04 *\/<\/span>\n\t\/\/\u83b7\u53d6\u5e94\u7528\u5c42\u4f20\u9012\u7684\u504f\u79fb<\/span>\n\tunsigned<\/span> long<\/span> offset =<\/span> vma-><\/span>vm_pgoff <<<\/span> PAGE_SHIFT;<\/span>\n\t\/\/\u8ba1\u7b97\u504f\u79fb\u540e\u7684\u5185\u6838\u7a7a\u95f4\u865a\u62df\u5730\u5740<\/span>\n\tunsigned<\/span> long<\/span> map_offset =<\/span> (<\/span>unsigned<\/span> long<\/span>)<\/span>gmem_buffer +<\/span> offset;<\/span>\n\t\/\/\u6839\u636e\u5185\u6838\u7a7a\u95f4\u865a\u62df\u5730\u5740\u8ba1\u7b97\u7269\u7406\u5730\u5740\u9875\u6846\u53f7<\/span>\n\tunsigned<\/span> long<\/span> pfn_start =<\/span> (<\/span>unsigned<\/span> long<\/span>)<\/span>vmalloc_to_pfn<\/span>(<\/span>(<\/span>void<\/span>*<\/span>)<\/span>map_offset)<\/span>;<\/span>\n\t\/\/\u8ba1\u7b97\u671f\u671b\u6620\u5c04\u7684\u5927\u5c0f<\/span>\n\tunsigned<\/span> long<\/span> size =<\/span> vma-><\/span>vm_end -<\/span> vma-><\/span>vm_start;<\/span>\n\t\/\/\u7528\u6237\u7a7a\u95f4\u865a\u62df\u5730\u5740\u8d77\u59cb\u503c\uff0c\u53ef\u80fd\u662f\u7528\u6237\u6307\u5b9a\uff0c\u53ef\u80fd\u662f\u7cfb\u7edf\u5206\u914d<\/span>\n\tunsigned<\/span> long<\/span> vmstart =<\/span> vma-><\/span>vm_start;<\/span>\n

linuxvideo\u9a71\u52a8_\u89c6\u9891\u9a71\u52a8\u5b89\u88c5_linux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891<\/p>\n\t\/\/\u6620\u5c04\u5927\u5c0f\u5fc5\u987b\u662fPAGE_SIZE\u7684\u6574\u500d\u6570<\/span>\n\tif<\/span>(<\/span>(<\/span>size %<\/span> PAGE_SIZE)<\/span> !=<\/span> 0<\/span>)<\/span>\n\t\tsize =<\/span> (<\/span>size +<\/span> PAGE_SIZE)<\/span> \/<\/span> PAGE_SIZE;<\/span>\n\t\/\/\u6700\u5927\u4e0d\u8d85\u8fc7\u5168\u5c40\u5185\u5b58\u5927\u5c0f<\/span>\n\tif<\/span>(<\/span>size ><\/span> GMEM_SIZE)<\/span>\n\t\tsize =<\/span> GMEM_SIZE;<\/span>\n\t\/\/\u589e\u52a0no cache\u5c5e\u6027<\/span>\n\tvma-><\/span>vm_page_prot =<\/span> pgprot_noncached<\/span>(<\/span>vma-><\/span>vm_page_prot)<\/span>;<\/span>\n\tprintk<\/span>(<\/span>\"phy: 0x%lx, offset: 0x%lx, size: 0x%lxrn\"<\/span>,<\/span> pfn_start <<<\/span> PAGE_SHIFT,<\/span> offset,<\/span> size)<\/span>;<\/span>\n\t\/* \u5bf9 vmalloc \u5206\u914d\u7684\u5185\u5b58\u4e00\u9875\u4e00\u9875\u7684\u8fdb\u884c\u6620\u5c04 *\/<\/span>\n\twhile<\/span> (<\/span>1<\/span>)<\/span> \n\t{<\/span>\n\t\t\/\/\u8fdb\u884c\u5185\u5b58\u6620\u5c04\uff0c\u6bcf\u6b21\u6620\u5c04\u4e00\u9875\uff0c\u56e0\u4e3avmalloc\u7533\u8bf7\u7684\u5185\u5b58\u53ef\u80fd\u4e0d\u8fde\u7eed<\/span>\n\t\tresult =<\/span> remap_pfn_range<\/span>(<\/span>vma,<\/span> vmstart,<\/span> pfn_start,<\/span> PAGE_SIZE,<\/span> vma-><\/span>vm_page_prot)<\/span>;<\/span>\n\t\tif<\/span>(<\/span>result !=<\/span> 0<\/span>)<\/span>\n\t\t{<\/span>\n\t\t\tprintk<\/span>(<\/span>\"remap_pfn_range failed at rn\"<\/span>)<\/span>;<\/span>\n\t\t\tbreak<\/span>;<\/span>\n\t\t}<\/span>\n\t\t\/\/\u5b8c\u6210\u5bf9\u6240\u6709\u5185\u5b58\u9875\u7684\u6620\u5c04<\/span>\n\t\tif<\/span>(<\/span>size <=<\/span> PAGE_SIZE)<\/span>\n\t\t\tbreak<\/span>;<\/span>\n\t\t\/\/\u5269\u4f59\u5927\u5c0f\u9012\u51cf<\/span>\n\t\tsize -=<\/span> PAGE_SIZE;<\/span>\n\t\t\/\/\u7528\u6237\u7a7a\u95f4\u865a\u62df\u5730\u5740\u9012\u52a0<\/span>\n\t\tvmstart +=<\/span> PAGE_SIZE;<\/span>\n\t\t\/\/\u5185\u6838\u7a7a\u95f4\u865a\u62df\u5730\u5740\u9012\u52a0<\/span>\n\t\tmap_offset +=<\/span> PAGE_SIZE;<\/span>\n\t\t\/\/\u6839\u636e\u5185\u5b58\u5730\u5740\u8ba1\u7b97\u7269\u7406\u5730\u5740\u9875\u6846\u53f7<\/span>\n\t\tpfn_start =<\/span> vmalloc_to_pfn<\/span>(<\/span>(<\/span>void<\/span> *<\/span>)<\/span>map_offset)<\/span>;<\/span>\n\t}<\/span>\n\treturn<\/span> result;<\/span>\n#<\/span>else<\/span><\/span>\n\t\/\/\u83b7\u53d6\u5e94\u7528\u5c42\u4f20\u9012\u7684\u504f\u79fb<\/span>\n\tunsigned<\/span> long<\/span> offset =<\/span> vma-><\/span>vm_pgoff <<<\/span> PAGE_SHIFT;<\/span>\n\t\/\/\u8ba1\u7b97\u504f\u79fb\u540e\u7684\u5185\u6838\u7a7a\u95f4\u865a\u62df\u5730\u5740<\/span>\n\tunsigned<\/span> long<\/span> map_offset =<\/span> (<\/span>unsigned<\/span> long<\/span>)<\/span>gmem_buffer +<\/span> offset;<\/span>\n\t\/\/\u6839\u636e\u5185\u6838\u7a7a\u95f4\u865a\u62df\u5730\u5740\u8ba1\u7b97\u7269\u7406\u5730\u5740\u9875\u6846\u53f7<\/span>\n\tunsigned<\/span> long<\/span> pfn_start =<\/span> virt_to_phys<\/span>(<\/span>(<\/span>void<\/span>*<\/span>)<\/span>map_offset)<\/span> >><\/span> PAGE_SHIFT;<\/span>\n\t\/\/\u8ba1\u7b97\u671f\u671b\u6620\u5c04\u7684\u5927\u5c0f<\/span>\n\tunsigned<\/span> long<\/span> size =<\/span> vma-><\/span>vm_end -<\/span> vma-><\/span>vm_start;<\/span>\n\t\/\/\u7528\u6237\u7a7a\u95f4\u865a\u62df\u5730\u5740\u8d77\u59cb\u503c\uff0c\u53ef\u80fd\u662f\u7528\u6237\u6307\u5b9a\uff0c\u53ef\u80fd\u662f\u7cfb\u7edf\u5206\u914d<\/span>\n\tunsigned<\/span> long<\/span> vmstart =<\/span> vma-><\/span>vm_start;<\/span>\n\t\/\/\u6620\u5c04\u5927\u5c0f\u5fc5\u987b\u662fPAGE_SIZE\u7684\u6574\u500d\u6570<\/span>\n\tif<\/span>(<\/span>(<\/span>size %<\/span> PAGE_SIZE)<\/span> !=<\/span> 0<\/span>)<\/span>\n\t\tsize =<\/span> (<\/span>size +<\/span> PAGE_SIZE)<\/span> \/<\/span> PAGE_SIZE *<\/span> PAGE_SIZE;<\/span>;<\/span>\n\t\/\/\u6700\u5927\u4e0d\u8d85\u8fc7\u5168\u5c40\u5185\u5b58\u5927\u5c0f<\/span>\n\tif<\/span>(<\/span>size ><\/span> GMEM_SIZE)<\/span>\n\t\tsize =<\/span> GMEM_SIZE;<\/span>\n\t\/\/\u589e\u52a0no cache\u5c5e\u6027<\/span>\n\tvma-><\/span>vm_page_prot =<\/span> pgprot_noncached<\/span>(<\/span>vma-><\/span>vm_page_prot)<\/span>;<\/span>\n\t\/\/\u8fdb\u884c\u5185\u5b58\u6620\u5c04<\/span>\n\tprintk<\/span>(<\/span>\"phy: 0x%lx, offset: 0x%lx, size: 0x%lxrn\"<\/span>,<\/span> pfn_start <<<\/span> PAGE_SHIFT,<\/span> offset,<\/span> size)<\/span>;<\/span>\n\tresult =<\/span> remap_pfn_range<\/span>(<\/span>vma,<\/span> vmstart,<\/span> pfn_start ,<\/span> size,<\/span> vma-><\/span>vm_page_prot)<\/span>;<\/span>\n\t\n\treturn<\/span> result;<\/span>\n#<\/span>endif<\/span><\/span>\n}<\/span>\nstruct<\/span> file_operations<\/span> fops =<\/span> {<\/span>\n\t.<\/span>open =<\/span> gmem_open,<\/span>\n\t.<\/span>release =<\/span> gmem_release,<\/span>\n\t.<\/span>read =<\/span> gmem_read,<\/span>\n\t.<\/span>mmap =<\/span> gmem_mmap,<\/span>\n}<\/span>;<\/span>\nstruct<\/span> miscdevice<\/span> gmem_misc =<\/span> {<\/span>\n\t.<\/span>minor =<\/span> GMEM_MINOR,<\/span>\n\t.<\/span>name =<\/span> GMEM_NAME,<\/span>\n\t.<\/span>fops =<\/span> &<\/span>fops,<\/span>\n}<\/span>;<\/span>\nstatic<\/span> int<\/span> __init gmem_init<\/span>(<\/span>void<\/span>)<\/span>\n{<\/span>\n\tint<\/span> err =<\/span> 0<\/span>;<\/span>\n\tprintk<\/span>(<\/span>\"global memory initrn\"<\/span>)<\/span>;<\/span>\n#<\/span>ifdef<\/span> USING_VMALLOC<\/span><\/span>\n\t\/\/\u91c7\u7528vmalloc\u5206\u914d\u5185\u5b58<\/span>\n\tgmem_buffer =<\/span> vmalloc<\/span>(<\/span>GMEM_SIZE)<\/span>;<\/span>\n#<\/span>else<\/span><\/span>\n\t\/\/\u91c7\u7528__get_free_pages\u5206\u914d\u5185\u5b58<\/span>\n\tgmem_buffer =<\/span> (<\/span>uint8_t<\/span> *<\/span>)<\/span>__get_free_pages<\/span>(<\/span>GFP_KERNEL,<\/span> GMEM_ORDER)<\/span>;<\/span>\n#<\/span>endif<\/span><\/span>\n\tif<\/span>(<\/span>gmem_buffer ==<\/span> NULL<\/span>)<\/span>\n\t{<\/span>\n

linuxvideo\u9a71\u52a8_linux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891_\u89c6\u9891\u9a71\u52a8\u5b89\u88c5<\/p>\n\t\tprintk<\/span>(<\/span>\"allocate mem failedrn\"<\/span>)<\/span>;<\/span>\n\t\treturn<\/span> -<\/span>ENOMEM;<\/span>\n\t}<\/span>\n\tprintk<\/span>(<\/span>\"gmem_buffer = 0x%lxrn\"<\/span>,<\/span> (<\/span>unsigned<\/span> long<\/span>)<\/span>gmem_buffer)<\/span>;<\/span>\n\t\/\/\u6ce8\u518cmisc\u8bbe\u5907<\/span>\n\terr =<\/span> misc_register<\/span>(<\/span>&<\/span>gmem_misc)<\/span>;<\/span>\n\tif<\/span>(<\/span>err !=<\/span> 0<\/span>)<\/span>\n\t{<\/span>\n\t\tprintk<\/span>(<\/span>\"register misc failedrn\"<\/span>)<\/span>;<\/span>\n\t\treturn<\/span> err;<\/span>\n\t}<\/span>\n\treturn<\/span> 0<\/span>;<\/span>\n}<\/span>\nstatic<\/span> void<\/span> __exit gmem_exit<\/span>(<\/span>void<\/span>)<\/span>\n{<\/span>\n\tprintk<\/span>(<\/span>\"global memory exitrn\"<\/span>)<\/span>;<\/span>\n#<\/span>ifdef<\/span> USING_VMALLOC<\/span><\/span>\n\t\/\/\u91c7\u7528vmalloc\u5206\u914d\u7684\u5185\u5b58\u5fc5\u987b\u7528vfree\u91ca\u653e<\/span>\n\tvfree<\/span>(<\/span>gmem_buffer)<\/span>;<\/span>\n#<\/span>else<\/span><\/span>\n\t\/\/\u91c7\u7528__get_free_pages\u5206\u914d\u7684\u5185\u5b58\u5fc5\u987b\u7528free_pages\u91ca\u653e<\/span>\n\tfree_pages<\/span>(<\/span>(<\/span>unsigned<\/span> long<\/span>)<\/span>gmem_buffer,<\/span> GMEM_ORDER)<\/span>;<\/span>\n#<\/span>endif<\/span><\/span>\n\t\/\/\u6ce8\u9500misc\u8bbe\u5907<\/span>\n\tmisc_deregister<\/span>(<\/span>&<\/span>gmem_misc)<\/span>;<\/span>\n}<\/span>\nmodule_init<\/span>(<\/span>gmem_init)<\/span>;<\/span>\nmodule_exit<\/span>(<\/span>gmem_exit)<\/span>;<\/span>\nMODULE_LICENSE<\/span>(<\/span>\"GPL\"<\/span>)<\/span>;<\/span>\nMODULE_AUTHOR<\/span>(<\/span>\"lf\"<\/span>)<\/span>;<\/span>\nMODULE_DESCRIPTION<\/span>(<\/span>\"mmap test\"<\/span>)<\/span>;<\/span>\nMODULE_ALIAS<\/span>(<\/span>\"gmem\"<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

\u9a71\u52a8\u6d4b\u8bd5\u7a0b\u5e8f\u5b9e\u73b0<\/p>\n

\u9a71\u52a8\u6d4b\u8bd5\u7a0b\u5e8f\u4e3b\u8981\u9a8c\u8bc1mmap\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff0c\u5b83\u9996\u5148\u901a\u8fc7mmap\u5c06\u5185\u6838\u7a7a\u95f4\u7684\u663e\u5b58\u6620\u5c04\u5230\u5e94\u7528\u5c42\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u4e4b\u540e\u901a\u8fc7\u8bfb\u5199\u663e\u5b58\u7684\u5f62\u5f0f\u5411\u5176\u4e2d\u5199\u5165\u6570\u636e\uff0c\u6700\u597d\u5728\u901a\u8fc7read\u51fd\u6570\u5c06\u5199\u5165\u7684\u6570\u636e\u8bfb\u53d6\u4e0b\u6765<\/p>\n

\n

#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\nint<\/span> main<\/span>(<\/span>int<\/span> argc,<\/span> char<\/span> *<\/span> argv[<\/span>]<\/span>)<\/span>\n{<\/span>\n\tint<\/span> fd;<\/span>\n\tchar<\/span> *<\/span>start;<\/span>\n\tint<\/span> i;<\/span>\n\tchar<\/span> buf[<\/span>32<\/span>]<\/span>;<\/span>\n\t\/\/\u6253\u5f00\u8bbe\u5907<\/span>\n\tfd =<\/span> open<\/span>(<\/span>\"\/dev\/gmem\"<\/span>,<\/span> O_RDWR)<\/span>;<\/span>\n\tif<\/span> (<\/span>fd ==<\/span> -<\/span>1<\/span>)<\/span>\n\t\tgoto<\/span> fail;<\/span>\n\t\/\/\u6620\u5c04\u5185\u6838\u7a7a\u95f4\u865a\u62df\u5730\u5740\u5230\u7528\u6237\u7a7a\u95f4\uff0c\u865a\u62df\u5730\u5740\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\uff0c\u6620\u5c04\u5927\u5c0f32B\uff08\u5b9e\u9645\u6620\u5c04\u4e86\u4e00\u9875\uff09\uff0c\u652f\u6301\u8bfb\u5199\uff0c\u5171\u4eab\uff0c\u4fbf\u5b9c\u4e3a0<\/span>\n\tstart =<\/span> mmap<\/span>(<\/span>NULL<\/span>,<\/span> 32<\/span>,<\/span> PROT_READ|<\/span>PROT_WRITE,<\/span> MAP_SHARED,<\/span> fd,<\/span> sysconf<\/span>(<\/span>_SC_PAGESIZE)<\/span>*<\/span>0<\/span>)<\/span>;<\/span>\n\tif<\/span> (<\/span>start ==<\/span> MAP_FAILED)<\/span>\n\t\tgoto<\/span> fail;<\/span>\n\t\/\/\u91c7\u7528\u5185\u5b58\u62f7\u8d1d\u65b9\u5f0f\u5199\u5165\u201cabcdefghijklmnopqrstuvwxyz\u201d<\/span>\n\tfor<\/span> (<\/span>i =<\/span> 0<\/span>;<\/span> i <<\/span> 26<\/span>;<\/span> i++<\/span>)<\/span>\n\t\t*<\/span>(<\/span>start +<\/span> i)<\/span> =<\/span> 'a'<\/span> +<\/span> i;<\/span>\n\t*<\/span>(<\/span>start +<\/span> i)<\/span> =<\/span> ''<\/span>;<\/span>\n\t\/\/\u901a\u8fc7read\u51fd\u6570\u8bfb\u53d6\u5185\u6838\u7a7a\u95f4\u5171\u4eab\u5185\u5b58\u7684\u6570\u636e<\/span>\n\tif<\/span> (<\/span>read<\/span>(<\/span>fd,<\/span> buf,<\/span> 27<\/span>)<\/span> ==<\/span> -<\/span>1<\/span>)<\/span>\n\t\tgoto<\/span> fail;<\/span>\n\t\/\/\u8f93\u51fa\u8bfb\u53d6\u7684\u6570\u636e\uff0c\u8fd9\u91cc\u5e94\u8be5\u662f\"abcdefghijklmnopqrstuvwxyz\"<\/span>\n\tputs<\/span>(<\/span>buf)<\/span>;<\/span>\n\t\/\/\u53d6\u6d88mmap\u7684\u6620\u5c04<\/span>\n\tmunmap<\/span>(<\/span>start,<\/span> 32<\/span>)<\/span>;<\/span>\n\t\/\/\u5173\u95ed\u8bbe\u5907<\/span>\n\tclose<\/span>(<\/span>fd)<\/span>;<\/span>\n\treturn<\/span> 0<\/span>;<\/span>\nfail:<\/span>\n\tperror<\/span>(<\/span>\"mmap test\"<\/span>)<\/span>;<\/span>\n\texit<\/span>(<\/span>EXIT_FAILURE)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n<\/p>\n

\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0<\/p>\n","protected":false},"excerpt":{"rendered":"

\u9a71\u52a8\u6d4b\u8bd5\u7a0b\u5e8f\u4e3b\u8981\u9a8c\u8bc1mmap\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff0c\u5b83\u9996\u5148\u901a\u8fc7mmap\u5c06\u5185\u6838\u7a7a\u95f4\u7684\u5185\u5b58\u6620\u5c04\u5230\u5e94\u7528\u5c42\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u7136\u540e\u901a\u8fc7\u8bfb\u5199\u5185\u5b58\u7684\u65b9\u5f0f\u5411\u5176\u4e2d\u5199\u5165\u6570\u636e\uff0c\u6700\u597d\u5728\u901a\u8fc7read\u51fd\u6570\u5c06\u5199\u5165\u7684\u6570\u636e\u8bfb\u53d6\u51fa\u6765<\/p>\n","protected":false},"author":1,"featured_media":295380,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[5240,5404,5403,5405,3034],"class_list":["post-295379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-mmap","tag-remap_pfn_range","tag-gxnc","tag-qdcscx","tag-qdcx"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/295379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=295379"}],"version-history":[{"count":0,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/295379\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/295380"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=295379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=295379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=295379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

\u4e0a\u673a\u5b9e\u9a8c\u4ece\u8fd9\u513f\u4e0b\u8f7d\u4ee3\u7801\uff0c\u8fdb\u884c\u7f16\u8bd1linux\u538b\u7f29\u547d\u4ee4\uff0c\u5e76\u62f7\u8d1d\u5230\u76ee\u6807\u677f\u8ddf\u6587\u4ef6\u7cfb\u7edf\u7684root\u76ee\u5f55\u4e2d\u901a\u8fc7\u547d\u4ee4<\/a>\u52a0\u8f7dinsmodmmap.ko\u9a71\u52a8\uff0c\u4e4b\u540e\u518d\u901a\u8fc7\u547d\u4ee4.\/app.out\u6267\u884clinux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891<\/strong>\uff0c\u53ef\u89c1\u5e94\u7528\u7a0b\u5e8f\u8f93\u51fa\u5b57\u7b26\u4e32\u201dabcdefghijklmnopqrstuvwxyz\u201c\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u9996\u5148\u901a\u8fc7mmap\u6620\u5c04\u5f97\u5230\u7684\u5730\u5740\u5199\u5165\u663e\u5b58\uff0c\u4e4b\u540e\u53c8\u901a\u8fc7read\u51fd\u6570\u8bfb\u51fa\u5e76\u8f93\u51fa<\/p>\n