blob: d06ba87f1a19e8b6d40906d330446395ffeec0d3 [file] [log] [blame]
Jeremy Kerr7cd58e42007-12-20 16:39:59 +09001/*
2 * SPU mm fault handler
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2007
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 * Author: Jeremy Kerr <jk@ozlabs.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23#include <linux/sched.h>
24#include <linux/mm.h>
25#include <linux/module.h>
26
27#include <asm/spu.h>
28#include <asm/spu_csa.h>
29
30/*
31 * This ought to be kept in sync with the powerpc specific do_page_fault
32 * function. Currently, there are a few corner cases that we haven't had
33 * to handle fortunately.
34 */
35int spu_handle_mm_fault(struct mm_struct *mm, unsigned long ea,
36 unsigned long dsisr, unsigned *flt)
37{
38 struct vm_area_struct *vma;
39 unsigned long is_write;
40 int ret;
41
Jeremy Kerr60ee0312009-02-17 11:44:14 +110042 if (mm == NULL)
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090043 return -EFAULT;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110044
45 if (mm->pgd == NULL)
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090046 return -EFAULT;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090047
48 down_read(&mm->mmap_sem);
Jeremy Kerr60ee0312009-02-17 11:44:14 +110049 ret = -EFAULT;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090050 vma = find_vma(mm, ea);
51 if (!vma)
Jeremy Kerr60ee0312009-02-17 11:44:14 +110052 goto out_unlock;
53
54 if (ea < vma->vm_start) {
55 if (!(vma->vm_flags & VM_GROWSDOWN))
56 goto out_unlock;
57 if (expand_stack(vma, ea))
58 goto out_unlock;
59 }
60
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090061 is_write = dsisr & MFC_DSISR_ACCESS_PUT;
62 if (is_write) {
63 if (!(vma->vm_flags & VM_WRITE))
Jeremy Kerr60ee0312009-02-17 11:44:14 +110064 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090065 } else {
66 if (dsisr & MFC_DSISR_ACCESS_DENIED)
Jeremy Kerr60ee0312009-02-17 11:44:14 +110067 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090068 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
Jeremy Kerr60ee0312009-02-17 11:44:14 +110069 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090070 }
Jeremy Kerr60ee0312009-02-17 11:44:14 +110071
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090072 ret = 0;
Linus Torvaldsd06063c2009-04-10 09:01:23 -070073 *flt = handle_mm_fault(mm, vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090074 if (unlikely(*flt & VM_FAULT_ERROR)) {
75 if (*flt & VM_FAULT_OOM) {
76 ret = -ENOMEM;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110077 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090078 } else if (*flt & VM_FAULT_SIGBUS) {
79 ret = -EFAULT;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110080 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090081 }
82 BUG();
83 }
Jeremy Kerr60ee0312009-02-17 11:44:14 +110084
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090085 if (*flt & VM_FAULT_MAJOR)
86 current->maj_flt++;
87 else
88 current->min_flt++;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110089
90out_unlock:
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090091 up_read(&mm->mmap_sem);
92 return ret;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090093}
94EXPORT_SYMBOL_GPL(spu_handle_mm_fault);