]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - fs/proc/vmcore.c
pwm: imx: support output polarity inversion
[karo-tx-linux.git] / fs / proc / vmcore.c
index a1a16eb97c7bd141c7a68419b44769ebea35c854..9100d695988690e0ae7e3263ad2502aa43d94e8c 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/crash_dump.h>
 #include <linux/list.h>
 #include <linux/vmalloc.h>
+#include <linux/pagemap.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
 #include "internal.h"
@@ -123,11 +124,65 @@ static ssize_t read_from_oldmem(char *buf, size_t count,
        return read;
 }
 
+/*
+ * Architectures may override this function to allocate ELF header in 2nd kernel
+ */
+int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
+{
+       return 0;
+}
+
+/*
+ * Architectures may override this function to free header
+ */
+void __weak elfcorehdr_free(unsigned long long addr)
+{}
+
+/*
+ * Architectures may override this function to read from ELF header
+ */
+ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
+{
+       return read_from_oldmem(buf, count, ppos, 0);
+}
+
+/*
+ * Architectures may override this function to read from notes sections
+ */
+ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
+{
+       return read_from_oldmem(buf, count, ppos, 0);
+}
+
+/*
+ * Architectures may override this function to map oldmem
+ */
+int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma,
+                                 unsigned long from, unsigned long pfn,
+                                 unsigned long size, pgprot_t prot)
+{
+       return remap_pfn_range(vma, from, pfn, size, prot);
+}
+
+/*
+ * Copy to either kernel or user space
+ */
+static int copy_to(void *target, void *src, size_t size, int userbuf)
+{
+       if (userbuf) {
+               if (copy_to_user((char __user *) target, src, size))
+                       return -EFAULT;
+       } else {
+               memcpy(target, src, size);
+       }
+       return 0;
+}
+
 /* Read from the ELF header and then the crash dump. On error, negative value is
  * returned otherwise number of bytes read are returned.
  */
-static ssize_t read_vmcore(struct file *file, char __user *buffer,
-                               size_t buflen, loff_t *fpos)
+static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
+                            int userbuf)
 {
        ssize_t acc = 0, tmp;
        size_t tsz;
@@ -144,7 +199,7 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
        /* Read ELF core header */
        if (*fpos < elfcorebuf_sz) {
                tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
-               if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
+               if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf))
                        return -EFAULT;
                buflen -= tsz;
                *fpos += tsz;
@@ -162,7 +217,7 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
 
                tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
                kaddr = elfnotes_buf + *fpos - elfcorebuf_sz;
-               if (copy_to_user(buffer, kaddr, tsz))
+               if (copy_to(buffer, kaddr, tsz, userbuf))
                        return -EFAULT;
                buflen -= tsz;
                *fpos += tsz;
@@ -178,7 +233,7 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
                if (*fpos < m->offset + m->size) {
                        tsz = min_t(size_t, m->offset + m->size - *fpos, buflen);
                        start = m->paddr + *fpos - m->offset;
-                       tmp = read_from_oldmem(buffer, tsz, &start, 1);
+                       tmp = read_from_oldmem(buffer, tsz, &start, userbuf);
                        if (tmp < 0)
                                return tmp;
                        buflen -= tsz;
@@ -195,6 +250,55 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
        return acc;
 }
 
+static ssize_t read_vmcore(struct file *file, char __user *buffer,
+                          size_t buflen, loff_t *fpos)
+{
+       return __read_vmcore((__force char *) buffer, buflen, fpos, 1);
+}
+
+/*
+ * The vmcore fault handler uses the page cache and fills data using the
+ * standard __vmcore_read() function.
+ *
+ * On s390 the fault handler is used for memory regions that can't be mapped
+ * directly with remap_pfn_range().
+ */
+static int mmap_vmcore_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
+{
+#ifdef CONFIG_S390
+       struct address_space *mapping = vma->vm_file->f_mapping;
+       pgoff_t index = vmf->pgoff;
+       struct page *page;
+       loff_t offset;
+       char *buf;
+       int rc;
+
+       page = find_or_create_page(mapping, index, GFP_KERNEL);
+       if (!page)
+               return VM_FAULT_OOM;
+       if (!PageUptodate(page)) {
+               offset = (loff_t) index << PAGE_CACHE_SHIFT;
+               buf = __va((page_to_pfn(page) << PAGE_SHIFT));
+               rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0);
+               if (rc < 0) {
+                       unlock_page(page);
+                       page_cache_release(page);
+                       return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
+               }
+               SetPageUptodate(page);
+       }
+       unlock_page(page);
+       vmf->page = page;
+       return 0;
+#else
+       return VM_FAULT_SIGBUS;
+#endif
+}
+
+static const struct vm_operations_struct vmcore_mmap_ops = {
+       .fault = mmap_vmcore_fault,
+};
+
 /**
  * alloc_elfnotes_buf - allocate buffer for ELF note segment in
  *                      vmalloc memory
@@ -223,7 +327,7 @@ static inline char *alloc_elfnotes_buf(size_t notes_sz)
  * regions in the 1st kernel pointed to by PT_LOAD entries) into
  * virtually contiguous user-space in ELF layout.
  */
