question: in the kernel mode (the Linux Kernel), a buffer is allocated. for 
some reasons, this buffer is also required to be used in the user mode. how to 
do this?

my idea: in the kernel mode, firstly, a buffer is allocated with vmalloc(), 
then about this buffer the physical addresses or the address of pages are 
known. so what should be done next is to allocate a buffer in the user mode 
with the same size, and replace the pages of the user mode buffer with the 
pages of the kernel mode buffer one by one. 

pseudo code:
    // to allocate 
    void * k_ad = vmalloc(BUFFER_SIZE);
    // to setup VMA
    void * u_ad = do_mmap(NULL, 0, BUFFER_SIZE, PROT_READ | PROT_WRITE | 
            PROT_EXEC, MAP_PRIVATE, 0);
    //
    unsigned long ulStart, ulPage, ulPos;
    struct page *page;
    ulStart = u_ad;
    ulPos = k_ad;
    while (nSize > 0) {
        ulPage = kvirt_to_phys(ulPos);
        page = virt_to_page(__va(ulPage));
        //
        mem_map_reserve(page);
        //
        spin_lock(��t->mm->page_table_lock);
        pgd_t *pgd = pgd_offset(current->mm, ulStart);
        pmd_t *pmd;
        pte_t *ptep;
        //
        if (!pgd_none(*pgd)) {
            pmd = pmd_offset(pgd, ulStart);
            if (!pmd_none(*pmd)) {
                ptep = pte_offset(pmd, ulStart);
                // free the old page
                put_page(pte_page(*ptep));
                // link the new page from vmalloc() to the user mode 
                // virtual address' pte
                set_pte(ptep, mk_pte_phys(ulPage, PAGE_SHARED));
            }
        }
        spin_unlock(��t->mm->page_table_lock);
        //  
        ulStart += PAGE_SIZE;
        ulPos += PAGE_SIZE;
        nSize -= PAGE_SIZE;
    }

    // then k_ad is the address of the buffer in the kernel mode,
    // and u_ad is the address of the buffer in the user mode.
--

�� �薷�:��InfoMagic � Mar  7 11:34:43 2005 �薷谋��摹�[FROM: 203.118.36.*]
�� ��源:��BBS 水木�华站 http://smth.org��[FROM: 203.125.170.*]