fault.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:12k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  arch/s390/mm/fault.c
  3.  *
  4.  *  S390 version
  5.  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6.  *    Author(s): Hartmut Penner (hp@de.ibm.com)
  7.  *               Ulrich Weigand (uweigand@de.ibm.com)
  8.  *
  9.  *  Derived from "arch/i386/mm/fault.c"
  10.  *    Copyright (C) 1995  Linus Torvalds
  11.  */
  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/init.h>
  25. #include <linux/console.h>
  26. #include <asm/system.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/hardirq.h>
  30. #ifdef CONFIG_SYSCTL
  31. extern int sysctl_userprocess_debug;
  32. #endif
  33. extern void die(const char *,struct pt_regs *,long);
  34. extern spinlock_t timerlist_lock;
  35. /*
  36.  * Unlock any spinlocks which will prevent us from getting the
  37.  * message out (timerlist_lock is acquired through the
  38.  * console unblank code)
  39.  */
  40. void bust_spinlocks(int yes)
  41. {
  42. spin_lock_init(&timerlist_lock);
  43. if (yes) {
  44. oops_in_progress = 1;
  45. } else {
  46. int loglevel_save = console_loglevel;
  47. oops_in_progress = 0;
  48. console_unblank();
  49. /*
  50.  * OK, the message is on the console.  Now we call printk()
  51.  * without oops_in_progress set so that printk will give klogd
  52.  * a poke.  Hold onto your hats...
  53.  */
  54. console_loglevel = 15;
  55. printk(" ");
  56. console_loglevel = loglevel_save;
  57. }
  58. }
  59. /*
  60.  * Check which address space is addressed by the access
  61.  * register in S390_lowcore.exc_access_id.
  62.  * Returns 1 for user space and 0 for kernel space.
  63.  */
  64. static int __check_access_register(struct pt_regs *regs, int error_code)
  65. {
  66. int areg = S390_lowcore.exc_access_id;
  67. if (areg == 0)
  68. /* Access via access register 0 -> kernel address */
  69. return 0;
  70. if (regs && areg < NUM_ACRS && regs->acrs[areg] <= 1)
  71. /*
  72.  * access register contains 0 -> kernel address,
  73.  * access register contains 1 -> user space address
  74.  */
  75. return regs->acrs[areg];
  76. /* Something unhealthy was done with the access registers... */
  77. die("page fault via unknown access register", regs, error_code);
  78. do_exit(SIGKILL);
  79. return 0;
  80. }
  81. /*
  82.  * Check which address space the address belongs to.
  83.  * Returns 1 for user space and 0 for kernel space.
  84.  */
  85. static inline int check_user_space(struct pt_regs *regs, int error_code)
  86. {
  87. /*
  88.  * The lowest two bits of S390_lowcore.trans_exc_code indicate
  89.  * which paging table was used:
  90.  *   0: Primary Segment Table Descriptor
  91.  *   1: STD determined via access register
  92.  *   2: Secondary Segment Table Descriptor
  93.  *   3: Home Segment Table Descriptor
  94.  */
  95. int descriptor = S390_lowcore.trans_exc_code & 3;
  96. if (descriptor == 1)
  97. return __check_access_register(regs, error_code);
  98. return descriptor >> 1;
  99. }
  100. /*
  101.  * Send SIGSEGV to task.  This is an external routine
  102.  * to keep the stack usage of do_page_fault small.
  103.  */
  104. static void force_sigsegv(struct pt_regs *regs, unsigned long error_code,
  105.   int si_code, unsigned long address)
  106. {
  107. struct siginfo si;
  108. #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
  109. #if defined(CONFIG_SYSCTL)
  110. if (sysctl_userprocess_debug)
  111. #endif
  112. {
  113. printk("User process fault: interruption code 0x%lXn",
  114.        error_code);
  115. printk("failing address: %lXn", address);
  116. show_regs(regs);
  117. }
  118. #endif
  119. si.si_signo = SIGSEGV;
  120. si.si_code = si_code;
  121. si.si_addr = (void *) address;
  122. force_sig_info(SIGSEGV, &si, current);
  123. }
  124. /*
  125.  * This routine handles page faults.  It determines the address,
  126.  * and the problem, and then passes it off to one of the appropriate
  127.  * routines.
  128.  *
  129.  * error_code:
  130.  *   04       Protection           ->  Write-Protection  (suprression)
  131.  *   10       Segment translation  ->  Not present       (nullification)
  132.  *   11       Page translation     ->  Not present       (nullification)
  133.  *   3b       Region third trans.  ->  Not present       (nullification)
  134.  */
  135. extern inline void do_exception(struct pt_regs *regs, unsigned long error_code)
  136. {
  137.         struct task_struct *tsk;
  138.         struct mm_struct *mm;
  139.         struct vm_area_struct * vma;
  140.         unsigned long address;
  141. int user_address;
  142.         unsigned long fixup;
  143. int si_code = SEGV_MAPERR;
  144.         tsk = current;
  145.         mm = tsk->mm;
  146. /* 
  147.          * Check for low-address protection.  This needs to be treated
  148.  * as a special case because the translation exception code 
  149.  * field is not guaranteed to contain valid data in this case.
  150.  */
  151. if (error_code == 4 && !(S390_lowcore.trans_exc_code & 4)) {
  152. /* Low-address protection hit in kernel mode means 
  153.    NULL pointer write access in kernel mode.  */
  154.   if (!(regs->psw.mask & PSW_PROBLEM_STATE)) {
  155. address = 0;
  156. user_address = 0;
  157. goto no_context;
  158. }
  159. /* Low-address protection hit in user mode 'cannot happen'.  */
  160. die ("Low-address protection", regs, error_code);
  161.          do_exit(SIGKILL);
  162. }
  163.         /* 
  164.          * get the failing address 
  165.          * more specific the segment and page table portion of 
  166.          * the address 
  167.          */
  168.         address = S390_lowcore.trans_exc_code & -4096L;
  169. user_address = check_user_space(regs, error_code);
  170. /*
  171.  * Verify that the fault happened in user space, that
  172.  * we are not in an interrupt and that there is a 
  173.  * user context.
  174.  */
  175.         if (user_address == 0 || in_interrupt() || !mm)
  176.                 goto no_context;
  177. /*
  178.  * When we get here, the fault happened in the current
  179.  * task's user address space, so we can switch on the
  180.  * interrupts again and then search the VMAs
  181.  */
  182. __sti();
  183.         down_read(&mm->mmap_sem);
  184.         vma = find_vma(mm, address);
  185.         if (!vma)
  186.                 goto bad_area;
  187.         if (vma->vm_start <= address) 
  188.                 goto good_area;
  189.         if (!(vma->vm_flags & VM_GROWSDOWN))
  190.                 goto bad_area;
  191.         if (expand_stack(vma, address))
  192.                 goto bad_area;
  193. /*
  194.  * Ok, we have a good vm_area for this memory access, so
  195.  * we can handle it..
  196.  */
  197. good_area:
  198. si_code = SEGV_ACCERR;
  199. if (error_code != 4) {
  200. /* page not present, check vm flags */
  201. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  202. goto bad_area;
  203. } else {
  204. if (!(vma->vm_flags & VM_WRITE))
  205. goto bad_area;
  206. }
  207. survive:
  208. /*
  209.  * If for any reason at all we couldn't handle the fault,
  210.  * make sure we exit gracefully rather than endlessly redo
  211.  * the fault.
  212.  */
  213. switch (handle_mm_fault(mm, vma, address, error_code == 4)) {
  214. case 1:
  215. tsk->min_flt++;
  216. break;
  217. case 2:
  218. tsk->maj_flt++;
  219. break;
  220. case 0:
  221. goto do_sigbus;
  222. default:
  223. goto out_of_memory;
  224. }
  225.         up_read(&mm->mmap_sem);
  226.         return;
  227. /*
  228.  * Something tried to access memory that isn't in our memory map..
  229.  * Fix it, but check if it's kernel or user first..
  230.  */
  231. bad_area:
  232.         up_read(&mm->mmap_sem);
  233.         /* User mode accesses just cause a SIGSEGV */
  234.         if (regs->psw.mask & PSW_PROBLEM_STATE) {
  235.                 tsk->thread.prot_addr = address;
  236.                 tsk->thread.trap_no = error_code;
  237. force_sigsegv(regs, error_code, si_code, address);
  238.                 return;
  239. }
  240. no_context:
  241.         /* Are we prepared to handle this kernel fault?  */
  242.         if ((fixup = search_exception_table(regs->psw.addr)) != 0) {
  243.                 regs->psw.addr = fixup;
  244.                 return;
  245.         }
  246. /*
  247.  * Oops. The kernel tried to access some bad page. We'll have to
  248.  * terminate things with extreme prejudice.
  249.  */
  250.         if (user_address == 0)
  251.                 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  252.                 " at virtual kernel address %016lxn", address);
  253.         else
  254.                 printk(KERN_ALERT "Unable to handle kernel paging request"
  255.        " at virtual user address %016lxn", address);
  256.         die("Oops", regs, error_code);
  257.         do_exit(SIGKILL);
  258. /*
  259.  * We ran out of memory, or some other thing happened to us that made
  260.  * us unable to handle the page fault gracefully.
  261. */
  262. out_of_memory:
  263. if (tsk->pid == 1) {
  264. yield();
  265. goto survive;
  266. }
  267. up_read(&mm->mmap_sem);
  268. printk("VM: killing process %sn", tsk->comm);
  269. if (regs->psw.mask & PSW_PROBLEM_STATE)
  270. do_exit(SIGKILL);
  271. goto no_context;
  272. do_sigbus:
  273. up_read(&mm->mmap_sem);
  274. /*
  275.  * Send a sigbus, regardless of whether we were in kernel
  276.  * or user mode.
  277.  */
  278.         tsk->thread.prot_addr = address;
  279.         tsk->thread.trap_no = error_code;
  280. force_sig(SIGBUS, tsk);
  281. /* Kernel mode? Handle exceptions or die */
  282. if (!(regs->psw.mask & PSW_PROBLEM_STATE))
  283. goto no_context;
  284. }
  285. void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
  286. {
  287. regs->psw.addr -= (error_code >> 16);
  288. do_exception(regs, 4);
  289. }
  290. void do_segment_exception(struct pt_regs *regs, unsigned long error_code)
  291. {
  292. do_exception(regs, 0x10);
  293. }
  294. void do_page_exception(struct pt_regs *regs, unsigned long error_code)
  295. {
  296. do_exception(regs, 0x11);
  297. }
  298. void do_region_exception(struct pt_regs *regs, unsigned long error_code)
  299. {
  300. do_exception(regs, 0x3b);
  301. }
  302. #ifdef CONFIG_PFAULT
  303. /*
  304.  * 'pfault' pseudo page faults routines.
  305.  */
  306. static int pfault_disable = 0;
  307. static int __init nopfault(char *str)
  308. {
  309. pfault_disable = 1;
  310. return 1;
  311. }
  312. __setup("nopfault", nopfault);
  313. typedef struct {
  314. __u16 refdiagc;
  315. __u16 reffcode;
  316. __u16 refdwlen;
  317. __u16 refversn;
  318. __u64 refgaddr;
  319. __u64 refselmk;
  320. __u64 refcmpmk;
  321. __u64 reserved;
  322. } __attribute__ ((packed)) pfault_refbk_t;
  323. typedef struct _pseudo_wait_t {
  324.        struct _pseudo_wait_t *next;
  325.        wait_queue_head_t queue;
  326.        unsigned long address;
  327.        int resolved;
  328. } pseudo_wait_t;
  329. int pfault_init(void)
  330. {
  331. pfault_refbk_t refbk =
  332. { 0x258, 0, 5, 2, __LC_KERNEL_STACK, 1ULL << 48, 1ULL << 48,
  333.           0x8000000000000000ULL };
  334.         int rc;
  335. if (pfault_disable)
  336. return -1;
  337.         __asm__ __volatile__(
  338.                 "    diag  %1,%0,0x258n"
  339. "0:  j     2fn"
  340. "1:  la    %0,8n"
  341. "2:n"
  342. ".section __ex_table,"a"n"
  343. "   .align 4n"
  344. "   .quad  0b,1bn"
  345. ".previous"
  346.                 : "=d" (rc) : "a" (&refbk) : "cc" );
  347. __ctl_set_bit(0, 9);
  348.         return rc;
  349. }
  350. void pfault_fini(void)
  351. {
  352. pfault_refbk_t refbk =
  353. { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
  354. if (pfault_disable)
  355. return;
  356. __ctl_clear_bit(0, 9);
  357.         __asm__ __volatile__(
  358.                 "    diag  %0,0,0x258n"
  359. "0:n"
  360. ".section __ex_table,"a"n"
  361. "   .align 4n"
  362. "   .quad  0b,0bn"
  363. ".previous"
  364. : : "a" (&refbk) : "cc" );
  365. }
  366. asmlinkage void
  367. pfault_interrupt(struct pt_regs *regs, __u16 error_code)
  368. {
  369. struct task_struct *tsk;
  370. wait_queue_head_t queue;
  371. wait_queue_head_t *qp;
  372. __u16 subcode;
  373. /*
  374.  * Get the external interruption subcode & pfault
  375.  * initial/completion signal bit. VM stores this 
  376.  * in the 'cpu address' field associated with the
  377.          * external interrupt. 
  378.  */
  379. subcode = S390_lowcore.cpu_addr;
  380. if ((subcode & 0xff00) != 0x0600)
  381. return;
  382. /*
  383.  * Get the token (= address of kernel stack of affected task).
  384.  */
  385. tsk = (struct task_struct *)
  386. (*((unsigned long *) __LC_PFAULT_INTPARM) - THREAD_SIZE);
  387. /*
  388.  * We got all needed information from the lowcore and can
  389.  * now safely switch on interrupts.
  390.  */
  391. if (regs->psw.mask & PSW_PROBLEM_STATE)
  392. __sti();
  393. if (subcode & 0x0080) {
  394. /* signal bit is set -> a page has been swapped in by VM */
  395. qp = (wait_queue_head_t *)
  396. xchg(&tsk->thread.pfault_wait, -1);
  397. if (qp != NULL) {
  398. /* Initial interrupt was faster than the completion
  399.  * interrupt. pfault_wait is valid. Set pfault_wait
  400.  * back to zero and wake up the process. This can
  401.  * safely be done because the task is still sleeping
  402.  * and can't procude new pfaults. */
  403. tsk->thread.pfault_wait = 0ULL;
  404. wake_up(qp);
  405. }
  406. } else {
  407. /* signal bit not set -> a real page is missing. */
  408.                 init_waitqueue_head (&queue);
  409. qp = (wait_queue_head_t *)
  410. xchg(&tsk->thread.pfault_wait, (addr_t) &queue);
  411. if (qp != NULL) {
  412. /* Completion interrupt was faster than the initial
  413.  * interrupt (swapped in a -1 for pfault_wait). Set
  414.  * pfault_wait back to zero and exit. This can be
  415.  * done safely because tsk is running in kernel 
  416.  * mode and can't produce new pfaults. */
  417. tsk->thread.pfault_wait = 0ULL;
  418. }
  419.                 /* go to sleep */
  420.                 wait_event(queue, tsk->thread.pfault_wait == 0ULL);
  421. }
  422. }
  423. #endif