Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Altera Corporation |
| 3 | * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch> |
| 4 | * Copyright (C) 2009 Wind River Systems Inc |
| 5 | * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com |
| 6 | * Copyright (C) 2004 Microtronix Datacom Ltd |
| 7 | * |
| 8 | * based on arch/m68k/mm/init.c |
| 9 | * |
| 10 | * This file is subject to the terms and conditions of the GNU General Public |
| 11 | * License. See the file "COPYING" in the main directory of this archive |
| 12 | * for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/signal.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/ptrace.h> |
| 22 | #include <linux/mman.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/pagemap.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 26 | #include <linux/memblock.h> |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/binfmts.h> |
| 29 | |
| 30 | #include <asm/setup.h> |
| 31 | #include <asm/page.h> |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 32 | #include <asm/sections.h> |
| 33 | #include <asm/tlb.h> |
| 34 | #include <asm/mmu_context.h> |
| 35 | #include <asm/cpuinfo.h> |
| 36 | #include <asm/processor.h> |
| 37 | |
| 38 | pgd_t *pgd_current; |
| 39 | |
| 40 | /* |
| 41 | * paging_init() continues the virtual memory environment setup which |
| 42 | * was begun by the code in arch/head.S. |
| 43 | * The parameters are pointers to where to stick the starting and ending |
| 44 | * addresses of available kernel virtual memory. |
| 45 | */ |
| 46 | void __init paging_init(void) |
| 47 | { |
Mike Rapoport | fa3354e | 2020-06-03 15:57:06 -0700 | [diff] [blame] | 48 | unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 }; |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 49 | |
| 50 | pagetable_init(); |
| 51 | pgd_current = swapper_pg_dir; |
| 52 | |
Mike Rapoport | fa3354e | 2020-06-03 15:57:06 -0700 | [diff] [blame] | 53 | max_zone_pfn[ZONE_NORMAL] = max_mapnr; |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 54 | |
| 55 | /* pass the memory from the bootmem allocator to the main allocator */ |
Mike Rapoport | fa3354e | 2020-06-03 15:57:06 -0700 | [diff] [blame] | 56 | free_area_init(max_zone_pfn); |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 57 | |
| 58 | flush_dcache_range((unsigned long)empty_zero_page, |
| 59 | (unsigned long)empty_zero_page + PAGE_SIZE); |
| 60 | } |
| 61 | |
| 62 | void __init mem_init(void) |
| 63 | { |
| 64 | unsigned long end_mem = memory_end; /* this must not include |
| 65 | kernel stack at top */ |
| 66 | |
| 67 | pr_debug("mem_init: start=%lx, end=%lx\n", memory_start, memory_end); |
| 68 | |
| 69 | end_mem &= PAGE_MASK; |
| 70 | high_memory = __va(end_mem); |
| 71 | |
| 72 | /* this will put all memory onto the freelists */ |
Mike Rapoport | c6ffc5c | 2018-10-30 15:09:30 -0700 | [diff] [blame] | 73 | memblock_free_all(); |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void __init mmu_init(void) |
| 77 | { |
| 78 | flush_tlb_all(); |
| 79 | } |
| 80 | |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 81 | #define __page_aligned(order) __aligned(PAGE_SIZE << (order)) |
| 82 | pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned(PGD_ORDER); |
| 83 | pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned(PTE_ORDER); |
| 84 | static struct page *kuser_page[1]; |
| 85 | |
| 86 | static int alloc_kuser_page(void) |
| 87 | { |
| 88 | extern char __kuser_helper_start[], __kuser_helper_end[]; |
| 89 | int kuser_sz = __kuser_helper_end - __kuser_helper_start; |
| 90 | unsigned long vpage; |
| 91 | |
| 92 | vpage = get_zeroed_page(GFP_ATOMIC); |
| 93 | if (!vpage) |
| 94 | return -ENOMEM; |
| 95 | |
| 96 | /* Copy kuser helpers */ |
| 97 | memcpy((void *)vpage, __kuser_helper_start, kuser_sz); |
| 98 | |
| 99 | flush_icache_range(vpage, vpage + KUSER_SIZE); |
| 100 | kuser_page[0] = virt_to_page(vpage); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | arch_initcall(alloc_kuser_page); |
| 105 | |
| 106 | int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) |
| 107 | { |
| 108 | struct mm_struct *mm = current->mm; |
| 109 | int ret; |
| 110 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 111 | mmap_write_lock(mm); |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 112 | |
| 113 | /* Map kuser helpers to user space address */ |
| 114 | ret = install_special_mapping(mm, KUSER_BASE, KUSER_SIZE, |
| 115 | VM_READ | VM_EXEC | VM_MAYREAD | |
| 116 | VM_MAYEXEC, kuser_page); |
| 117 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 118 | mmap_write_unlock(mm); |
Ley Foon Tan | 5ccc6af5 | 2014-11-06 15:19:41 +0800 | [diff] [blame] | 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | const char *arch_vma_name(struct vm_area_struct *vma) |
| 124 | { |
| 125 | return (vma->vm_start == KUSER_BASE) ? "[kuser]" : NULL; |
| 126 | } |