The blog has been migrated to: http://kulv.sinaapp.com /.
The following functions show how the kernel address space is combined with the process address space.
/* <Br/> * this routine handles page faults. it determines the address, <br/> * and the problem, and then passes it off to one of the appropriate <br/> * routines. <br/> * error_code: <br/> * bit 0 = 0 means no page found, 1 means protection fault <br/> * bit 1 = 0 means read, 1 means write <br/> * bit 2 = 0 means kernel, 1 means user-mode <br/> */<br/> asmlinkage void do_page_fault (struct Pt_regs * regs, unsigned long error_code) <br/>{< br/> struct task_struct * tsk; <br/> struct mm_struct * mm; <br/> struct vm_area_struct * VMA; <br/> unsigned long address; <br/> unsigned long page; <br/> unsigned long fixup; <br/> int write; <br/> siginfo_t Info; </P> <p>/* Get the address */<br/>__ ASM _ ("movl % CR2, % 0 ": "= r" (Address); </P> <p> tsk = current; </P> <p>/* <br/> * We fault-in kernel-space virtual Memory on-demand. The <br/> * 'reference 'page table is init_mm.pgd. <br/> * Note! We must not take any locks for this case. we may <br/> * be in an interrupt or a critical region, and shocould <br/> * only copy the information from the master page table, <br/> * nothing more. <br/> */<br/> If (address> = task_size) // This is where the key is located. If the kernel address is 3 GB or above, so... you know. <Br/> goto vmalloc_fault; </P> <p> MM = tsk-> mm; <br/> info. si_code = segv_maperr; <br/>/* <br/> * If we're in an interrupt or have no user <br/> * context, we must not take the fault .. <br/> */<br/> If (in_interrupt () |! Mm) <br/> goto no_context; <br/> down (& mm-> mmap_sem); <br/> VMA = find_vma (mm, address ); <br/> If (! VMA) <br/> goto bad_area; <br/> If (VMA-> vm_start <= address) <br/> goto good_area; <br/> If (! (VMA-> vm_flags & vm_growsdown) <br/> goto bad_area; <br/> If (error_code & 4) {<br/>/* <br/> * accessing the stack below % ESP is always a bug. <br/> * The "+ 32" Is there due to some instructions (like <br/> * pusha) doing post-decrement on the stack and that <br/> * doesn't show up until later .. <br/> */<br/> If (address + 32 <regs-> ESP) <br/> goto bad_area; <br/>}< br/> If (expand_stack (VMA, addh SS) <br/> goto bad_area; <br/>/* <br/> * OK, we have a good vm_area for this memory access, so <br/> * We can handle it .. <br/> */<br/> good_area: <br/> info. si_code = segv_accerr; <br/> write = 0; <br/> switch (error_code & 3) {<br/> default:/* 3: write, present */<br/> # ifdef test_verify_area <br/> If (regs-> cs = kernel_cs) <br/> printk ("WP fault at % 08lx/N ", regs-> EIP); <br/> # endif <br/>/* fall THR Ough */<br/> case 2:/* write, not present */<br/> If (! (VMA-> vm_flags & vm_write) <br/> goto bad_area; <br/> write ++; <br/> break; <br/> case 1: /* read, present */<br/> goto bad_area; <br/> case 0:/* read, not present */<br/> If (! (VMA-> vm_flags & (vm_read | vm_exec) <br/> goto bad_area; <br/>}</P> <p>/* <br/> * if for any reason at all we couldn't handle the fault, <br/> * Make sure we exit gracefully rather than endlessly redo <br/> * the fault. <br/> */<br/> switch (handle_mm_fault (mm, VMA, address, write) {<br/> case 1: <br/> tsk-> min_flt ++; <br/> break; <br/> case 2: <br/> tsk-> maj_flt ++; <br/> break; <br/> case 0: <br/> GOTO Do_sigbus; <br/> default: <br/> goto out_of_memory; <br/>}</P> <p>/* <br/> * did it hit the DOS screen memory VA from vm86 mode? <Br/> */<br/> If (regs-> eflags & vm_mask) {<br/> unsigned long bit = (address-0xa0000)> page_shift; <br/> If (bit <32) <br/> tsk-> thread. screen_bitmap | = 1 <bit; <br/>}< br/> up (& mm-> mmap_sem); <br/> return; </P> <p>/* <br/> * something tried to access memory that isn't in our memory map .. <br/> * fix it, but check if it's kernel or user first .. <br/> */<br/> bad_area: <br/> up (& mm-> mmap_sem); </P> <P> bad_area_nosemaphore: <br/>/* User Mode accesses just cause a SIGSEGV */<br/> If (error_code & 4) {<br/> tsk-> thread. cr2 = address; <br/> tsk-> thread. error_code = error_code; <br/> tsk-> thread. trap_no = 14; <br/> info. si_signo = SIGSEGV; <br/> info. si_errno = 0; <br/>/* info. si_code has been set above */<br/> info. si_addr = (void *) address; <br/> force_sig_info (SIGSEGV, & info, tsk); <br/> return; <br />}</P> <p>/* <br/> * Pentium F0 0f C7 C8 bug workaround. <br/> */<br/> If (boot_cpu_data.f00f_bug) {<br/> unsigned long NR; </P> <p> Nr = (address-IDT)> 3; </P> <p> If (Nr = 6) {<br/> do_invalid_op (regs, 0); <br/> return; <br/>}</P> <p> no_context: <br/>/* Are we prepared to handle this Kernel Fault? */<Br/> If (fixup = search_exception_table (regs-> EIP ))! = 0) {<br/> regs-> EIP = fixup; <br/> return; <br/>}</P> <p>/* <br/> * oops. the kernel tried to access some bad page. we'll have to <br/> * terminate things with extreme prejudice. <br/> */</P> <p> bust_spinlocks (); </P> <p> If (address <page_size) <br/> printk (kern_alert "unable to handle kernel Null Pointer Dereference"); <br/> else <br/> printk (kern_alert "unable to handle kernel paging request "); <br/> P Rintk ("at virtual address % 08lx/N", address); <br/> printk ("printing EIP:/N "); <br/> printk ("% 08lx/N", regs-> EIP); <br/> ASM ("movl % 32a, % 0 ": "= r" (page); <br/> page = (unsigned long *) _ VA (page) [Address> 22]; <br/> printk (kern_alert "* PVDF = % 08lx/N", page); <br/> If (page & 1) {<br/> page & = page_mask; <br/> address & = 0x003ff000; <br/> page = (unsigned long *) _ VA (page) [Address> page_shift]; <Br/> printk (kern_alert "* PTE = % 08lx/N", page); <br/>}< br/> die ("oops", regs, error_code ); <br/> do_exit (sigkill); </P> <p>/* <br/> * we ran out of memory, or some other thing happened to us that made <br/> * us unable to handle the page fault gracefully. <br/> */<br/> out_of_memory: <br/> up (& mm-> mmap_sem); <br/> printk ("VM: killing process % s/n ", tsk-> comm); <br/> If (error_code & 4) <br/> do_exit (sigk Ill); <br/> goto no_context; </P> <p> do_sigbus: <br/> up (& mm-> mmap_sem ); </P> <p>/* <br/> * Send a sigbus, regardless of whether we were in kernel <br/> * or user mode. <br/> */<br/> tsk-> thread. cr2 = address; <br/> tsk-> thread. error_code = error_code; <br/> tsk-> thread. trap_no = 14; <br/> info. si_code = sigbus; <br/> info. si_errno = 0; <br/> info. si_code = bus_adrerr; <br/> info. si_addr = (void *) address; <br /> Force_sig_info (sigbus, & info, tsk); </P> <p>/* kernel mode? Handle exceptions or die */<br/> If (! (Error_code & 4) <br/> goto no_context; <br/> return; </P> <p> vmalloc_fault: // The following describes how to deal with the kernel address space. The kernel global page Directory table and process page Directory table are synchronized here <br/>{< br/>/* <br/> * synchronize this task's top level page-Table <br/> * With the 'reference' page table. <br/> */<br/> int offset = _ pgd_offset (Address); <br/> pgd_t * PGD, * pgd_k; <br/> pmd_t * PMD, * pmd_k; <br/> PGD = tsk-> active_mm-> PGD + offset; <br/> pgd_k = init_mm.pgd + offset; <br/> If (! Pgd_present (* PGD) {<br/> If (! Pgd_present (* pgd_k) <br/> goto bad_area_nosemaphore; <br/> set_pgd (PGD, * pgd_k); <br/> return; <br/>}< br/> PMD = pmd_offset (PGD, address); <br/> pmd_k = pmd_offset (pgd_k, address ); <br/> If (pmd_present (* PMD) |! Pmd_present (* pmd_k) <br/> goto bad_area_nosemaphore; <br/> set_pmd (PMD, * pmd_k); <br/> return; <br/>}< br/>