/*1* The SH64 TLB miss.2*3* Original code from fault.c4* Copyright (C) 2000, 2001 Paolo Alberelli5*6* Fast PTE->TLB refill path7* Copyright (C) 2003 [email protected]8*9* IMPORTANT NOTES :10* The do_fast_page_fault function is called from a context in entry.S11* where very few registers have been saved. In particular, the code in12* this file must be compiled not to use ANY caller-save registers that13* are not part of the restricted save set. Also, it means that code in14* this file must not make calls to functions elsewhere in the kernel, or15* else the excepting context will see corruption in its caller-save16* registers. Plus, the entry.S save area is non-reentrant, so this code17* has to run with SR.BL==1, i.e. no interrupts taken inside it and panic18* on any exception.19*20* This file is subject to the terms and conditions of the GNU General Public21* License. See the file "COPYING" in the main directory of this archive22* for more details.23*/24#include <linux/signal.h>25#include <linux/sched.h>26#include <linux/kernel.h>27#include <linux/errno.h>28#include <linux/string.h>29#include <linux/types.h>30#include <linux/ptrace.h>31#include <linux/mman.h>32#include <linux/mm.h>33#include <linux/smp.h>34#include <linux/interrupt.h>35#include <asm/system.h>36#include <asm/tlb.h>37#include <asm/io.h>38#include <asm/uaccess.h>39#include <asm/pgalloc.h>40#include <asm/mmu_context.h>41#include <cpu/registers.h>4243/* Callable from fault.c, so not static */44inline void __do_tlb_refill(unsigned long address,45unsigned long long is_text_not_data, pte_t *pte)46{47unsigned long long ptel;48unsigned long long pteh=0;49struct tlb_info *tlbp;50unsigned long long next;5152/* Get PTEL first */53ptel = pte_val(*pte);5455/*56* Set PTEH register57*/58pteh = neff_sign_extend(address & MMU_VPN_MASK);5960/* Set the ASID. */61pteh |= get_asid() << PTEH_ASID_SHIFT;62pteh |= PTEH_VALID;6364/* Set PTEL register, set_pte has performed the sign extension */65ptel &= _PAGE_FLAGS_HARDWARE_MASK; /* drop software flags */6667tlbp = is_text_not_data ? &(cpu_data->itlb) : &(cpu_data->dtlb);68next = tlbp->next;69__flush_tlb_slot(next);70asm volatile ("putcfg %0,1,%2\n\n\t"71"putcfg %0,0,%1\n"72: : "r" (next), "r" (pteh), "r" (ptel) );7374next += TLB_STEP;75if (next > tlbp->last) next = tlbp->first;76tlbp->next = next;7778}7980static int handle_vmalloc_fault(struct mm_struct *mm,81unsigned long protection_flags,82unsigned long long textaccess,83unsigned long address)84{85pgd_t *dir;86pud_t *pud;87pmd_t *pmd;88static pte_t *pte;89pte_t entry;9091dir = pgd_offset_k(address);9293pud = pud_offset(dir, address);94if (pud_none_or_clear_bad(pud))95return 0;9697pmd = pmd_offset(pud, address);98if (pmd_none_or_clear_bad(pmd))99return 0;100101pte = pte_offset_kernel(pmd, address);102entry = *pte;103104if (pte_none(entry) || !pte_present(entry))105return 0;106if ((pte_val(entry) & protection_flags) != protection_flags)107return 0;108109__do_tlb_refill(address, textaccess, pte);110111return 1;112}113114static int handle_tlbmiss(struct mm_struct *mm,115unsigned long long protection_flags,116unsigned long long textaccess,117unsigned long address)118{119pgd_t *dir;120pud_t *pud;121pmd_t *pmd;122pte_t *pte;123pte_t entry;124125/* NB. The PGD currently only contains a single entry - there is no126page table tree stored for the top half of the address space since127virtual pages in that region should never be mapped in user mode.128(In kernel mode, the only things in that region are the 512Mb super129page (locked in), and vmalloc (modules) + I/O device pages (handled130by handle_vmalloc_fault), so no PGD for the upper half is required131by kernel mode either).132133See how mm->pgd is allocated and initialised in pgd_alloc to see why134the next test is necessary. - RPC */135if (address >= (unsigned long) TASK_SIZE)136/* upper half - never has page table entries. */137return 0;138139dir = pgd_offset(mm, address);140if (pgd_none(*dir) || !pgd_present(*dir))141return 0;142if (!pgd_present(*dir))143return 0;144145pud = pud_offset(dir, address);146if (pud_none(*pud) || !pud_present(*pud))147return 0;148149pmd = pmd_offset(pud, address);150if (pmd_none(*pmd) || !pmd_present(*pmd))151return 0;152153pte = pte_offset_kernel(pmd, address);154entry = *pte;155156if (pte_none(entry) || !pte_present(entry))157return 0;158159/*160* If the page doesn't have sufficient protection bits set to161* service the kind of fault being handled, there's not much162* point doing the TLB refill. Punt the fault to the general163* handler.164*/165if ((pte_val(entry) & protection_flags) != protection_flags)166return 0;167168__do_tlb_refill(address, textaccess, pte);169170return 1;171}172173/*174* Put all this information into one structure so that everything is just175* arithmetic relative to a single base address. This reduces the number176* of movi/shori pairs needed just to load addresses of static data.177*/178struct expevt_lookup {179unsigned short protection_flags[8];180unsigned char is_text_access[8];181unsigned char is_write_access[8];182};183184#define PRU (1<<9)185#define PRW (1<<8)186#define PRX (1<<7)187#define PRR (1<<6)188189#define DIRTY (_PAGE_DIRTY | _PAGE_ACCESSED)190#define YOUNG (_PAGE_ACCESSED)191192/* Sized as 8 rather than 4 to allow checking the PTE's PRU bit against whether193the fault happened in user mode or privileged mode. */194static struct expevt_lookup expevt_lookup_table = {195.protection_flags = {PRX, PRX, 0, 0, PRR, PRR, PRW, PRW},196.is_text_access = {1, 1, 0, 0, 0, 0, 0, 0}197};198199/*200This routine handles page faults that can be serviced just by refilling a201TLB entry from an existing page table entry. (This case represents a very202large majority of page faults.) Return 1 if the fault was successfully203handled. Return 0 if the fault could not be handled. (This leads into the204general fault handling in fault.c which deals with mapping file-backed205pages, stack growth, segmentation faults, swapping etc etc)206*/207asmlinkage int do_fast_page_fault(unsigned long long ssr_md,208unsigned long long expevt,209unsigned long address)210{211struct task_struct *tsk;212struct mm_struct *mm;213unsigned long long textaccess;214unsigned long long protection_flags;215unsigned long long index;216unsigned long long expevt4;217218/* The next few lines implement a way of hashing EXPEVT into a219* small array index which can be used to lookup parameters220* specific to the type of TLBMISS being handled.221*222* Note:223* ITLBMISS has EXPEVT==0xa40224* RTLBMISS has EXPEVT==0x040225* WTLBMISS has EXPEVT==0x060226*/227expevt4 = (expevt >> 4);228/* TODO : xor ssr_md into this expression too. Then we can check229* that PRU is set when it needs to be. */230index = expevt4 ^ (expevt4 >> 5);231index &= 7;232protection_flags = expevt_lookup_table.protection_flags[index];233textaccess = expevt_lookup_table.is_text_access[index];234235/* SIM236* Note this is now called with interrupts still disabled237* This is to cope with being called for a missing IO port238* address with interrupts disabled. This should be fixed as239* soon as we have a better 'fast path' miss handler.240*241* Plus take care how you try and debug this stuff.242* For example, writing debug data to a port which you243* have just faulted on is not going to work.244*/245246tsk = current;247mm = tsk->mm;248249if ((address >= VMALLOC_START && address < VMALLOC_END) ||250(address >= IOBASE_VADDR && address < IOBASE_END)) {251if (ssr_md)252/*253* Process-contexts can never have this address254* range mapped255*/256if (handle_vmalloc_fault(mm, protection_flags,257textaccess, address))258return 1;259} else if (!in_interrupt() && mm) {260if (handle_tlbmiss(mm, protection_flags, textaccess, address))261return 1;262}263264return 0;265}266267268