initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / m32r / mm / fault.c
blob393ff03ae73d878baba0287635d6e86de7092818
1 /*
2 * linux/arch/m32r/mm/fault.c
4 * Copyright (c) 2001, 2002 Hitoshi Yamamoto, and H. Kondo
6 * Some code taken from i386 version.
7 * Copyright (C) 1995 Linus Torvalds
8 */
10 /* $Id$ */
12 #include <linux/config.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/ptrace.h>
20 #include <linux/mman.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/tty.h>
27 #include <linux/vt_kern.h> /* For unblank_screen() */
28 #include <linux/highmem.h>
29 #include <linux/module.h>
31 #include <asm/m32r.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/hardirq.h>
35 #include <asm/mmu_context.h>
36 #include <asm/tlbflush.h>
38 extern void die(const char *, struct pt_regs *, long);
40 #ifndef CONFIG_SMP
41 asmlinkage unsigned int tlb_entry_i_dat;
42 asmlinkage unsigned int tlb_entry_d_dat;
43 #define tlb_entry_i tlb_entry_i_dat
44 #define tlb_entry_d tlb_entry_d_dat
45 #else
46 unsigned int tlb_entry_i_dat[NR_CPUS];
47 unsigned int tlb_entry_d_dat[NR_CPUS];
48 #define tlb_entry_i tlb_entry_i_dat[smp_processor_id()]
49 #define tlb_entry_d tlb_entry_d_dat[smp_processor_id()]
50 #endif
52 extern void init_tlb(void);
55 * Unlock any spinlocks which will prevent us from getting the
56 * message out
58 void bust_spinlocks(int yes)
60 int loglevel_save = console_loglevel;
62 if (yes) {
63 oops_in_progress = 1;
64 return;
66 #ifdef CONFIG_VT
67 unblank_screen();
68 #endif
69 oops_in_progress = 0;
71 * OK, the message is on the console. Now we call printk()
72 * without oops_in_progress set so that printk will give klogd
73 * a poke. Hold onto your hats...
75 console_loglevel = 15; /* NMI oopser may have shut the console up */
76 printk(" ");
77 console_loglevel = loglevel_save;
80 /*======================================================================*
81 * do_page_fault()
82 *======================================================================*
83 * This routine handles page faults. It determines the address,
84 * and the problem, and then passes it off to one of the appropriate
85 * routines.
87 * ARGUMENT:
88 * regs : M32R SP reg.
89 * error_code : See below
90 * address : M32R MMU MDEVA reg. (Operand ACE)
91 * : M32R BPC reg. (Instruction ACE)
93 * error_code :
94 * bit 0 == 0 means no page found, 1 means protection fault
95 * bit 1 == 0 means read, 1 means write
96 * bit 2 == 0 means kernel, 1 means user-mode
97 * bit 3 == 0 means data, 1 means instruction
98 *======================================================================*/
99 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code,
100 unsigned long address)
102 struct task_struct *tsk;
103 struct mm_struct *mm;
104 struct vm_area_struct * vma;
105 unsigned long page, addr;
106 int write;
107 siginfo_t info;
110 * If BPSW IE bit enable --> set PSW IE bit
112 if (regs->psw & M32R_PSW_BIE)
113 local_irq_enable();
115 tsk = current;
117 info.si_code = SEGV_MAPERR;
120 * We fault-in kernel-space virtual memory on-demand. The
121 * 'reference' page table is init_mm.pgd.
123 * NOTE! We MUST NOT take any locks for this case. We may
124 * be in an interrupt or a critical region, and should
125 * only copy the information from the master page table,
126 * nothing more.
128 * This verifies that the fault happens in kernel space
129 * (error_code & 4) == 0, and that the fault was not a
130 * protection error (error_code & 1) == 0.
132 if (address >= TASK_SIZE && !(error_code & 4))
133 goto vmalloc_fault;
135 mm = tsk->mm;
138 * If we're in an interrupt or have no user context or are running in an
139 * atomic region then we must not take the fault..
141 if (in_atomic() || !mm)
142 goto bad_area_nosemaphore;
144 /* When running in the kernel we expect faults to occur only to
145 * addresses in user space. All other faults represent errors in the
146 * kernel and should generate an OOPS. Unfortunatly, in the case of an
147 * erroneous fault occuring in a code path which already holds mmap_sem
148 * we will deadlock attempting to validate the fault against the
149 * address space. Luckily the kernel only validly references user
150 * space from well defined areas of code, which are listed in the
151 * exceptions table.
153 * As the vast majority of faults will be valid we will only perform
154 * the source reference check when there is a possibilty of a deadlock.
155 * Attempt to lock the address space, if we cannot we then validate the
156 * source. If this is invalid we can skip the address space check,
157 * thus avoiding the deadlock.
159 if (!down_read_trylock(&mm->mmap_sem)) {
160 if ((error_code & 4) == 0 &&
161 !search_exception_tables(regs->psw))
162 goto bad_area_nosemaphore;
163 down_read(&mm->mmap_sem);
166 vma = find_vma(mm, address);
167 if (!vma)
168 goto bad_area;
169 if (vma->vm_start <= address)
170 goto good_area;
171 if (!(vma->vm_flags & VM_GROWSDOWN))
172 goto bad_area;
173 #if 0
174 if (error_code & 4) {
176 * accessing the stack below "spu" is always a bug.
177 * The "+ 4" is there due to the push instruction
178 * doing pre-decrement on the stack and that
179 * doesn't show up until later..
181 if (address + 4 < regs->spu)
182 goto bad_area;
184 #endif
185 if (expand_stack(vma, address))
186 goto bad_area;
188 * Ok, we have a good vm_area for this memory access, so
189 * we can handle it..
191 good_area:
192 info.si_code = SEGV_ACCERR;
193 write = 0;
194 switch (error_code & 3) {
195 default: /* 3: write, present */
196 /* fall through */
197 case 2: /* write, not present */
198 if (!(vma->vm_flags & VM_WRITE))
199 goto bad_area;
200 write++;
201 break;
202 case 1: /* read, present */
203 case 0: /* read, not present */
204 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
205 goto bad_area;
208 survive:
210 * If for any reason at all we couldn't handle the fault,
211 * make sure we exit gracefully rather than endlessly redo
212 * the fault.
214 addr = (address & PAGE_MASK) | (error_code & 8);
215 switch (handle_mm_fault(mm, vma, addr, write)) {
216 case VM_FAULT_MINOR:
217 tsk->min_flt++;
218 break;
219 case VM_FAULT_MAJOR:
220 tsk->maj_flt++;
221 break;
222 case VM_FAULT_SIGBUS:
223 goto do_sigbus;
224 case VM_FAULT_OOM:
225 goto out_of_memory;
226 default:
227 BUG();
230 up_read(&mm->mmap_sem);
231 return;
234 * Something tried to access memory that isn't in our memory map..
235 * Fix it, but check if it's kernel or user first..
237 bad_area:
238 up_read(&mm->mmap_sem);
240 bad_area_nosemaphore:
241 /* User mode accesses just cause a SIGSEGV */
242 if (error_code & 4) {
243 tsk->thread.address = address;
244 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
245 tsk->thread.trap_no = 14;
246 info.si_signo = SIGSEGV;
247 info.si_errno = 0;
248 /* info.si_code has been set above */
249 info.si_addr = (void __user *)address;
250 force_sig_info(SIGSEGV, &info, tsk);
251 return;
254 no_context:
255 /* Are we prepared to handle this kernel fault? */
256 if (fixup_exception(regs))
257 return;
260 * Oops. The kernel tried to access some bad page. We'll have to
261 * terminate things with extreme prejudice.
264 bust_spinlocks(1);
266 if (address < PAGE_SIZE)
267 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
268 else
269 printk(KERN_ALERT "Unable to handle kernel paging request");
270 printk(" at virtual address %08lx\n",address);
271 printk(KERN_ALERT " printing bpc:\n");
272 printk("%08lx\n", regs->bpc);
273 page = *(unsigned long *)MPTB;
274 page = ((unsigned long *) page)[address >> PGDIR_SHIFT];
275 printk(KERN_ALERT "*pde = %08lx\n", page);
276 if (page & _PAGE_PRESENT) {
277 page &= PAGE_MASK;
278 address &= 0x003ff000;
279 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
280 printk(KERN_ALERT "*pte = %08lx\n", page);
282 die("Oops", regs, error_code);
283 bust_spinlocks(0);
284 do_exit(SIGKILL);
287 * We ran out of memory, or some other thing happened to us that made
288 * us unable to handle the page fault gracefully.
290 out_of_memory:
291 up_read(&mm->mmap_sem);
292 if (tsk->pid == 1) {
293 yield();
294 down_read(&mm->mmap_sem);
295 goto survive;
297 printk("VM: killing process %s\n", tsk->comm);
298 if (error_code & 4)
299 do_exit(SIGKILL);
300 goto no_context;
302 do_sigbus:
303 up_read(&mm->mmap_sem);
305 /* Kernel mode? Handle exception or die */
306 if (!(error_code & 4))
307 goto no_context;
309 tsk->thread.address = address;
310 tsk->thread.error_code = error_code;
311 tsk->thread.trap_no = 14;
312 info.si_signo = SIGBUS;
313 info.si_errno = 0;
314 info.si_code = BUS_ADRERR;
315 info.si_addr = (void __user *)address;
316 force_sig_info(SIGBUS, &info, tsk);
317 return;
319 vmalloc_fault:
322 * Synchronize this task's top level page-table
323 * with the 'reference' page table.
325 * Do _not_ use "tsk" here. We might be inside
326 * an interrupt in the middle of a task switch..
328 int offset = pgd_index(address);
329 pgd_t *pgd, *pgd_k;
330 pmd_t *pmd, *pmd_k;
331 pte_t *pte_k;
333 pgd = (pgd_t *)*(unsigned long *)MPTB;
334 pgd = offset + (pgd_t *)pgd;
335 pgd_k = init_mm.pgd + offset;
337 if (!pgd_present(*pgd_k))
338 goto no_context;
341 * set_pgd(pgd, *pgd_k); here would be useless on PAE
342 * and redundant with the set_pmd() on non-PAE.
345 pmd = pmd_offset(pgd, address);
346 pmd_k = pmd_offset(pgd_k, address);
347 if (!pmd_present(*pmd_k))
348 goto no_context;
349 set_pmd(pmd, *pmd_k);
351 pte_k = pte_offset_kernel(pmd_k, address);
352 if (!pte_present(*pte_k))
353 goto no_context;
355 addr = (address & PAGE_MASK) | (error_code & 8);
356 update_mmu_cache(NULL, addr, *pte_k);
357 return;
361 /*======================================================================*
362 * update_mmu_cache()
363 *======================================================================*/
364 #define TLB_MASK (NR_TLB_ENTRIES - 1)
365 #define ITLB_END (unsigned long *)(ITLB_BASE + (NR_TLB_ENTRIES * 8))
366 #define DTLB_END (unsigned long *)(DTLB_BASE + (NR_TLB_ENTRIES * 8))
367 void update_mmu_cache(struct vm_area_struct *vma, unsigned long vaddr,
368 pte_t pte)
370 unsigned long *entry1, *entry2;
371 unsigned long pte_data, flags;
372 unsigned int *entry_dat;
373 int inst = vaddr & 8;
374 int i;
376 /* Ptrace may call this routine. */
377 if (vma && current->active_mm != vma->vm_mm)
378 return;
380 local_irq_save(flags);
382 vaddr = (vaddr & PAGE_MASK) | get_asid();
384 #ifdef CONFIG_CHIP_OPSP
385 entry1 = (unsigned long *)ITLB_BASE;
386 for(i = 0 ; i < NR_TLB_ENTRIES; i++) {
387 if(*entry1++ == vaddr) {
388 pte_data = pte_val(pte);
389 set_tlb_data(entry1, pte_data);
390 break;
392 entry1++;
394 entry2 = (unsigned long *)DTLB_BASE;
395 for(i = 0 ; i < NR_TLB_ENTRIES ; i++) {
396 if(*entry2++ == vaddr) {
397 pte_data = pte_val(pte);
398 set_tlb_data(entry2, pte_data);
399 break;
401 entry2++;
403 local_irq_restore(flags);
404 return;
405 #else
406 pte_data = pte_val(pte);
409 * Update TLB entries
410 * entry1: ITLB entry address
411 * entry2: DTLB entry address
413 __asm__ __volatile__ (
414 "seth %0, #high(%4) \n\t"
415 "st %2, @(%5, %0) \n\t"
416 "ldi %1, #1 \n\t"
417 "st %1, @(%6, %0) \n\t"
418 "add3 r4, %0, %7 \n\t"
419 ".fillinsn \n"
420 "1: \n\t"
421 "ld %1, @(%6, %0) \n\t"
422 "bnez %1, 1b \n\t"
423 "ld %0, @r4+ \n\t"
424 "ld %1, @r4 \n\t"
425 "st %3, @+%0 \n\t"
426 "st %3, @+%1 \n\t"
427 : "=&r" (entry1), "=&r" (entry2)
428 : "r" (vaddr), "r" (pte_data), "i" (MMU_REG_BASE),
429 "i" (MSVA_offset), "i" (MTOP_offset), "i" (MIDXI_offset)
430 : "r4", "memory"
433 if ((!inst && entry2 >= DTLB_END) || (inst && entry1 >= ITLB_END))
434 goto notfound;
436 found:
437 local_irq_restore(flags);
439 return;
441 /* Valid entry not found */
442 notfound:
444 * Update ITLB or DTLB entry
445 * entry1: TLB entry address
446 * entry2: TLB base address
448 if (!inst) {
449 entry2 = (unsigned long *)DTLB_BASE;
450 entry_dat = &tlb_entry_d;
451 } else {
452 entry2 = (unsigned long *)ITLB_BASE;
453 entry_dat = &tlb_entry_i;
455 entry1 = entry2 + (((*entry_dat - 1) & TLB_MASK) << 1);
457 for (i = 0 ; i < NR_TLB_ENTRIES ; i++) {
458 if (!(entry1[1] & 2)) /* Valid bit check */
459 break;
461 if (entry1 != entry2)
462 entry1 -= 2;
463 else
464 entry1 += TLB_MASK << 1;
467 if (i >= NR_TLB_ENTRIES) { /* Empty entry not found */
468 entry1 = entry2 + (*entry_dat << 1);
469 *entry_dat = (*entry_dat + 1) & TLB_MASK;
471 *entry1++ = vaddr; /* Set TLB tag */
472 set_tlb_data(entry1, pte_data);
474 goto found;
475 #endif
478 /*======================================================================*
479 * flush_tlb_page() : flushes one page
480 *======================================================================*/
481 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
483 if (vma->vm_mm && mm_context(vma->vm_mm) != NO_CONTEXT) {
484 unsigned long flags;
486 local_irq_save(flags);
487 page &= PAGE_MASK;
488 page |= (mm_context(vma->vm_mm) & MMU_CONTEXT_ASID_MASK);
489 __flush_tlb_page(page);
490 local_irq_restore(flags);
494 /*======================================================================*
495 * flush_tlb_range() : flushes a range of pages
496 *======================================================================*/
497 void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
498 unsigned long end)
500 struct mm_struct *mm;
502 mm = vma->vm_mm;
503 if (mm_context(mm) != NO_CONTEXT) {
504 unsigned long flags;
505 int size;
507 local_irq_save(flags);
508 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
509 if (size > (NR_TLB_ENTRIES / 4)) { /* Too many TLB to flush */
510 mm_context(mm) = NO_CONTEXT;
511 if (mm == current->mm)
512 activate_context(mm);
513 } else {
514 unsigned long asid;
516 asid = mm_context(mm) & MMU_CONTEXT_ASID_MASK;
517 start &= PAGE_MASK;
518 end += (PAGE_SIZE - 1);
519 end &= PAGE_MASK;
521 start |= asid;
522 end |= asid;
523 while (start < end) {
524 __flush_tlb_page(start);
525 start += PAGE_SIZE;
528 local_irq_restore(flags);
532 /*======================================================================*
533 * flush_tlb_mm() : flushes the specified mm context TLB's
534 *======================================================================*/
535 void local_flush_tlb_mm(struct mm_struct *mm)
537 /* Invalidate all TLB of this process. */
538 /* Instead of invalidating each TLB, we get new MMU context. */
539 if (mm_context(mm) != NO_CONTEXT) {
540 unsigned long flags;
542 local_irq_save(flags);
543 mm_context(mm) = NO_CONTEXT;
544 if (mm == current->mm)
545 activate_context(mm);
546 local_irq_restore(flags);
550 /*======================================================================*
551 * flush_tlb_all() : flushes all processes TLBs
552 *======================================================================*/
553 void local_flush_tlb_all(void)
555 unsigned long flags;
557 local_irq_save(flags);
558 __flush_tlb_all();
559 local_irq_restore(flags);
562 /*======================================================================*
563 * init_mmu()
564 *======================================================================*/
565 void __init init_mmu(void)
567 tlb_entry_i = 0;
568 tlb_entry_d = 0;
569 mmu_context_cache = MMU_CONTEXT_FIRST_VERSION;
570 set_asid(mmu_context_cache & MMU_CONTEXT_ASID_MASK);
571 *(volatile unsigned long *)MPTB = (unsigned long)swapper_pg_dir;