xref: /linux/arch/nios2/mm/init.c (revision 7a9b709e7cc5ce1ffb84ce07bf6d157e1de758df)
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>
26 #include <linux/memblock.h>
27 #include <linux/slab.h>
28 #include <linux/binfmts.h>
29 #include <linux/execmem.h>
30 
31 #include <asm/setup.h>
32 #include <asm/page.h>
33 #include <asm/sections.h>
34 #include <asm/tlb.h>
35 #include <asm/mmu_context.h>
36 #include <asm/cpuinfo.h>
37 #include <asm/processor.h>
38 
39 pgd_t *pgd_current;
40 
41 /*
42  * paging_init() continues the virtual memory environment setup which
43  * was begun by the code in arch/head.S.
44  * The parameters are pointers to where to stick the starting and ending
45  * addresses of available kernel virtual memory.
46  */
47 void __init paging_init(void)
48 {
49 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
50 
51 	pagetable_init();
52 	pgd_current = swapper_pg_dir;
53 
54 	max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
55 
56 	/* pass the memory from the bootmem allocator to the main allocator */
57 	free_area_init(max_zone_pfn);
58 
59 	flush_dcache_range((unsigned long)empty_zero_page,
60 			(unsigned long)empty_zero_page + PAGE_SIZE);
61 }
62 
63 void __init mmu_init(void)
64 {
65 	flush_tlb_all();
66 }
67 
68 pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
69 pte_t invalid_pte_table[PTRS_PER_PTE] __aligned(PAGE_SIZE);
70 static struct page *kuser_page[1];
71 static struct vm_special_mapping vdso_mapping = {
72 	.name = "[vdso]",
73 	.pages = kuser_page,
74 };
75 
76 static int alloc_kuser_page(void)
77 {
78 	extern char __kuser_helper_start[], __kuser_helper_end[];
79 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
80 	unsigned long vpage;
81 
82 	vpage = get_zeroed_page(GFP_ATOMIC);
83 	if (!vpage)
84 		return -ENOMEM;
85 
86 	/* Copy kuser helpers */
87 	memcpy((void *)vpage, __kuser_helper_start, kuser_sz);
88 
89 	flush_icache_range(vpage, vpage + KUSER_SIZE);
90 	kuser_page[0] = virt_to_page(vpage);
91 
92 	return 0;
93 }
94 arch_initcall(alloc_kuser_page);
95 
96 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
97 {
98 	struct mm_struct *mm = current->mm;
99 	struct vm_area_struct *vma;
100 
101 	mmap_write_lock(mm);
102 
103 	/* Map kuser helpers to user space address */
104 	vma = _install_special_mapping(mm, KUSER_BASE, KUSER_SIZE,
105 				      VM_READ | VM_EXEC | VM_MAYREAD |
106 				      VM_MAYEXEC, &vdso_mapping);
107 
108 	mmap_write_unlock(mm);
109 
110 	return IS_ERR(vma) ? PTR_ERR(vma) : 0;
111 }
112 
113 const char *arch_vma_name(struct vm_area_struct *vma)
114 {
115 	return (vma->vm_start == KUSER_BASE) ? "[kuser]" : NULL;
116 }
117 
118 static const pgprot_t protection_map[16] = {
119 	[VM_NONE]					= MKP(0, 0, 0),
120 	[VM_READ]					= MKP(0, 0, 1),
121 	[VM_WRITE]					= MKP(0, 0, 0),
122 	[VM_WRITE | VM_READ]				= MKP(0, 0, 1),
123 	[VM_EXEC]					= MKP(1, 0, 0),
124 	[VM_EXEC | VM_READ]				= MKP(1, 0, 1),
125 	[VM_EXEC | VM_WRITE]				= MKP(1, 0, 0),
126 	[VM_EXEC | VM_WRITE | VM_READ]			= MKP(1, 0, 1),
127 	[VM_SHARED]					= MKP(0, 0, 0),
128 	[VM_SHARED | VM_READ]				= MKP(0, 0, 1),
129 	[VM_SHARED | VM_WRITE]				= MKP(0, 1, 0),
130 	[VM_SHARED | VM_WRITE | VM_READ]		= MKP(0, 1, 1),
131 	[VM_SHARED | VM_EXEC]				= MKP(1, 0, 0),
132 	[VM_SHARED | VM_EXEC | VM_READ]			= MKP(1, 0, 1),
133 	[VM_SHARED | VM_EXEC | VM_WRITE]		= MKP(1, 1, 0),
134 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= MKP(1, 1, 1)
135 };
136 DECLARE_VM_GET_PAGE_PROT
137 
138 #ifdef CONFIG_EXECMEM
139 static struct execmem_info execmem_info __ro_after_init;
140 
141 struct execmem_info __init *execmem_arch_setup(void)
142 {
143 	execmem_info = (struct execmem_info){
144 		.ranges = {
145 			[EXECMEM_DEFAULT] = {
146 				.start	= MODULES_VADDR,
147 				.end	= MODULES_END,
148 				.pgprot	= PAGE_KERNEL_EXEC,
149 				.alignment = 1,
150 			},
151 		},
152 	};
153 
154 	return &execmem_info;
155 }
156 #endif /* CONFIG_EXECMEM */
157