-#if defined(CONFIG_MMU) && !defined(CONFIG_S390)
+#ifdef CONFIG_MMU
 static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
 {
        size_t size = vma->vm_end - vma->vm_start;
@@ -241,6 +345,7 @@ static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
 
        vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
        vma->vm_flags |= VM_MIXEDMAP;
+       vma->vm_ops = &vmcore_mmap_ops;
 
        len = 0;
 
@@ -282,9 +387,9 @@ static int mmap_vmcore(struct file *file, struct vm_area_struct *vma)
 
                        tsz = min_t(size_t, m->offset + m->size - start, size);
                        paddr = m->paddr + start - m->offset;
-                       if (remap_pfn_range(vma, vma->vm_start + len,
-                                           paddr >> PAGE_SHIFT, tsz,
-                                           vma->vm_page_prot))
+                       if (remap_oldmem_pfn_range(vma, vma->vm_start + len,
+                                                  paddr >> PAGE_SHIFT, tsz,
+                                                  vma->vm_page_prot))
                                goto fail;
                        size -= tsz;
                        start += tsz;
@@ -357,7 +462,7 @@ static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr)
                notes_section = kmalloc(max_sz, GFP_KERNEL);
                if (!notes_section)
                        return -ENOMEM;
-               rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
+               rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
                if (rc < 0) {
                        kfree(notes_section);
                        return rc;
@@ -444,7 +549,8 @@ static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf)
                if (phdr_ptr->p_type != PT_NOTE)
                        continue;
                offset = phdr_ptr->p_offset;
-               rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0);
+               rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
+                                          &offset);
                if (rc < 0)
                        return rc;
                notes_buf += phdr_ptr->p_memsz;
@@ -536,7 +642,7 @@ static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr)
                notes_section = kmalloc(max_sz, GFP_KERNEL);
                if (!notes_section)
                        return -ENOMEM;
-               rc = read_from_oldmem(notes_section, max_sz, &offset, 0);
+               rc = elfcorehdr_read_notes(notes_section, max_sz, &offset);
                if (rc < 0) {
                        kfree(notes_section);
                        return rc;
@@ -623,7 +729,8 @@ static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf)
                if (phdr_ptr->p_type != PT_NOTE)
                        continue;
                offset = phdr_ptr->p_offset;
-               rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0);
+               rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz,
+                                          &offset);
                if (rc < 0)
                        return rc;
                notes_buf += phdr_ptr->p_memsz;
@@ -810,7 +917,7 @@ static int __init parse_crash_elf64_headers(void)
        addr = elfcorehdr_addr;
 
        /* Read Elf header */
-       rc = read_from_oldmem((char*)&ehdr, sizeof(Elf64_Ehdr), &addr, 0);
+       rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr);
        if (rc < 0)
                return rc;
 
@@ -837,7 +944,7 @@ static int __init parse_crash_elf64_headers(void)
        if (!elfcorebuf)
                return -ENOMEM;
        addr = elfcorehdr_addr;
-       rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
+       rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
        if (rc < 0)
                goto fail;
 
@@ -866,7 +973,7 @@ static int __init parse_crash_elf32_headers(void)
        addr = elfcorehdr_addr;
 
        /* Read Elf header */
-       rc = read_from_oldmem((char*)&ehdr, sizeof(Elf32_Ehdr), &addr, 0);
+       rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr);
        if (rc < 0)
                return rc;
 
@@ -892,7 +999,7 @@ static int __init parse_crash_elf32_headers(void)
        if (!elfcorebuf)
                return -ENOMEM;
        addr = elfcorehdr_addr;
-       rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0);
+       rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr);
        if (rc < 0)
                goto fail;
 
@@ -919,7 +1026,7 @@ static int __init parse_crash_elf_headers(void)
        int rc=0;
 
        addr = elfcorehdr_addr;
-       rc = read_from_oldmem(e_ident, EI_NIDENT, &addr, 0);
+       rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr);
        if (rc < 0)
                return rc;
        if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
@@ -952,7 +1059,14 @@ static int __init vmcore_init(void)
 {
        int rc = 0;
 
-       /* If elfcorehdr= has been passed in cmdline, then capture the dump.*/
+       /* Allow architectures to allocate ELF header in 2nd kernel */
+       rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size);
+       if (rc)
+               return rc;
+       /*
+        * If elfcorehdr= has been passed in cmdline or created in 2nd kernel,
+        * then capture the dump.
+        */
        if (!(is_vmcore_usable()))
                return rc;
        rc = parse_crash_elf_headers();
@@ -960,6 +1074,8 @@ static int __init vmcore_init(void)
                pr_warn("Kdump: vmcore not initialized\n");
                return rc;
        }
+       elfcorehdr_free(elfcorehdr_addr);
+       elfcorehdr_addr = ELFCORE_ADDR_ERR;
 
        proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
        if (proc_vmcore)